Sha256: 1e76a6943e010ed94997fcce559af92b000b043e2865fdc777bf6b9859065be4

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Zunnit
	# Fixed constants
	API_URL			= "http://api.zunnit.com/"
	API_ACTIONS = {
		:related_items										=> "/related/items",
		:recommendation_items_for_user		=> "/recommendation/items/for_user",
		:recommendation_groups_for_user		=> "/recommendation/groups/for_user",
		:recommendation_users_for_user		=> "/recommendation/users/for_user",
		:recommendation_users_for_item		=> "/recommendation/users/for_item",
		:recommendation_tags_for_item			=> "/recommendation/tags/for_item",
		:recommendation_cluster_for_item	=> "/recommendation/cluster/for_item",
		:action_items_add									=> "/action/items/add",
		:action_items_rate								=> "/action/items/rate",
		:action_items_view								=> "/action/items/view",
		:action_user_follow								=> "/action/user/follow"		
	}
	
	# Configurable constants
	API_CLIENT	= "busk"
	API_KEY	= "busk"
	API_VERSION	= "1.0"	
	
	class << self	
		# API URL for action
		def url_for(action)
			raise "Invalid action '#{action}'" unless API_ACTIONS[action]
			
			# Generate the URL
			url = "#{API_URL}#{API_CLIENT}#{API_ACTIONS[action]}?api_key=#{API_KEY}"
			url
		end
		
		# Make a request to Zunnit's API
		def request(http_method, action, options={})
			connection = Faraday.new(:url => self.url_for(action))
			response = case http_method.to_sym
			when :get
				connection.get { |req| req.params = options }
			when :post
				connection.post { |req| req.params = options }
			else
				raise "Invalid http_method \"#{http_method}\""
			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.3 lib/zunnit/zunnit.rb