module Sentimeta::RestClient
Attributes
namespace[RW]
require “sentimeta/error/record_not_found”
Public Instance Methods
fetch(path, params={})
click to toggle source
# File lib/sentimeta/rest_client.rb, line 31 def fetch path, params={} method = params.delete(:method) || :get token = params.delete(:token) params = params.keep_if { |key, value| !!value } url = generate_uri path, params url = "#{ url }?p=#{ params.to_json }" if method.to_sym == :get && params.present? Sentimeta.logger.debug " #{ 'Sentimeta:'.colorize :green } #{ method.upcase } #{ url } #{ params.to_json if params.present? && method.to_sym != :get }" Observers.each { |observer| observer.notify("fetch", method: method, url: url, params: params) } headers = {} headers['X-SERVER-ACCESS-TOKEN'] = Sentimeta.server_token if Sentimeta.server_token headers['X-TOKEN'] = token if token response = begin ::RestClient::Request.execute \ method: method, url: URI::encode(url), payload: params, headers: headers, accept: :json rescue ::RestClient::Exception => e Sentimeta.logger.error " #{ 'Sentimeta:'.colorize :red } #{ e.message } / #{ e.response }" return ApiResponse.new e.response rescue Exception => e raise Sentimeta::Error::Unreachable.new e.message end ApiResponse.new response end
generate_uri(endpoint, options={})
click to toggle source
# File lib/sentimeta/rest_client.rb, line 70 def generate_uri endpoint, options={} [].tap do |components| components << Sentimeta.endpoint components << (options.delete(:sphere) || try(:sphere)) unless endpoint == :spheres components << namespace unless namespace.nil? components << endpoint components << options.delete(:filter) if endpoint == :attributes # TODO remove components << options.delete(:provider) if endpoint == :prices # TODO remove components << options.delete(:id) end.compact.join('/') end
method_missing(method, *args)
click to toggle source
# File lib/sentimeta/rest_client.rb, line 61 def method_missing method, *args if %i(get post put patch delete options).include?(method) path, params = args fetch path, (params || {}).merge(method: method) else Sentimeta.logger.fatal "Unknown method #{ method }(#{ method.class.name })" # TODO remove end end