goog_currency 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +36 -0
- data/Rakefile +1 -0
- data/goog_currency.gemspec +23 -0
- data/lib/goog_currency/version.rb +3 -0
- data/lib/goog_currency.rb +28 -0
- data/spec/goog_currency_spec.rb +42 -0
- metadata +119 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Girish S
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# GoogCurrency
|
2
|
+
|
3
|
+
A simple Ruby interface for currency conversion using Google API. Uses http://www.google.com/ig/calculator?hl=en&q=100USD=?INR
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'goog_currency'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install goog_currency
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Sample usage
|
22
|
+
|
23
|
+
usd = GoogCurrency.usd_to_inr(1)
|
24
|
+
gbp = GoogCurrency.usd_to_gbp(1)
|
25
|
+
|
26
|
+
Throws GoogCurrency::Exception in case of any error.
|
27
|
+
|
28
|
+
Throws GoogCurrency::NoMethodException if conversion method syntax is invalid.
|
29
|
+
|
30
|
+
## Contributing
|
31
|
+
|
32
|
+
1. Fork it
|
33
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
34
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
35
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
36
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'goog_currency/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "goog_currency"
|
8
|
+
gem.version = GoogCurrency::VERSION
|
9
|
+
gem.authors = ["Girish S"]
|
10
|
+
gem.email = ["girish.sonawane@gmail.com"]
|
11
|
+
gem.description = %q{Gem for currency conversion using Google API}
|
12
|
+
gem.summary = %q{Simple Ruby interface to Google Currency API}
|
13
|
+
gem.add_runtime_dependency 'rest-client', '~> 1.6.7'
|
14
|
+
gem.add_runtime_dependency 'json', '~> 1.7.5'
|
15
|
+
|
16
|
+
gem.add_development_dependency "rspec", "~> 2.12.0"
|
17
|
+
gem.add_development_dependency "fakeweb", "~> 1.3.0"
|
18
|
+
|
19
|
+
gem.files = `git ls-files`.split($/)
|
20
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
21
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
22
|
+
gem.require_paths = ["lib"]
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "goog_currency/version"
|
2
|
+
require 'rest_client'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module GoogCurrency
|
6
|
+
def self.method_missing(meth, *args)
|
7
|
+
from, to = meth.to_s.split("_to_")
|
8
|
+
|
9
|
+
if from.nil? or to.nil?
|
10
|
+
raise GoogCurrency::NoMethodException, "GoogCurrency accepts methods in 'usd_to_inr' or 'gbp_to_usd' format"
|
11
|
+
end
|
12
|
+
|
13
|
+
response = RestClient.get("http://www.google.com/ig/calculator?hl=en&q=#{args.first}#{from.upcase}=?#{to.upcase}").body
|
14
|
+
|
15
|
+
# response is not valid json
|
16
|
+
response.gsub!(/(lhs|rhs|error|icc)/, '"\1"')
|
17
|
+
response_hash = JSON.parse(response)
|
18
|
+
|
19
|
+
if response_hash['error'].nil? or response_hash['error'] == ''
|
20
|
+
response_hash['rhs'].to_f
|
21
|
+
else
|
22
|
+
raise GoogCurrency::Exception, "An error occurred: #{response_hash['error']}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Exception < StandardError; end
|
27
|
+
class NoMethodException < StandardError; end
|
28
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'fakeweb'
|
4
|
+
require 'goog_currency'
|
5
|
+
|
6
|
+
valid_response =<<-VALID
|
7
|
+
{lhs: "1 U.S. dollar",rhs: "54.836587 Indian rupees",error: "",icc: true}
|
8
|
+
VALID
|
9
|
+
|
10
|
+
invalid_response =<<-INVALID
|
11
|
+
{lhs: "",rhs: "",error: "4",icc: false}
|
12
|
+
INVALID
|
13
|
+
|
14
|
+
describe "GoogCurrency" do
|
15
|
+
describe "valid currencies" do
|
16
|
+
it "converts USD to INR" do
|
17
|
+
FakeWeb.register_uri(:get,
|
18
|
+
"http://www.google.com/ig/calculator?hl=en&q=1USD=?INR",
|
19
|
+
:status => "200",
|
20
|
+
:body => valid_response)
|
21
|
+
usd = GoogCurrency.usd_to_inr(1)
|
22
|
+
usd.should == 54.836587
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "invalid currencies" do
|
27
|
+
it "throws exception for USD to INX" do
|
28
|
+
FakeWeb.register_uri(:get,
|
29
|
+
"http://www.google.com/ig/calculator?hl=en&q=1USD=?INX",
|
30
|
+
:status => "200",
|
31
|
+
:body => invalid_response)
|
32
|
+
expect { GoogCurrency.usd_to_inx(1) }.to raise_error(GoogCurrency::Exception)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "invalid method" do
|
37
|
+
it "throws exception for invalid method" do
|
38
|
+
expect { GoogCurrency.usd_2_inr(1) }.to raise_error(GoogCurrency::NoMethodException)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: goog_currency
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Girish S
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rest-client
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.6.7
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.6.7
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: json
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 1.7.5
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.7.5
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.12.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.12.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: fakeweb
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.3.0
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.3.0
|
78
|
+
description: Gem for currency conversion using Google API
|
79
|
+
email:
|
80
|
+
- girish.sonawane@gmail.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- goog_currency.gemspec
|
91
|
+
- lib/goog_currency.rb
|
92
|
+
- lib/goog_currency/version.rb
|
93
|
+
- spec/goog_currency_spec.rb
|
94
|
+
homepage:
|
95
|
+
licenses: []
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 1.8.24
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: Simple Ruby interface to Google Currency API
|
118
|
+
test_files:
|
119
|
+
- spec/goog_currency_spec.rb
|