Class: Changer

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from_type, to_type) ⇒ Changer

Returns a new instance of Changer.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/currency.rb', line 9

def initialize(from_type, to_type)
  from_type = from_type.to_s.upcase
  to_type = to_type.to_s.upcase
  url = URI("https://currency-exchange.p.rapidapi.com/exchange?to=#{to_type}&from=#{from_type}&q=1.0")
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  
  request = Net::HTTP::Get.new(url)
  request["x-rapidapi-key"] = ENV['RAPID_API_KEY']
  request["x-rapidapi-host"] = 'currency-exchange.p.rapidapi.com'
  
  response = http.request(request)
  @rate = JSON.parse(response.read_body).to_f
end

Instance Attribute Details

#rateObject (readonly)

Returns the value of attribute rate.



7
8
9
# File 'lib/currency.rb', line 7

def rate
  @rate
end