oxr 0.1.0.1 → 0.2.0

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.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +2 -0
  3. data/README.md +52 -1
  4. data/lib/oxr.rb +55 -18
  5. data/lib/oxr/version.rb +1 -1
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 644b32c3a84f34023d8e1809d3040c7d197e7b7c
4
- data.tar.gz: 2863c2d71e004965e2950b507279dc26e8cecef0
3
+ metadata.gz: 61911998061bee0501d6db3d1fe3f3f00dcc7c9b
4
+ data.tar.gz: 4afda8ff15cf6b26a1728d3ae1b50d2b270305cb
5
5
  SHA512:
6
- metadata.gz: 3245f994b6181e2bac92d5a652197661d9d72e654f32ee486e47c01e2f08cd738ad2bed058d4c5ae96dddb48f13ce6be9410966508aa88703e45c5e13fd5efee
7
- data.tar.gz: 862bbba898c8f93e2ed56951e0ed9a327c6b74758295bd4c2f504f572d62161b2836facb37cc903624f7191fc8d24b71db547d965e65f24f115624e47b6953d6
6
+ metadata.gz: 8798474d8dbb3e53954857a2c7c56515a92eacc0674b5cdad083591927fb2794728f470a91bbb6ebe40db872b16499880d0b72e80d19c54cf41bdf8943ca76ff
7
+ data.tar.gz: 5108175cb8ac1ee4d79b4ecbd7b2c207c7f616b57ced79e7bcb60a85d1181e1e402cb7085b3243c00b40c974b409270b5d443240ba93c88536c6a11b9305fe64
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.8
4
+ - 2.2.4
3
5
  - 2.3.0
4
6
  before_install: gem install bundler -v 1.11.2
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ # [![Gem Version](https://badge.fury.io/rb/oxr.svg)](https://badge.fury.io/rb/oxr) [![Build Status](https://travis-ci.org/jparker/oxr.svg?branch=master)](https://travis-ci.org/jparker/oxr)
2
+
1
3
  # OXR
2
4
 
3
5
  This gem provides a basic interface to the [Open Exchange Rates](https://openexchangerates.org) API.
@@ -28,7 +30,7 @@ Instantiate a new OXR object, passing your App ID as an argument.
28
30
  oxr = OXR.new 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
29
31
  ```
30
32
 
31
- The Open Exchange Rates API returns results as JSON objects. OXR parses these (using the [json](https://rubygems.org/gems/json)) and returns the resulting Hashes. To see the exact structure of the responses for different queries, check the [Open Exchange Rates documentation](https://docs.openexchangerates.org/) or examine the sample responses in `test/fixtures`.
33
+ The Open Exchange Rates API returns results as JSON objects. OXR parses these (using the [json](https://rubygems.org/gems/json) gem) and returns the resulting Hashes. To see the exact structure of the responses for different queries, check the [Open Exchange Rates documentation](https://docs.openexchangerates.org/) or examine the sample responses in `test/fixtures`.
32
34
 
33
35
  Get the latest conversion rates with `OXR#latest`.
34
36
 
@@ -36,12 +38,38 @@ Get the latest conversion rates with `OXR#latest`.
36
38
  oxr.latest
37
39
  ```
38
40
 
41
+ This will return a JSON object with a structure similar to the following:
42
+
43
+ ```json
44
+ {
45
+ "disclaimer": "...",
46
+ "license": "...",
47
+ "timestamp": 1234567890,
48
+ "base": "USD",
49
+ "rates": {
50
+ "AED": 3.672995,
51
+ "AFN": 68.360001,
52
+ "ALL": 123.0332,
53
+ /* ... */
54
+ }
55
+ }
56
+ ```
57
+
58
+ `OXR#[]` is a shortcut for looking up the conversion rate for a single currency without digging through the JSON object returned by `OXR#latest` yourself.
59
+
60
+ ```ruby
61
+ oxr['GBP'] # => 0.642607
62
+ oxr['JPY'] # => 123.3267
63
+ ```
64
+
39
65
  Get historical conversion rates for specific dates with `OXR#historical`. This method requires you to provide a Date object for the date you wish to query.
40
66
 
41
67
  ```ruby
42
68
  oxr.historical on: Date.new(2016, 3, 24)
43
69
  ```
44
70
 
71
+ This will return a JSON object with a structure similar to that returned by `OXR#latest`.
72
+
45
73
  Get a list of currently supported currencies with `OXR#currencies`.
46
74
 
47
75
  ```ruby
@@ -54,6 +82,29 @@ Get information about your account (including your usage for the current period)
54
82
  oxr.usage
55
83
  ```
56
84
 
85
+ ## Testing
86
+
87
+ Normally, any API call will result in a live request to Open Exchange Rates. This probably isn't what you want when you're running tests. You can optionally stub the responses of specific API calls by adding an entry to `OXR.sources`. If you want to stop using custom sources, you can restore normal behavior by calling `OXR.reset_sources`. For example:
88
+
89
+ ```ruby
90
+ class SomeTest < Minitest::Test
91
+ def setup
92
+ OXR.sources[:latest] = 'test/fixtures/sample_data.json'
93
+ end
94
+
95
+ def teardown
96
+ OXR.reset_sources
97
+ end
98
+
99
+ def test_something
100
+ oxr = OXR.new('XXX')
101
+ assert_equal 42, oxr.latest['rates']['GBP']
102
+ end
103
+ end
104
+ ```
105
+
106
+ In this example `test/fixtures/sample_data.json` should contain JSON data matching the structure of an actual Open Exchange Rates response. If you want to stop using custom sources, you can restore normal behavior by calling `OXR.reset_sources`.
107
+
57
108
  ## Development
58
109
 
59
110
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/oxr.rb CHANGED
@@ -7,38 +7,75 @@ require 'open-uri'
7
7
  class OXR
8
8
  BASE_PATH = 'https://openexchangerates.org/api/'.freeze
9
9
 
10
+ class OXRError < StandardError
11
+ def initialize(message, response)
12
+ super message
13
+ @response = response
14
+ end
15
+
16
+ attr_reader :response
17
+ end
18
+
10
19
  def initialize(app_id)
11
20
  @app_id = app_id
12
21
  end
13
22
 
14
23
  attr_reader :app_id
15
24
 
16
- def latest(only: nil)
17
- endpoint = URI.join BASE_PATH, 'latest.json'
18
- endpoint.query = "app_id=#{app_id}"
19
- # Only allowed for paid plans
20
- endpoint.query += "&symbols=#{Array(only).join ','}" if only
21
- JSON.load open endpoint
25
+ def [](code)
26
+ latest['rates'][code]
22
27
  end
23
28
 
24
- def historical(on:, only: nil)
25
- date = on.strftime '%Y-%m-%d'
26
- endpoint = URI.join BASE_PATH, 'historical/', "#{date}.json"
27
- endpoint.query = "app_id=#{app_id}"
28
- # Only allowed for paid plans
29
- endpoint.query += "&symbols=#{Array(only).join ','}" if only
30
- JSON.load open endpoint
29
+ def latest
30
+ endpoint = sources[:latest] || build_uri_endpoint('latest.json')
31
+ call endpoint
32
+ end
33
+
34
+ def historical(on:)
35
+ endpoint = sources[:historical] || \
36
+ build_uri_endpoint('historical/', "#{on.strftime '%Y-%m-%d'}.json")
37
+ call endpoint
31
38
  end
32
39
 
33
40
  def currencies
34
- endpoint = URI.join BASE_PATH, 'currencies.json'
35
- endpoint.query = "app_id=#{app_id}"
36
- JSON.load open endpoint
41
+ endpoint = sources[:currencies] || build_uri_endpoint('currencies.json')
42
+ call endpoint
37
43
  end
38
44
 
39
45
  def usage
40
- endpoint = URI.join BASE_PATH, 'usage.json'
41
- endpoint.query = "app_id=#{app_id}"
46
+ endpoint= sources[:usage] || build_uri_endpoint('usage.json')
47
+ call endpoint
48
+ end
49
+
50
+ private
51
+
52
+ def build_uri_endpoint(*path, **params)
53
+ URI.join(BASE_PATH, *path).tap do |uri|
54
+ uri.query = "app_id=#{app_id}"
55
+ end
56
+ end
57
+
58
+ def call(endpoint)
42
59
  JSON.load open endpoint
60
+ rescue OpenURI::HTTPError => e
61
+ case e.message
62
+ when /\A4[[:digit:]]{2}/
63
+ response = JSON.load e.io
64
+ raise OXRError.new response['description'], response
65
+ else
66
+ raise
67
+ end
68
+ end
69
+
70
+ def sources
71
+ self.class.sources
72
+ end
73
+
74
+ def self.sources
75
+ @sources ||= {}
76
+ end
77
+
78
+ def self.reset_sources
79
+ sources.clear
43
80
  end
44
81
  end
@@ -1,3 +1,3 @@
1
1
  class OXR
2
- VERSION = "0.1.0.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oxr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Parker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-25 00:00:00.000000000 Z
11
+ date: 2016-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler