ccy_convertor 0.0.2 → 1.0.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: 6cd5bc29c2df00a8629ba57024c388232494f90c
4
- data.tar.gz: a4ef3c9b93ae1f0ce86987f110f964f81a610dbd
3
+ metadata.gz: ea00cf636ccf956c5e6ef07a8e0187cc8417ff7e
4
+ data.tar.gz: acad21629105b1e49f93053f106c13a83ed79c0b
5
5
  SHA512:
6
- metadata.gz: 96976f7e8d1aa9f0e9acbe5251a631a43197d11ea058d660ec252f2298767b4a1a7ce063bc6d569fbd4692e0a8677f329d36d0cbc00a87695757aa0c852ce322
7
- data.tar.gz: bea4e021e2bacef892d284c63321ab6d75fdec76913bba22695e155f14e3ffbd7343069d8aa9646d7118a54d42fd7cb9bad268e4d2cb488f14f34fb363a66ad4
6
+ metadata.gz: d3cf80f94e9cffb418c35363c5ef7248fd2c5900e50ef42b2e6e9e3b65a3b77ad1e0a53b750f6a322b345a67847e5b361dd65ee2233309e0824cd1febcedb6ed
7
+ data.tar.gz: 756ee7a4664491c997a0f4f66d7d4be84824cbe454489993b8aadc68ab3092ff828d57ce23fd5bdbe9781dd3889a07801f5f6330f2c684ab9af17a3b0a51d7c9
data/README.md CHANGED
@@ -57,13 +57,11 @@ You can convert money from one currency to another in following ways
57
57
  Below code will use rate provided from www.openexchangerates.org
58
58
  ```ruby
59
59
  CcyConvertor::OpenExchangeRate.convert(from_ccy: 'USD', to_ccy: 'NZD', amount: 10)
60
- 10.usd.to_nzd(rate_provider: Ccyconvertor::OpenExchangeRate)
61
60
  ```
62
61
 
63
62
  Below code will use default rate provider. Default rate provider is configurable . Check [configuration section](#configuration)
64
63
  ```ruby
65
64
  CcyConvertor.convert(from_ccy: 'USD', to_ccy: 'NZD', amount: 10)
66
- 10.usd.to_nzd
67
65
  ```
68
66
  You can also get all currency rate (rate matrix) with respect to USD in single request for OpenExchangeRate and CurrencyLayer rate provider
69
67
  ```ruby
@@ -77,7 +75,7 @@ While For Ccyconvertor::CurrencyLayer we can specify the base currency
77
75
  Ccyconvertor::CurrencyLayer.rate_matrix('NZD')
78
76
  ```
79
77
  All rates returned would be with respect to NZD. If no parameter is given then base currency is USD by default
80
-
78
+
81
79
  Note:
82
80
  You cannot provide base currency for free account at currencylayer.com. By defualt base currency will be USD. You need a paid account at currencylayer.com to supply base currency. And Ccyconvertor::YahooFinance do not support rate_matrix method
83
81
 
@@ -22,5 +22,4 @@ Gem::Specification.new do |spec|
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
 
24
24
  spec.add_runtime_dependency "activesupport", ">= 3.0"
25
- spec.add_runtime_dependency "httparty"
26
25
  end
data/lib/ccy_convertor.rb CHANGED
@@ -1,13 +1,10 @@
1
1
  require 'active_support/core_ext/string/inflections'
2
2
  require 'active_support/cache.rb'
3
- require 'httparty'
4
3
  require 'digest/sha1'
5
4
 
6
5
  require 'ccy_convertor/version'
7
6
  require 'ccy_convertor/configuration'
8
7
  require 'ccy_convertor/constant'
9
- require 'ccy_convertor/extension'
10
- require 'ccy_convertor/money'
11
8
  require 'ccy_convertor/exception'
12
9
  require 'ccy_convertor/rate_cache'
13
10
  require 'ccy_convertor/process_and_validate_option'
@@ -19,7 +16,6 @@ require 'ccy_convertor/rate_providers/currency_layer'
19
16
  module CcyConvertor
20
17
  class << self
21
18
  def rate(options)
22
- RateProvider.process_options!(options)
23
19
  RateProvider.validate_presence_of_hash_keys!(options, [:from_ccy, :to_ccy])
24
20
  rate = rate_provider(options).rate(options[:to_ccy], options[:from_ccy])
25
21
  return rate if CcyConvertor.configuration.round_up_rate.nil?
@@ -6,4 +6,6 @@ module CcyConvertor
6
6
  class InvalidCurrencyCode < StandardError; end
7
7
 
8
8
  class CurrencyNotSupported < StandardError; end
9
- end
9
+
10
+ class ResponseInvalid < StandardError; end
11
+ end
@@ -1,11 +1,5 @@
1
1
  module CcyConvertor
2
2
  module ProcessAndValidateOption
3
- def process_options!(options)
4
- unless options[:money].nil?
5
- options.merge!(from_ccy: options[:money].code, amount: options[:money].amount)
6
- end
7
- end
8
-
9
3
  def validate_options!(options)
10
4
  validate_presence_of_hash_keys!(options, [:from_ccy, :to_ccy, :amount])
11
5
  validate_amount!(options[:amount])
@@ -42,4 +36,4 @@ module CcyConvertor
42
36
  end
43
37
  end
44
38
  end
45
- end
39
+ end
@@ -6,8 +6,13 @@ module CcyConvertor
6
6
  end
7
7
 
8
8
  def rate_matrix(base_ccy = nil)
9
- rates = rate_matrix_response(base_ccy)['quotes']
10
- rates.inject({}) { |acc, (k, v)| acc[k[3, 3]] = v; acc }
9
+ rate_matrix_response = rate_matrix_response(base_ccy)
10
+ if error = rate_matrix_response['error']
11
+ raise CcyConvertor::ResponseInvalid, error['info']
12
+ end
13
+ rate_matrix_response['quotes'].inject({}) do |acc, (k, v)|
14
+ acc[k[3, 3]] = v; acc
15
+ end
11
16
  end
12
17
  end
13
18
  end
@@ -6,8 +6,12 @@ module CcyConvertor
6
6
  end
7
7
 
8
8
  def rate_matrix
9
+ rate_matrix_response = rate_matrix_response()
10
+ if rate_matrix_response['error']
11
+ raise CcyConvertor::ResponseInvalid, rate_matrix_response['description']
12
+ end
9
13
  rate_matrix_response['rates']
10
14
  end
11
15
  end
12
16
  end
13
- end
17
+ end
@@ -1,3 +1,6 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
1
4
  module CcyConvertor
2
5
  class RateProvider
3
6
  extend CcyConvertor::RateCache
@@ -26,7 +29,8 @@ module CcyConvertor
26
29
  def response_hash(request_url)
27
30
  cache_key = cache_key(request_url)
28
31
  return cache.read(cache_key) if cache.exist?(cache_key)
29
- response = HTTParty.get(request_url).parsed_response
32
+
33
+ response = JSON.parse(Net::HTTP.get(URI(request_url)))
30
34
  cache.write(cache_key, response, expires_in: cache_duration)
31
35
  response
32
36
  end
@@ -47,7 +51,6 @@ module CcyConvertor
47
51
  alias :rate :rate_from_rate_matrix
48
52
 
49
53
  def convert(options)
50
- process_options!(options)
51
54
  validate_options!(options)
52
55
  converted_rate = options[:amount] * rate(options[:to_ccy], options[:from_ccy])
53
56
  return converted_rate if CcyConvertor.configuration.round_up_amount.nil?
@@ -55,4 +58,4 @@ module CcyConvertor
55
58
  end
56
59
  end
57
60
  end
58
- end
61
+ end
@@ -1,3 +1,3 @@
1
1
  module CcyConvertor
2
- VERSION = '0.0.2'
2
+ VERSION = '1.0.0'
3
3
  end
metadata CHANGED
@@ -1,71 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ccy_convertor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rohan Pujari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-04 00:00:00.000000000 Z
11
+ date: 2016-06-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.6'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activesupport
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '3.0'
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
- - !ruby/object:Gem::Dependency
56
- name: httparty
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - '>='
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
55
  description: CcyConvertor provides live currency rate for various currencies and allows
70
56
  to convert money from one currency to other. Currently it supports 3 rate provider.
71
57
  New rate provider can be easily plugged in
@@ -75,7 +61,7 @@ executables: []
75
61
  extensions: []
76
62
  extra_rdoc_files: []
77
63
  files:
78
- - .gitignore
64
+ - ".gitignore"
79
65
  - Gemfile
80
66
  - LICENSE.txt
81
67
  - README.md
@@ -85,8 +71,6 @@ files:
85
71
  - lib/ccy_convertor/configuration.rb
86
72
  - lib/ccy_convertor/constant.rb
87
73
  - lib/ccy_convertor/exception.rb
88
- - lib/ccy_convertor/extension.rb
89
- - lib/ccy_convertor/money.rb
90
74
  - lib/ccy_convertor/process_and_validate_option.rb
91
75
  - lib/ccy_convertor/rate_cache.rb
92
76
  - lib/ccy_convertor/rate_providers/currency_layer.rb
@@ -104,12 +88,12 @@ require_paths:
104
88
  - lib
105
89
  required_ruby_version: !ruby/object:Gem::Requirement
106
90
  requirements:
107
- - - '>='
91
+ - - ">="
108
92
  - !ruby/object:Gem::Version
109
93
  version: '0'
110
94
  required_rubygems_version: !ruby/object:Gem::Requirement
111
95
  requirements:
112
- - - '>='
96
+ - - ">="
113
97
  - !ruby/object:Gem::Version
114
98
  version: '0'
115
99
  requirements: []
@@ -120,4 +104,3 @@ specification_version: 4
120
104
  summary: CcyConvertor provides live currency rate for various currencies and allows
121
105
  to convert money from one currency to other
122
106
  test_files: []
123
- has_rdoc:
@@ -1,5 +0,0 @@
1
- class Numeric
2
- CcyConvertor::Constant::CCY_CODE_AND_NAME.each do |code, name|
3
- define_method(code.downcase) { CcyConvertor::Money.new(code, self) }
4
- end
5
- end
@@ -1,21 +0,0 @@
1
- module CcyConvertor
2
- class Money
3
- attr_accessor :code, :name, :amount
4
-
5
- def initialize(code, amount)
6
- @code = code
7
- @amount = amount
8
- end
9
-
10
- def name
11
- CcyConvertor::Constant::CCY_CODE_AND_NAME[code]
12
- end
13
-
14
- CcyConvertor::Constant::CCY_CODE_AND_NAME.each do |code, name|
15
- define_method("to_#{code.downcase}") do |options = {}|
16
- options.merge!(money: self, to_ccy: code)
17
- CcyConvertor.convert(options)
18
- end
19
- end
20
- end
21
- end