So I have an action in my controller that does a get_response to an API:
def memeapi
require "net/http"
require "uri"
@meme = Meme.new(params[:meme])
url = "http://version1.api.memegenerator.net/Instance_Create?username=apigen&password=SECRET&languageCode=en&generatorID=#{@meme.memeid}&imageID=#{@meme.imgid}&text0=#{@meme.text0}&text1=#{@meme.text1}"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
# I want to convert it to the Rails data structure - a hash
result = ActiveSupport::JSON.decode(data)
end
Ok but now I want to get back the information, use it to create another object, but I cant even format the information I am getting, can anyone tell me what I am doing wrong, what am I missing?
I want to be able to access the information from the get_response…
Thank you.
This is the JSON structure
{"success":true,"result":{"generatorID":45,"displayName":"Insanity Wolf","urlName":"Insanity-Wolf","totalVotesScore":0,"imageUrl":"/cache/images/400x/0/0/20.jpg","instanceID":13226270,"text0":"push","text1":null,"instanceImageUrl":"/cache/instances/400x/12/12916/13226270.jpg","instanceUrl":"http://memegenerator.net/instance/13226270"}}
I dont want to save all the fields btw…
resultwill look something like this afterJSON.decodein your code.You have a nested hash (hash as part of a hash) here, which you can access like this:
What you actually want to do with this information, I cannot guess. Maybe you want to update the
Memeobject you created?