Sha256: ba566d32d1e6280bc30f4ab7abf567c0ca049a8c49f1938e72a053e21aaf75d7

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 KB

Contents

module Zunnit	
	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

		# Connection object
		def connection
			@connection ||= Faraday.new(:url => "#{Zunnit::URL}#{self.client}")
		end
		private :connection
		
		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		
			action_url	= Zunnit.actions[action]			
			response = connection.send(http_method) do |req|
				req.url(action_url)
				req.params	= params_for options 
			end
			
			response
		end
		
		# Make a GET request to Zunnit's API
		def get(action, options={})
			self.request(:get, action, options)
		end
		
		# Make a POST request to Zunnit's API
		def post(action, options={})
			self.request(:post, action, options)
		end
	end 
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zunnit-0.0.4 lib/zunnit/api.rb