class Revolut::Api::Base
Attributes
configuration[RW]
headers[RW]
host[RW]
memoized[RW]
Public Class Methods
new(host: "api.revolut.com", configuration: ::Revolut::Api.configuration)
click to toggle source
# File lib/revolut/api/base.rb, line 6 def initialize(host: "api.revolut.com", configuration: ::Revolut::Api.configuration) self.host = host self.configuration = configuration self.headers = { 'Host' => self.host } self.memoized = {} end
Public Instance Methods
authable?()
click to toggle source
# File lib/revolut/api/base.rb, line 114 def authable? !self.configuration.user_id.to_s.empty? && !self.configuration.access_token.to_s.empty? end
check_configuration!()
click to toggle source
# File lib/revolut/api/base.rb, line 24 def check_configuration! %w(user_id access_token user_agent device_id device_model).each do |config_key| raise ::Revolut::Api::MissingConfigurationError, "You need to specify the #{config_key.gsub("_", " ")}!" if ::Revolut::Api.configuration.send(config_key).to_s.empty? end end
get(path, params: {}, options: {})
click to toggle source
# File lib/revolut/api/base.rb, line 61 def get(path, params: {}, options: {}) request path, method: :get, params: params, options: options end
log(message)
click to toggle source
# File lib/revolut/api/base.rb, line 118 def log(message) puts "[Revolut::Api] - #{Time.now}: #{message}" if !message.to_s.empty? && self.configuration.verbose end
patch(path, params: {}, data: {}, options: {})
click to toggle source
# File lib/revolut/api/base.rb, line 69 def patch(path, params: {}, data: {}, options: {}) request path, method: :patch, params: params, data: data, options: options end
post(path, params: {}, data: {}, options: {})
click to toggle source
# File lib/revolut/api/base.rb, line 65 def post(path, params: {}, data: {}, options: {}) request path, method: :post, params: params, data: data, options: options end
quotes(from: [], to: [], endpoint: "quote", options: {})
click to toggle source
# File lib/revolut/api/base.rb, line 30 def quotes(from: [], to: [], endpoint: "quote", options: {}) from = (from.is_a?(Array) ? from : split_to_array(from)).collect(&:upcase) to = (to.is_a?(Array) ? to : split_to_array(to)).collect(&:upcase) args = [] from.each do |f| to.each do |t| args << "#{f.to_s.upcase}#{t.to_s.upcase}" end end params = {symbol: args} options[:params_encoder] = ::Faraday::FlatParamsEncoder response = get(endpoint, params: params, options: options) data = [] if response && response.is_a?(Array) && response.any? response.each do |hash| data << ::Revolut::Api::Response::Quote.new(hash) end end return data end
request(path, method: :get, params: {}, data: {}, options: {})
click to toggle source
# File lib/revolut/api/base.rb, line 73 def request(path, method: :get, params: {}, data: {}, options: {}) check_configuration! if options.fetch(:check_configuration, true) authenticate = options.fetch(:authenticate, true) params_encoder = options.fetch(:params_encoder, nil) user_agent = options.fetch(:user_agent, self.configuration.user_agent) self.headers.merge!(user_agent: user_agent) unless user_agent.to_s.empty? opts = {url: to_uri(path)} opts.merge!(request: { params_encoder: params_encoder }) unless params_encoder.nil? connection = Faraday.new(opts) do |builder| builder.headers = self.headers builder.request :basic_auth, self.configuration.user_id, self.configuration.access_token if authenticate && authable? builder.request :json builder.response :json builder.response :logger if self.configuration.verbose builder.adapter :net_http end response = case method when :get connection.get do |request| request.params = params if params && !params.empty? end&.body when :post, :patch connection.send(method) do |request| request.body = data request.params = params if params && !params.empty? end&.body end error?(response) return response end
split_to_array(string)
click to toggle source
# File lib/revolut/api/base.rb, line 57 def split_to_array(string) string.include?(",") ? string.split(",") : [string] end
to_uri(path)
click to toggle source
# File lib/revolut/api/base.rb, line 20 def to_uri(path) "https://#{self.host}/#{path}" end