Class: Currencyapi::Endpoints

Inherits:
Object
  • Object
show all
Defined in:
lib/currencyapi/endpoints.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Endpoints

Returns a new instance of Endpoints.



6
7
8
# File 'lib/currencyapi/endpoints.rb', line 6

def initialize(options = {})
  @apikey = options[:apikey] || Currencyapi.configuration.apikey
end

Instance Attribute Details

#apikey=(value) ⇒ Object (writeonly)

Sets the attribute apikey

Parameters:

  • value

    the value to set the attribute apikey to.



4
5
6
# File 'lib/currencyapi/endpoints.rb', line 4

def apikey=(value)
  @apikey = value
end

Instance Method Details

#call_api(route) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/currencyapi/endpoints.rb', line 34

def call_api(route)
  begin
    @response = RestClient.get "#{Currencyapi::BASE_URL}#{route}&apikey=#{@apikey}", { 'Accept' => 'application/json' }
  rescue RestClient::ExceptionWithResponse => e
    @data = e.response
  end
end

#convert(value, date = '', baseCurrency = 'USD', currencies = '') ⇒ Object



30
31
32
# File 'lib/currencyapi/endpoints.rb', line 30

def convert(value, date = '', baseCurrency = 'USD', currencies = '')
  @result = call_api(sprintf('convert?value=%s&date=%s&base_currency=%s&currencies=%s', value, date, baseCurrency, currencies))
end

#currencies(currencies = '') ⇒ Object



14
15
16
# File 'lib/currencyapi/endpoints.rb', line 14

def currencies(currencies = '')
  @result = call_api(sprintf('currencies?currencies=%s', currencies))
end

#historical(date, baseCurrency = 'USD', currencies = '') ⇒ Object



22
23
24
# File 'lib/currencyapi/endpoints.rb', line 22

def historical(date, baseCurrency = 'USD', currencies = '')
  @result = call_api(sprintf('historical?date=%s&base_currency=%s&currencies=%s', date, baseCurrency, currencies))
end

#latest(baseCurrency = 'USD', currencies = '') ⇒ Object



18
19
20
# File 'lib/currencyapi/endpoints.rb', line 18

def latest(baseCurrency = 'USD', currencies = '')
  @result = call_api(sprintf('latest?base_currency=%s&currencies=%s', baseCurrency, currencies))
end

#range(datetime_start, datetime_end, accuracy = 'day', baseCurrency = 'USD', currencies = '') ⇒ Object



26
27
28
# File 'lib/currencyapi/endpoints.rb', line 26

def range(datetime_start, datetime_end, accuracy = 'day', baseCurrency = 'USD', currencies = '')
  @result = call_api(sprintf('range?datetime_start=%s&datetime_end=%s&accuracy=%s&base_currency=%s&currencies=%s', datetime_start, datetime_end, accuracy, baseCurrency, currencies))
end

#statusObject



10
11
12
# File 'lib/currencyapi/endpoints.rb', line 10

def status()
  @result = call_api('status?type=quota')
end