From 8e92e48e59465e66e747e026280a7389aa7ceee8 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Fri, 6 Dec 2013 06:22:46 +0400 Subject: [PATCH] first version --- .gitignore | 1 + example.rb | 12 ++++++++++++ lib/openexchangerates.rb | 31 +++++++++++++++++++++++++++++++ openexchangerates.gemspec | 14 ++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 .gitignore create mode 100644 example.rb create mode 100644 lib/openexchangerates.rb create mode 100644 openexchangerates.gemspec diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c111b33 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.gem diff --git a/example.rb b/example.rb new file mode 100644 index 0000000..504d813 --- /dev/null +++ b/example.rb @@ -0,0 +1,12 @@ +require 'json' +require 'time' +require 'openexchangerates' + +currency = OpenExchange.new('586ee899f7eb4f79861589a1f00a8630') +cur = JSON.parse(currency.get_latest) + +cur_show = 'RUB' + +puts "Latest update: #{Time.at(cur['timestamp'])}" +puts "1 USD = #{cur['rates'][cur_show]} \ +(#{JSON.parse(currency.get_currencies)[cur_show]})" diff --git a/lib/openexchangerates.rb b/lib/openexchangerates.rb new file mode 100644 index 0000000..78f9d0a --- /dev/null +++ b/lib/openexchangerates.rb @@ -0,0 +1,31 @@ +require 'open-uri' + +class OpenExchange + @@url = 'http://openexchangerates.org/api/' + + def initialize(api_key) + @api_key = api_key + @user_agent = "Ruby/#{RUBY_VERSION}" + end + + def set_userAgent(user_agent) + @user_agent = user_agent + end + + def get_latest() + json = open("#{@@url}latest.json?app_id=#{@api_key}", + 'User-Agent' => @user_agent).read + return json + end + + def get_old(date) # YYYY-MM-DD + json = open("#{@@url}historical/#{date}.json?app_id=#{@api_key}", + 'User-Agent' => @user_agent).read + end + + def get_currencies() + json = open("#{@@url}currencies.json?app_id=#{@api_key}", + 'User-Agent' => @user_agent).read + return json + end +end diff --git a/openexchangerates.gemspec b/openexchangerates.gemspec new file mode 100644 index 0000000..3daf46b --- /dev/null +++ b/openexchangerates.gemspec @@ -0,0 +1,14 @@ +Gem::Specification.new do |s| + s.name = 'openexchangerates' + s.version = '0.0.1' + s.date = '2012-12-06' + s.summary = 'Implementation API openexchangerates.org on Ruby' + s.description = 'Implementation API openexchangerates.org on Ruby' + s.authors = ['Alexander Popov'] + s.email = ['iiiypuk@iiiypuk.me'] + s.homepage = 'https://github.com/IIIypuk/openexchangerates' + s.license = 'MIT' + + s.files = `git ls-files`.split("\n") + s.require_paths = ["lib"] +end