Sha256: c2dee5727fb6a8194a1ce3c57eb62cd38be47836647d1edf4c1970007e956ba4

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require 'uri'
require 'net/http'
require 'openssl'
require 'json'
require 'date'

module CoinmarketcapFree
  # Helper module
  module Helper
    BASE_URI = 'https://api.coinmarketcap.com/data-api/'
    VERSION_API = 'v3'

    URI_API_CRYPTOCURRENCIES = "#{BASE_URI}#{VERSION_API}/cryptocurrency/listing"
    URI_API_CRYPTOCURRENCY_HISTORIES = "#{BASE_URI}#{VERSION_API}/cryptocurrency/detail/chart"

    class << self
      # Get cryptocurrency of histories
      # @param data [Hash] Parameters for Coinmarketcap API
      # @return [Hash] Return coin histories
      def get_cryptocurrency_histories(data)
        str = http_get(URI_API_CRYPTOCURRENCY_HISTORIES, data)
        JSON.parse(str)
      end

      # Get cryptocurrencies
      # @param data [Hash] Parameters for Coinmarketcap API
      # @return [Hash] Return cryptocurrencies
      def get_cryptocarencies(data)
        str = http_get(URI_API_CRYPTOCURRENCIES, data)
        JSON.parse(str)
      end

      private

      def http_get(url, data = nil)
        uri = URI(generate_url(url, data))
        Net::HTTP.get(uri)
      end

      def generate_url(url, data = nil)
        "#{url}#{data ? '?' : ''}#{data.map { |key, value| "#{key}=#{value}" }.join('&')}"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
coinmarketcap_free-1.0.1 lib/coinmarketcap_free/helper.rb