Class: Kaca
- Inherits:
-
Object
- Object
- Kaca
- Defined in:
- lib/kaca.rb
Instance Method Summary collapse
- #get(url) ⇒ Object
- #getir(url, ne, nekadar, islem) ⇒ Object
-
#initialize(opts = ARGV) ⇒ Kaca
constructor
A new instance of Kaca.
-
#is_number?(string) ⇒ Boolean
Ilk parametre string fakat numeric mi bakmak icin.
Constructor Details
#initialize(opts = ARGV) ⇒ 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/kaca.rb', line 6 def initialize(opts = ARGV) urls = { altin: 'http://www.doviz.com/api/v1/golds/all/latest', doviz: 'http://www.doviz.com/api/v1/currencies/all/latest', bitcoin: 'https://blockchain.info/tr/ticker' } if is_number? opts[0] ne = opts[1] nekadar = opts[0] islem = opts[2] else ne = opts[0] nekadar = nil islem = opts[1] end 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) bitcoin = %w(bitcoin coin) ne = FuzzyMatch.new(altinlar + birimler + bitcoin).find(ne.downcase) url = if altinlar.include? ne urls[:altin] elsif bitcoin.include? ne urls[:bitcoin] else urls[:doviz] end islem = if ['alabilirim', 'alırım', 'alirim', 'bulurum'].include? islem 'selling' else 'buying' end islem = islem[0..-4] if ne == 'bitcoin' getir(url, ne, nekadar, islem) rescue => e puts "Anlarken Errör olustu: #{e}" end |
Instance Method Details
#get(url) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/kaca.rb', line 68 def get url 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) return result end |
#getir(url, ne, nekadar, islem) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/kaca.rb', line 48 def getir(url, ne, nekadar, islem) result = get url if ne == 'bitcoin' aranan = 'bitcoin' dolar = get('http://www.doviz.com/api/v1/currencies/all/latest').select { |r| r['name'] == 'amerikan-dolari' }.first try = (result['USD'][islem] * dolar['selling']).to_f.round(2) else aranan = result.select { |r| r['name'] == ne }.first try = aranan[islem].to_f.round(2) aranan = aranan['full_name'].downcase end try = try * nekadar.to_i unless nekadar.nil? sonuc = "#{try} TRY e #{aranan} #{islem == 'buying' ? 'satabilirsin' : 'alabilirsin'}." puts sonuc rescue => e puts "Getirirken Errör olustu: #{e}" end |
#is_number?(string) ⇒ Boolean
Ilk parametre string fakat numeric mi bakmak icin
79 80 81 |
# File 'lib/kaca.rb', line 79 def is_number? string true if Float(string) rescue false end |