Sha256: 10cb9322fb6a4db5c49696f99b70969d965c4a4343f03e31171a000903caae70

Contents?: true

Size: 1.38 KB

Versions: 3

Compression:

Stored size: 1.38 KB

Contents

require "cryptocoincharts_api/version"
require "cryptocoincharts_api/api"

require "faraday"
require "json"

module CryptocoinchartsApi

  class Client
    include CryptocoinchartsApi::API

    def initialize; end

    def self.debug
      @debug ||= false
    end

    def self.debug=(v)
      @debug = !!v
    end

    API_VERSION = 'v2'
    BASE_URL = "http://www.cryptocoincharts.info/#{API_VERSION}/api/"
  
    private
  
    def api_call(method_name, options, verb=:get)
      response = connection(method_name, options, verb)
      puts response.inspect if self.class.debug
      parse_response response
    end
  
    def parse_response(response)
      if response.status.to_i == 200
        JSON.parse response.body, symbolize_names: true
      else
        raise response.status.to_s
      end
    end
  
    def connection(method_name, options, verb)
      conn = Faraday.new(:url => BASE_URL) do |faraday|
        faraday.request  :url_encoded
        faraday.response(:logger) if self.class.debug
        faraday.adapter  Faraday.default_adapter
        faraday.headers['User-Agent'] = "CryptocoinchartsAPI rubygem v#{VERSION}"
      end
  
      case verb
        when :put then conn.put(method_name, options)
        when :post then conn.post(method_name, options)
        when :delete then conn.delete(method_name, options)
        else conn.get(method_name, options)
      end
    end
  
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cryptocoincharts_api-0.0.4 lib/cryptocoincharts_api.rb
cryptocoincharts_api-0.0.3 lib/cryptocoincharts_api.rb
cryptocoincharts_api-0.0.2 lib/cryptocoincharts_api.rb