Class: Revolut::Api::Base

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/revolut/api/base.rb

Direct Known Subclasses

Client, Public::Client

Constant Summary

Constants included from Errors

Errors::MAPPING

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Errors

#error?

Constructor Details

#initialize(host: "api.revolut.com", configuration: ::Revolut::Api.configuration) ⇒ Base

Returns a new instance of Base.



6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



4
5
6
# File 'lib/revolut/api/base.rb', line 4

def configuration
  @configuration
end

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/revolut/api/base.rb', line 4

def headers
  @headers
end

#hostObject

Returns the value of attribute host.



4
5
6
# File 'lib/revolut/api/base.rb', line 4

def host
  @host
end

#memoizedObject

Returns the value of attribute memoized.



4
5
6
# File 'lib/revolut/api/base.rb', line 4

def memoized
  @memoized
end

Instance Method Details

#authable?Boolean

Returns:

  • (Boolean)


114
115
116
# 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!Object



24
25
26
27
28
# 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: {}) ⇒ Object



61
62
63
# File 'lib/revolut/api/base.rb', line 61

def get(path, params: {}, options: {})
  request path, method: :get, params: params, options: options
end

#log(message) ⇒ Object



118
119
120
# 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: {}) ⇒ Object



69
70
71
# 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: {}) ⇒ Object



65
66
67
# 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: {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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: {}) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object



57
58
59
# File 'lib/revolut/api/base.rb', line 57

def split_to_array(string)
  string.include?(",") ? string.split(",") : [string]
end

#to_uri(path) ⇒ Object



20
21
22
# File 'lib/revolut/api/base.rb', line 20

def to_uri(path)
  "https://#{self.host}/#{path}"
end