Sha256: b587f63e780fd38270acaf9ceaf1a604974ac58766684073727082b2dc2f2bb7
Contents?: true
Size: 1.59 KB
Versions: 2
Compression:
Stored size: 1.59 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 def url "http://#{self.client}.zunnit.com/" 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 => 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
zunnit-0.4.3 | lib/zunnit/api.rb |
zunnit-0.4.2 | lib/zunnit/api.rb |