Sha256: 83ab3073a022f73f524c1e031a8a48715bd6a20201d53d67351634574b13a982
Contents?: true
Size: 1.54 KB
Versions: 7
Compression:
Stored size: 1.54 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 = 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
7 entries across 7 versions & 1 rubygems