Ruby Example
Naranek.2570:
EDIT: I just did more code to share so I’ll just update this post atm :-)
NOTE: run this on IRB so you can track the code easier
NOTE2: You want json gem (gem install json)
WARNING I’m trusting all SSL certs, you should NOT do that if you want to do something bigger. If I have the time I may post how to get their certs and use them in the right way. WARNING
I was just testing the API with ruby and this is a basic idea of how to get access to it:
=SIMPLE TEST=
require ’net/http’
require ’net/https’
uri = URI.parse(“https://api.guildwars2.com/”)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.get(“/v1/items.json”)
response = http.get(“/v1/items.json”)
response.body
==MORE CODE (print item_details)=
require 'net/http'
require 'net/https'
require 'json'
#Just some paths I'll use
items_list = "/v1/items.json"
item_details = "/v1/item_details.json?item_id="
#I need this method to recurse through the item_details hash, need better formating
def item_details_print(item_details)
if item_details.is_a?(Hash) || item_details.is_a?(Array) then
item_details.each do |key, value|
if value.nil?
puts " #{value}"
item_details_print(key)
else
print "#{key} : "
item_details_print(value)
end
end
else
print "#{item_details} \n"
end
end
#Connection to the API server
uri = URI.parse("https://api.guildwars2.com/")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
##JSON to Ruby hash: items.json
response_list = JSON.parse(http.get(items_list).body)
#Print the first item. Just uncomment the puts
#WARNING: do not do a response["items"] on a console
#It will take forever to print it on the screen sometimes.
puts response_list["items"][1]
#Ok, we have the items list, details now!
details = item_details + response_list["items"][1].to_s
response_details = JSON.parse(http.get(details).body)
#Lets see thoses details
item_details_print(response_details)
===
I’ll probably be doing some more code, I kinda want a “console” program when playing since my 3rd screen is a linux machine :D
I’ll post around here if I do more.
Happy coding!
Killer Rhino.6794:
cool. thanks for sharing!
Naranek.2570:
Just an update, more example code, nothing fancy.
BTW, I wish they didn’t put arrays inside hashes inside more hashes when you retrieve the item_details hash, also arrays with keys and arrays without keys….. not cool anet! not cool! >.>