module CoinMarketCap
Constants
- VERSION
Public Class Methods
connection()
click to toggle source
Build or get an HTTP connection object.
# File lib/coinmarketcap_api.rb, line 42 def connection @connection ||= Faraday.new(request: { timeout: CoinMarketCap.timeout }) end
connection=(connection)
click to toggle source
Set an HTTP connection object.
@param connection Connection object to be used.
# File lib/coinmarketcap_api.rb, line 49 def connection=(connection) @connection = connection end
request(type: 'ticker', options: {})
click to toggle source
Retrieve the request for a given type and options.
@param [String] type Type of request. Can be either `ticker` or `global`. @param [Object] options Optional parameters. Valid options are `:currency`, `:start`, `:limit` and `:convert`. @return [Array,Hash]
# 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
Private Class Methods
get(path, params = {})
click to toggle source
# File lib/coinmarketcap_api.rb, line 67 def get(path, params = {}) params = CoinMarketCap.default_params.merge(params || {}) connection.get(path, params) end
multiple_responses?(options, result, type)
click to toggle source
# File lib/coinmarketcap_api.rb, line 63 def multiple_responses?(options, result, type) result.is_a?(Array) && !options[:currency] && type != 'global' end
request_url(options, type)
click to toggle source
# File lib/coinmarketcap_api.rb, line 54 def request_url(options, type) query_options = options.reject { |k| k == :currency } result = "#{CoinMarketCap.api_endpoint}/#{type}/" result += "#{options[:currency]}/" if options[:currency] result += "?#{query_options.map { |k, v| "#{k}=#{v}" }.join('&')}" result end