Module: CoinMarketCap

Extended by:
Configuration
Defined in:
lib/coinmarketcap_api.rb,
lib/coinmarketcap_api/version.rb,
lib/coinmarketcap_api/configuration.rb

Defined Under Namespace

Modules: Configuration

Constant Summary collapse

VERSION =
'1.0.0'.freeze

Constants included from Configuration

Configuration::DEFAULT_API_ENDPOINT

Instance Attribute Summary

Attributes included from Configuration

#default_params, #timeout

Class Method Summary collapse

Methods included from Configuration

api_endpoint, configure

Class Method Details

.connectionObject

Build or get an HTTP connection object.



42
43
44
# File 'lib/coinmarketcap_api.rb', line 42

def connection
  @connection ||= Faraday.new(request: { timeout: CoinMarketCap.timeout })
end

.connection=(connection) ⇒ Object

Set an HTTP connection object.

Parameters:

  • connection

    Connection object to be used.



49
50
51
# File 'lib/coinmarketcap_api.rb', line 49

def connection=(connection)
  @connection = connection
end

.request(type: 'ticker', options: {}) ⇒ Array, Hash

Retrieve the request for a given type and options.

Can be either ‘ticker` or `global`. Valid options are `:currency`, `:start`, `:limit` and `:convert`.

Parameters:

  • type (String) (defaults to: 'ticker')

    Type of request.

  • options (Object) (defaults to: {})

    Optional parameters.

Returns:

  • (Array, Hash)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/coinmarketcap_api.rb', line 21

def request(type: 'ticker', options: {})
  request_response = get(request_url(options, type), options[:params])

  if request_response.success?
    # Rename api properties due to ruby restrictions.
    dictionary = { '1h' => 'hourly', '24h' => 'daily', '7d' => 'weekly' }
    dictionary.each { |k, v| request_response.body.gsub!(k, v) }

    result = MultiJson.load(request_response.body)

    if multiple_responses?(options, result, type)
      return result.map { |hash| Hashie::Mash.new(hash) }
    end

    result = result.first if result.length == 1

    Hashie::Mash.new(result)
  end
end