Class: Cryptowatch::RestClientAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/cryptowatch/adapter/rest_client_adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RestClientAdapter

Returns a new instance of RestClientAdapter.



17
18
19
# File 'lib/cryptowatch/adapter/rest_client_adapter.rb', line 17

def initialize (options)
  @options = options
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/cryptowatch/adapter/rest_client_adapter.rb', line 15

def options
  @options
end

Instance Method Details

#get(*elements) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cryptowatch/adapter/rest_client_adapter.rb', line 21

def get (*elements)

  if (elements.size == 0)
     Messages::LOG.error(Messages::ERROR_NO_URL)
    return nil
  end

  url = Api::format_url(*elements)

  begin
    milli_start = Time.now
    answer = JSON.parse(RestClient::Request.execute(
      method: :get,
      url: url,
      timeout: @options[:timeout]),
      accept: :json
    )
    milli_end = Time.now
    elapsed   = Utils::time_elapsed(milli_start, milli_end)
    Messages::LOG.info("[#{elapsed.round} ms] #{Messages::GET}'#{url}'")
    return answer
  rescue SocketError, RestClient::ExceptionWithResponse => e
    Messages::LOG.error("'#{e.message}' on => #{url}")
  end
end