Class: Kaca
- Inherits:
-
Object
- Object
- Kaca
- Defined in:
- lib/kaca.rb
Instance Method Summary collapse
- #getir(url, ne, islem) ⇒ Object
-
#initialize(opts) ⇒ Kaca
constructor
A new instance of Kaca.
Constructor Details
#initialize(opts) ⇒ Kaca
Returns a new instance of Kaca.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/kaca.rb', line 6 def initialize(opts) urls = { altin: 'http://www.doviz.com/api/v1/golds/all/latest', doviz: 'http://www.doviz.com/api/v1/currencies/all/latest' } altinlar = %w(ceyrek-altin yarim-altin tam-altin cumhuriyet-altini ata-altin besli-altin gumus) birimler = %w(amerikan-dolari euro sterlin isvicre-frangi rus-rublesi japon-yeni) ne = FuzzyMatch.new(altinlar + birimler).find(opts[0].downcase) url = if altinlar.include? ne urls[:altin] else urls[:doviz] end islem = if FuzzyMatch.new(%w(almak alabilirim alırım)).find(opts[1]) 'selling' else 'buying' end getir(url, ne, islem) rescue => e puts "Errör: #{e}" end |
Instance Method Details
#getir(url, ne, islem) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/kaca.rb', line 32 def getir(url, ne, islem) uri = URI.parse(url) http = Net::HTTP.new(uri.host) req = Net::HTTP::Get.new(uri, 'Content-Type' => 'application/json') res = http.request(req) result = JSON.parse(res.body) aranan = result.select { |r| r['name'] == ne }.first sonuc = "Bugün #{aranan['full_name'].downcase!} #{aranan[islem].to_f.round(2)} TRY e #{islem == 'buying' ? 'satabilirsin' : 'alabilirsin'}." puts sonuc rescue => e puts "Errör: #{e}" end |