Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
lib/marvel: add a client Still unsure about this client API layout, but we'll see.
lib/marvel/client.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++
diff --git a/lib/marvel/client.rb b/lib/marvel/client.rb new file mode 100644 index 0000000000000000000000000000000000000000..54799269f3425d045a3a4f9f8deb03de62e91d96 --- /dev/null +++ b/lib/marvel/client.rb @@ -0,0 +1,45 @@ +module Marvel + class Client + HOST = "https://gateway.marvel.com" + PRIVKEY = Configuration["api"]["private"] + PUBKEY = Configuration["api"]["public"] + + def initialize + if PRIVKEY.nil? || PUBKEY.nil? + raise ArgumentError.new("You must set Marvel's api keys on config/config.yml") + end + end + + def story(id) + dial("stories/#{id}").first + end + + def characters_from_story(story_id) + dial("stories/#{story_id}/characters") + end + + private + + def dial(endpoint) + response = HTTP::Client.get("#{HOST}/v1/public/#{endpoint}", parameters) + + extract_results(response) + end + + def extract_results(response) + unless response.nil? + response["data"]["results"] + end + end + + def parameters + now = Time.now.to_i + + { + "ts" => now, + "apikey" => PUBKEY, + "hash" => Digest::MD5.hexdigest("#{now}#{PRIVKEY}#{PUBKEY}") + } + end + end +end