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