Sha256: c942202a97ec841e677207afa98b51e7be08a386825d9d01306bf93b5cb72fa0
Contents?: true
Size: 1.55 KB
Versions: 3
Compression:
Stored size: 1.55 KB
Contents
module Zunnit # Singleton API Accessor def self.api @api ||= Api.new end # API Class class Api attr_accessor :client, :key, :version def initialize self.client = Zunnit.client self.key = Zunnit.key self.version = Zunnit.version yield self if block_given? end # Make a GET request to Zunnit's API def get(action, options={}) request(:get, action, options) end # Make a POST request to Zunnit's API def post(action, options={}) request(:post, action, options) end private def connection @connection ||= Faraday.new(:url => Zunnit::URL) end def params_for(options={}) return { :api_key => self.key }.merge(options) end # Make a request to Zunnit's API def request(http_method, action, options={}) # Raise error if the actions is invalid unless Zunnit.actions[action] raise "Invalid action \"#{action}\"" end # Raise error if the http method is invalid unless [:get, :post].include? http_method raise "Invalid http_method \"#{http_method}\"" end # Make the Request response = connection.send(http_method) do |req| action_url = "/#{client}#{Zunnit.actions[action]}" req.url(action_url) if http_method.to_s =~ /post|put/ req.body = params_for options else req.params = params_for options end end # return HASH for body JSON JSON response.body, :symbolize_names => true end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
zunnit-0.3.3 | lib/zunnit/api.rb |
zunnit-0.3.2 | lib/zunnit/api.rb |
zunnit-0.3.1 | lib/zunnit/api.rb |