Sha256: b27a7704156b2d54b3262e552293c4e7b7b7e921d8f3599ed31ac080b5859839
Contents?: true
Size: 1.6 KB
Versions: 3
Compression:
Stored size: 1.6 KB
Contents
require 'open-uri' require 'hashie/mash' require 'json' module RSqoot module Request def get(path, opts = {}) uri, headers = url_generator(path, opts) begin json = JSON.parse uri.open(headers).read ::Hashie::Mash.new json rescue nil end end def url_generator(path, opts = {}, require_key = false) uri = URI.parse base_api_url headers = {read_timeout: read_timeout} uri.path = '/v2/' + path query = options_parser opts endpoint = path.split('/')[0] case authentication_method when :header headers.merge! h = {"Authorization" => "api_key #{api_key(endpoint)}"} query = query + "&api_key=#{api_key(endpoint)}" if require_key when :parameter query = query + "&api_key=#{api_key(endpoint)}" end uri.query = query [uri, headers] end private def private_endpoints %w(clicks commissions) end def public_endpoints %w(categories deals merchants providers) end def api_key(endpoint='') if private_endpoints.include? endpoint private_api_key elsif public_endpoints.include? endpoint public_api_key else raise "No such endpoint #{endpoint} available." end end # Example: options = {per_page: 10, page: 1} # Options should be parsed as http query: per_page=10&page=1 # # @return [String] def options_parser(options = {}) query = options.map do |key, value| [key, value].map(&:to_s).join('=') end.join('&') URI.encode query end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
rsqoot-0.3.2 | lib/rsqoot/request.rb |
rsqoot-0.3.1 | lib/rsqoot/request.rb |
rsqoot-0.3.0 | lib/rsqoot/request.rb |