class Fixer::Client::HttpClient

Constants

BASE_URI
CONFIG_FILE
SCOPES

Public Class Methods

new(scope = :latest) click to toggle source
# File lib/fixer/client/http_client.rb, line 19
  def initialize(scope = :latest)
raise ::ArgumentError.new unless SCOPES.include?(scope)
@scope = scope
@config = read_config
@api_key = get_api_key
  end

Public Instance Methods

fetch(symbols = [], date = nil) click to toggle source
# File lib/fixer/client/http_client.rb, line 26
  def fetch(symbols = [], date = nil)
raise ::ArgumentError.new("You cannot put date argument for 'latest' mode client") if @scope == :latest && !date.nil? && !date.empty?
response_hash = hash(symbols)
raise Fixer::Client::Errors::InvalidResponse.new("Response: #{response_hash}") if response_hash["success"].to_s != "true"
response_hash
  end

Private Instance Methods

default_config() click to toggle source
# File lib/fixer/client/http_client.rb, line 57
def default_config
  hash = {}
  hash["api_key"] = 'invalid_key'
  hash["enabled_environments"] = ['production', 'development', 'test']
  hash
end
get_api_key() click to toggle source
# File lib/fixer/client/http_client.rb, line 64
def get_api_key
  return nil if Kernel.const_defined?("Rails") && !Rails.env.nil? && !@config["enabled_environments"].include?(Rails.env)
  return nil if @config.nil? 
  @config["api_key"]
end
hash(symbols) click to toggle source
# File lib/fixer/client/http_client.rb, line 35
def hash(symbols)
  ::JSON.parse(json(symbols))
end
json(symbols) click to toggle source
# File lib/fixer/client/http_client.rb, line 39
def json(symbols)
  Net::HTTP.get(url(symbols))
end
read_config() click to toggle source
# File lib/fixer/client/http_client.rb, line 53
def read_config
  File.exist?(CONFIG_FILE) ? YAML.load_file(CONFIG_FILE) : default_config
end
url(symbols) click to toggle source
# File lib/fixer/client/http_client.rb, line 43
def url(symbols)
  symbols_arg = symbols.join(",")
  case @scope
  when :latest
    URI("#{BASE_URI}/latest?access_key=#{@api_key}&symbols=#{symbols_arg}")
  when :historical
    raise ::ArgumentError.new("Not implemented yet!")
  end
end