apilayer 0.0.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7c74d7ef9158c6816618bfdefc24b46add14a17f
4
- data.tar.gz: 0925e5677d3797905b3904681c05c68da90b38f9
3
+ metadata.gz: 2c90421964d0e4f1e87b8dd237cc9fd9829828d4
4
+ data.tar.gz: 0d134f0f06cfa93e4c1aeb7bab7cebf2b48f4d4c
5
5
  SHA512:
6
- metadata.gz: 072fe6a9874a9c5bf297fddc8fd78d6f45d07e88aa97fd19fef7795ac24cdcc9350d8013a41afabe34bd12924073b6bb9729b35e8eb0a2e5cccd3a8fa157ce55
7
- data.tar.gz: 204aeaf2ee0049706999a04d31e516f420254ac54cf35333d91c6aa66bfda0a9fdd16f8bc17bdc27d213760aa2c6d23cfe1316d56ea457909a5763a414d23090
6
+ metadata.gz: cc9197e568e72415aca957bff9eebcdcac94be67a5e17c5b6d78822808c4828675eadbf1efb1726693fd309d1cbce578cc43746ef5905fce46924a90681cbdc3
7
+ data.tar.gz: 5559cc9b38fbdf4f34b92727b58c3d03f6b3222b5ade671a25708596e2f573a1a938c45786af2f77fc3a9871bfb1260a33f7f8bd62bd64b5204b51545fb122d0
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2016 Alex Fong http://www.github.com/actfong
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7
+ of the Software, and to permit persons to whom the Software is furnished to do
8
+ so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
File without changes
@@ -0,0 +1,5 @@
1
+ require 'rspec/core/rake_task'
2
+ task :default => :spec
3
+ desc 'Run test suite'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
@@ -16,7 +16,7 @@ module Apilayer
16
16
  @configs ||= init_configs
17
17
  end
18
18
 
19
- def self.reset
19
+ def self.reset!
20
20
  @configs = init_configs
21
21
  end
22
22
 
@@ -25,6 +25,7 @@ module Apilayer
25
25
  end
26
26
  end
27
27
 
28
+ require "apilayer/connection_helper"
28
29
  require "apilayer/currency"
29
30
  require "apilayer/vat"
30
- require "apilayer/error"
31
+ require "apilayer/error"
@@ -0,0 +1,39 @@
1
+ module Apilayer
2
+ module ConnectionHelper
3
+
4
+ def reset_connection
5
+ @connection = nil
6
+ end
7
+
8
+ def get_and_parse(url, params={})
9
+ resp = get_request(url, params)
10
+ parse_response(resp)
11
+ end
12
+
13
+ def get_request(url, params={})
14
+ # calls connection method on the extended module
15
+ connection.get do |req|
16
+ req.url "api/#{url}"
17
+ params.each_pair do |k,v|
18
+ req.params[k] = v
19
+ end
20
+ end
21
+ end
22
+
23
+ def parse_response(resp)
24
+ body = JSON.parse(resp.body)
25
+ # According to documentation, currencylayer has a "success" field
26
+ # while vatlayer has a "valid" field to indicate whether the request was succesful or not.
27
+ # However, for both layers, an unsuccesful request would contain an "error" field.
28
+ # That's why the presence of "error" is chosen to determine whether we should raise an error or not.
29
+ if body['error']
30
+ raise Apilayer::Error.new(
31
+ body['error']['info'],
32
+ body['error']['code']
33
+ )
34
+ else
35
+ body
36
+ end
37
+ end
38
+ end
39
+ end
@@ -1,42 +1,40 @@
1
1
  module Apilayer
2
-
3
2
  module Currency
3
+ extend ConnectionHelper
4
+
5
+ CURRENCYLAYER_KEY_MISSING_MSG = "Please configure access_key for currency_layer first!"
4
6
 
5
7
  def self.connection
6
- if Apilayer.configs[:currency_key].nil?
7
- raise Apilayer::Error.new "Please configure access_key for currency_layer first!"
8
- else
9
- @connection ||= ::Faraday.new(:url => 'http://apilayer.net',
10
- :params => {"access_key" => Apilayer.configs[:currency_key]})
11
- end
8
+ @connection ||= ::Faraday.new(:url => 'http://apilayer.net',
9
+ :params => {"access_key" => Apilayer.configs[:currency_key]})
12
10
  end
13
11
 
14
12
  def self.live(*currencies)
15
- resp = connection.get do |req|
16
- req.url 'api/live'
17
- req.params['currencies'] = currencies.map(&:strip).join(",")
13
+ if currencies.any?
14
+ currencies_str = join_by_commas(currencies)
15
+ params = {:currencies => currencies_str}
16
+ get_and_parse("live", params)
17
+ else
18
+ get_and_parse("live")
18
19
  end
19
- JSON.parse(resp.body)
20
20
  end
21
21
 
22
- def self.historical(date, *currencies)
23
- resp = connection.get do |req|
24
- req.url 'api/historical'
25
- req.params['date'] = date
26
- req.params['currencies'] = currencies.map(&:strip).join(",") if currencies.any?
27
- end
28
- JSON.parse(resp.body)
22
+ def self.historical(date, *currencies)
23
+ params = {:date => date}
24
+ params.merge!(:currencies => join_by_commas(currencies)) if currencies.any?
25
+ get_and_parse("historical", params)
29
26
  end
30
27
 
28
+ =begin
31
29
  def self.convert(from, to, amount, date=nil)
32
- resp = connection.get do |req|
33
- req.url 'api/convert' # static
34
- req.params['from'] = from
35
- req.params['to'] = to
36
- req.params['amount'] = amount
37
- req.params['date'] = date if date
38
- end
39
- JSON.parse(resp.body)
30
+ params = {:from => from, :to => to, :amount => amount}
31
+ params.merge!(:date => date) if date
32
+ get_and_parse("convert", params)
40
33
  end
34
+ =end
35
+ def self.join_by_commas(currencies)
36
+ currencies.map(&:strip).join(",")
37
+ end
38
+
41
39
  end
42
40
  end
@@ -0,0 +1,10 @@
1
+ module Apilayer
2
+ class Error < StandardError
3
+ attr_reader :code
4
+
5
+ def initialize(message, code=nil)
6
+ super(message)
7
+ @code = code
8
+ end
9
+ end
10
+ end
@@ -1,51 +1,41 @@
1
1
  module Apilayer
2
-
3
2
  module Vat
3
+ extend ConnectionHelper
4
+
5
+ COUNTRY_CRITERIA_MISSING_MSG = "You must provide either :country_code or :ip_address"
6
+ VATLAYER_KEY_MISSING_MSG = "Please configure access_key for vat_layer first!"
7
+
4
8
  def self.connection
5
- if Apilayer.configs[:vat_key].nil?
6
- raise Apilayer::Error.new "Please configure access_key for vat_layer first!"
7
- else
8
- @connection ||= ::Faraday.new(:url => 'http://apilayer.net',
9
- :params => {"access_key" => Apilayer.configs[:vat_key]})
9
+ @connection ||= ::Faraday.new(:url => 'http://apilayer.net',
10
+ :params => {"access_key" => Apilayer.configs[:vat_key]})
11
+ end
12
+
13
+ def self.validate_country_criteria(criteria)
14
+ unless [:country_code, :ip_address].include? criteria
15
+ raise Apilayer::Error.new COUNTRY_CRITERIA_MISSING_MSG
10
16
  end
11
17
  end
12
18
 
13
19
  def self.validate(vat_number)
14
- resp = connection.get do |req|
15
- req.url 'api/validate'
16
- req.params['vat_number'] = vat_number
17
- end
18
- JSON.parse(resp.body)
20
+ params = {:vat_number => vat_number}
21
+ get_and_parse("validate", params)
19
22
  end
20
23
 
21
24
  def self.rate(criteria, value)
22
- unless [:country_code, :ip_address].include? criteria
23
- raise Apilayer::Error.new("You must provide either :country_code or :ip_address")
24
- end
25
- resp = connection.get do |req|
26
- req.url 'api/rate'
27
- req.params[criteria.to_s] = value
28
- end
29
- JSON.parse(resp.body)
25
+ validate_country_criteria(criteria)
26
+ params = {criteria.to_sym => value}
27
+ get_and_parse("rate", params)
30
28
  end
31
29
 
32
30
  def self.rate_list
33
- resp = connection.get do |req|
34
- req.url 'api/rate_list'
35
- end
36
- JSON.parse(resp.body)
31
+ get_and_parse("rate_list")
37
32
  end
38
33
 
39
34
  def self.price(price, criteria, value)
40
- unless [:country_code, :ip_address].include? criteria
41
- raise Apilayer::Error.new("You must provide either :country_code or :ip_address")
42
- end
43
- resp = connection.get do |req|
44
- req.url 'api/price'
45
- req.params['amount'] = price
46
- req.params[criteria.to_s] = value
47
- end
48
- JSON.parse(resp.body)
35
+ validate_country_criteria(criteria)
36
+ params = {:amount => price, criteria.to_sym => value}
37
+ get_and_parse("price", params)
49
38
  end
39
+
50
40
  end
51
41
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apilayer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Fong
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-09 00:00:00.000000000 Z
11
+ date: 2016-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -158,8 +158,14 @@ executables: []
158
158
  extensions: []
159
159
  extra_rdoc_files: []
160
160
  files:
161
+ - Gemfile
162
+ - LICENSE
163
+ - README.rdoc
164
+ - Rakefile
161
165
  - lib/apilayer.rb
166
+ - lib/apilayer/connection_helper.rb
162
167
  - lib/apilayer/currency.rb
168
+ - lib/apilayer/error.rb
163
169
  - lib/apilayer/vat.rb
164
170
  homepage: https://github.com/actfong/apilayer
165
171
  licenses: