bloomberg_currency 0.1.0 → 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: 0f537c2ce326a9b6ea8dbb464eb3d8941634f174
4
- data.tar.gz: 881ac7365063871ef8768566317a0834860d95f0
3
+ metadata.gz: 5750a9ea831b7ce49a0e34ea6f50dd02e055a84c
4
+ data.tar.gz: d8d18e027f2fb8af17eecb69272e7eba0db81dab
5
5
  SHA512:
6
- metadata.gz: d1e4c3cdf6d7c83cb1c46b6344821887b5a3ed95a378404315c7a2a81929de71f81dbb3e2912ad63c6f36ec931c62229a1e0a7930af980588ab1c45f7879bc7d
7
- data.tar.gz: 99ad8a6a0eacfc9438766833f2710bbf74fc142bff96bacac4172065d0682c9c0bcb28ef1ab2dfb7b401816f6deaf18841903a042c6a55692eaf89c4169b2c7d
6
+ metadata.gz: fae8f01e406f1ad92d792eaa7a3e4bc90faa3f39b75cace02be43b9fd1226c9c9b2e71e7ac5eb16b1730c1c4687839f79d869f9cde3b1fa7c449a00911a7db1b
7
+ data.tar.gz: fa004d8549d9606417bc50b3d5230ac8eeabf7897e3f49750bd9febaa98980831ee57a059560ffa40db49cc7e9cf10bf7bc6d4f289ae93faaff816eb7e45111f
data/.gitignore CHANGED
@@ -9,4 +9,9 @@
9
9
  /tmp/
10
10
 
11
11
  # CodeClimate Coverage Reporter
12
- codeclimate-coverage-reporter.sh
12
+ codeclimate-coverage-reporter.sh
13
+
14
+ .DS_Store
15
+ */**/.DS_Store
16
+
17
+ .byebug_history
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/sebasjimenez10/bloomberg_currency.svg?branch=master)](https://travis-ci.org/sebasjimenez10/bloomberg_currency) [![Code Climate](https://codeclimate.com/github/sebasjimenez10/bloomberg_currency/badges/gpa.svg)](https://codeclimate.com/github/sebasjimenez10/bloomberg_currency) [![Test Coverage](https://codeclimate.com/github/sebasjimenez10/bloomberg_currency/badges/coverage.svg)](https://codeclimate.com/github/sebasjimenez10/bloomberg_currency/coverage) [![Issue Count](https://codeclimate.com/github/sebasjimenez10/bloomberg_currency/badges/issue_count.svg)](https://codeclimate.com/github/sebasjimenez10/bloomberg_currency)
4
4
 
5
- Bloomberg Currency provides an easy-to-use API to pull currency exchange rates from the Bloomberg sites. If you would like to test out this gem before including it into your project, feel free to clone it, bundle it and run `bin/console` for an interactive prompt.
5
+ Bloomberg Currency provides an easy-to-use API to pull currency exchange rates from the Bloomberg site. If you would like to test out this gem before including it into your project, feel free to clone it, bundle it and run `bin/console` for an interactive prompt.
6
6
 
7
7
  ## Installation
8
8
 
@@ -22,19 +22,57 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- You just basically need to create a new instance of the Site object and then call the `quote` method on it, passing the `<currency_from>` and `<currency_to>` as parameters. Like this:
25
+ *NOTE*: From version `0.1.0` to `1.0.0` there are mayor breaking changes. Please make sure you read the docs before installing/upgrading to the newest version.
26
26
 
27
+ The gem usability has been improved in version 1.0. Now you only need to create an instance of the class `BC::Quote`, passing the two currencies you'd like to get information about, like this:
28
+
29
+ ```
30
+ quote = BC::Quote.new('USD', 'EUR')
31
+ => #<BC::Quote:0x007fe92c297670 @price=0.8156, @datetime=#<DateTime: 2018-01-16T17:13:00-05:00 ((2458135j,79980s,0n),-18000s,2299161j)>, @detail=#<BC::QuoteDetail:0x007fe92c3ae8d8 @open=0.8157, @day_range=0.8156..0.8159, @previous_close=0.8157, @last_52_weeks_range=0.8133..0.9529, @ytd_return=-2.14>, @available=true>
27
32
  ```
28
- site = BloombergCurrency::API::Site.new
29
- site.quote 'USD', 'EUR'
30
- # => => #<BloombergCurrency::Models::Quote:0x007fdb95161098 @price=#<BigDecimal:7fdb95189930,'0.921E0',9(18)>, @price_datetime=#<DateTime: 2016-11-11T16:47:00-05:00 ((2457704j,78420s,0n),-18000s,2299161j)>, @quote_details={:open=>"0.9180", :day_range=>"0.9156 - 0.9233", :previous_close=>"0.9180", :"52wk_range"=>"0.8612 - 0.9496", :ytd_return=>"0.10%"}>
33
+
34
+ Then the quote object offers a few accessor methods:
35
+
36
+ - `price`: Returns a float price
37
+ - `datetime`: Returns the date time when the price was captured
38
+ - `detail`: Returns an object of the class BC::QuoteDetail which encapsulate the details of the quote. Will be explained later on.
39
+ - `available?`: Return true or false if the exchange is available or not.
40
+
41
+
42
+ The `BC::QuoteDetail` class offers the following methods:
43
+
44
+ - `open`: Returns the value in which the currency opened.
45
+ - `day_range`: Returns the day range up until the moment the quote is generated.
46
+ - `previous_close`: Returns the value where the exchange closed last day.
47
+ - `last_52_weeks_range`: Returns the range od the last 52 weeks.
48
+ - `ytd_return`: Returns the YTD percentage.
49
+
50
+
51
+ *NOTE*: When the some of the markets close, the `open` and `day_range` attributes on the Bloomberg site aren't displayed. So those attributes may not be present if the market is closed. But the quote may still be available.
52
+
53
+ ## Examples
54
+
55
+ Getting information about the USD and EUR exchange:
56
+
57
+ ```
58
+ quote = BC::Quote.new('USD', 'EUR')
59
+ quote.price
60
+ => 0.8147
61
+
62
+ quote.datetime
63
+ => #<DateTime: 2018-01-16T14:35:00-05:00 ((2458135j,70500s,0n),-18000s,2299161j)>
64
+
65
+ quote.detail
66
+ => #<BC::QuoteDetail:0x007faadd9fb868 @open=0.8154, @day_range=0.8143..0.8199, @previous_close=0.8154, @last_52_weeks_range=0.8133..0.9529, @ytd_return=-1.91>
67
+
68
+ quote.available?
69
+ => true
70
+
31
71
  ```
32
- As you could notice it will return a Quote object. This object contains three main attributes: `price`, `price_datetime` and `quote_details`.
33
- Out of those three attributes, the `quote_details` attribute contains a hash, here's an example: `{:open=>"0.9180", :day_range=>"0.9156 - 0.9233", :previous_close=>"0.9180", :"52wk_range"=>"0.8612 - 0.9496", :ytd_return=>"0.10%"}`
34
72
 
35
73
  ## Development
36
74
 
37
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
75
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
76
 
39
77
  To install this gem onto your local machine, run `bundle exec rake install`.
40
78
 
data/Rakefile CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'bundler/gem_tasks'
3
4
  require 'rspec/core/rake_task'
4
5
 
@@ -7,7 +7,7 @@ require 'bloomberg_currency/version'
7
7
 
8
8
  Gem::Specification.new do |spec|
9
9
  spec.name = 'bloomberg_currency'
10
- spec.version = BloombergCurrency::VERSION
10
+ spec.version = BC::VERSION
11
11
  spec.authors = ['Sebastian Jimenez Velez']
12
12
  spec.email = ['sebasjimenezv@gmail.com']
13
13
 
@@ -37,7 +37,9 @@ Gem::Specification.new do |spec|
37
37
  spec.add_development_dependency 'rake', '~> 10.0'
38
38
  spec.add_development_dependency 'rspec', '~> 3.0'
39
39
  spec.add_development_dependency 'simplecov', '~> 0.11'
40
- spec.add_development_dependency 'nokogiri', '~> 1.6', '>= 1.6.8'
41
- spec.add_development_dependency 'rubocop', '~> 0.45.0'
42
- spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0.0'
40
+ spec.add_development_dependency 'rubocop', '~> 0.49.0'
41
+ spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0', '>= 1.0.0'
42
+ spec.add_development_dependency 'pry-byebug', '~> 3.5.1'
43
+
44
+ spec.add_runtime_dependency 'nokogiri', '~> 1.6', '>= 1.6.8'
43
45
  end
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'bloomberg_currency/version'
3
4
  require 'bloomberg_currency/api/site'
4
5
  require 'bloomberg_currency/api/host'
5
- require 'bloomberg_currency/models/quote'
6
+ require 'bloomberg_currency/quote'
7
+ require 'bloomberg_currency/quote_detail'
6
8
 
7
9
  # Main Gem Module
8
- module BloombergCurrency
10
+ module BC
9
11
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
- module BloombergCurrency
2
+
3
+ module BC
3
4
  module API
4
5
  class Host
5
6
  URL = 'https://www.bloomberg.com/quote/'.freeze
@@ -1,57 +1,62 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'open-uri'
3
4
  require 'nokogiri'
4
5
  require 'bigdecimal'
5
6
 
6
- module BloombergCurrency
7
+ module BC
7
8
  module API
8
9
  # Implements site scrapping
9
10
  class Site
10
- def quote(currency_one, currency_two)
11
- BloombergCurrency::Models::Quote.new(process(currency_one, currency_two))
12
- end
13
-
14
- private
11
+ class << self
12
+ def quote(currency_one, currency_two)
13
+ process(currency_one, currency_two)
14
+ end
15
15
 
16
- def process(currency_one, currency_two)
17
- site = load_site(currency_one, currency_two)
18
- parse(site)
19
- end
16
+ private
20
17
 
21
- def load_site(currency_one, currency_two)
22
- file = open("#{BloombergCurrency::API::Host::URL}#{currency_one}#{currency_two}:CUR")
23
- Nokogiri::HTML(file)
24
- end
18
+ def process(currency_one, currency_two)
19
+ site = load_site(currency_one, currency_two)
20
+ parse(site)
21
+ end
25
22
 
26
- def parse(document)
27
- parse_quote(document)
28
- end
23
+ def load_site(currency_one, currency_two)
24
+ file = open("#{BC::API::Host::URL}#{currency_one}#{currency_two}:CUR")
25
+ Nokogiri::HTML(file)
26
+ end
29
27
 
30
- def parse_quote(document)
31
- price_container = document.xpath("//div[contains(@class, 'price-container')]")
32
- detailed_quote_container = document.xpath("//div[contains(@class, 'detailed-quote')]")
28
+ def parse(document)
29
+ parse_quote(document)
30
+ rescue
31
+ { price: nil, datetime: nil, detail: nil, available: false }
32
+ end
33
33
 
34
- price_element = price_container.xpath("//div[@class='price']")
35
- price_datetime_element = price_container.xpath("//div[@class='price-datetime']")
34
+ def parse_quote(document)
35
+ price_container = document.xpath("//div[contains(@class, 'price-container')]")
36
+ detailed_quote_container = document.xpath("//div[contains(@class, 'detailed-quote')]")
36
37
 
37
- price = BigDecimal.new(price_element.text.strip.tr(',', ''))
38
- price_datetime = DateTime.strptime(price_datetime_element.text.strip, 'As of %H:%M %p %z %m/%d/%Y')
39
- quote_details_hash = quote_details(detailed_quote_container)
38
+ price_element = price_container.xpath("//div[@class='price']")
39
+ price_datetime_element = price_container.xpath("//div[@class='price-datetime']")
40
40
 
41
- { price: price, price_datetime: price_datetime, quote_details: quote_details_hash }
42
- end
41
+ price = price_element.text.strip.tr(',', '').to_f
42
+ datetime = DateTime.strptime(price_datetime_element.text.strip, 'As of %H:%M %p %z %m/%d/%Y')
43
+ details_hash = quote_details(detailed_quote_container)
43
44
 
44
- def quote_details(container)
45
- detail_elements = container.xpath("//div[@class='data-table data-table_detailed']").xpath(
46
- "//div[contains(@class, 'cell') and contains(@class, 'cell__mobile-basic')]"
47
- )
45
+ { price: price, datetime: datetime, detail: BC::QuoteDetail.new(details_hash), available: true }
46
+ end
48
47
 
49
- details_hash = {}
50
- details_matrix = detail_elements.map { |a| a.text.strip.split(' ') }
51
- details_matrix.each do |key_value_array|
52
- details_hash[key_value_array[0].downcase.tr(' ', '_').to_sym] = key_value_array[1]
48
+ def quote_details(container)
49
+ detail_elements = container.xpath("//div[@class='data-table data-table_detailed']").xpath(
50
+ "//div[contains(@class, 'cell') and contains(@class, 'cell__mobile-basic')]"
51
+ )
52
+
53
+ details_hash = {}
54
+ details_matrix = detail_elements.map { |a| a.text.strip.split(' ') }
55
+ details_matrix.each do |key_value_array|
56
+ details_hash[key_value_array[0].downcase.tr(' ', '_').to_sym] = key_value_array[1]
57
+ end
58
+ details_hash
53
59
  end
54
- details_hash
55
60
  end
56
61
  end
57
62
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BC
4
+ # Quote Information Class
5
+ class Quote
6
+ attr_accessor :price, :datetime, :detail, :available
7
+ def initialize(currency_one, currency_two)
8
+ result = BC::API::Site.quote(currency_one, currency_two)
9
+
10
+ @price = result[:price]
11
+ @datetime = result[:datetime]
12
+ @detail = result[:detail]
13
+ @available = result[:available]
14
+ end
15
+
16
+ def available?
17
+ available
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BC
4
+ # Quote Details Class
5
+ class QuoteDetail
6
+ attr_accessor :open, :day_range, :previous_close, :last_52_weeks_range, :ytd_return
7
+
8
+ def initialize(attributes = {})
9
+ @open = process_float(attributes[:open])
10
+ @day_range = process_range(attributes[:day_range])
11
+ @previous_close = process_float(attributes[:previous_close])
12
+ @last_52_weeks_range = process_range(attributes[:"52wk_range"])
13
+ @ytd_return = process_float(attributes[:ytd_return])
14
+ end
15
+
16
+ private
17
+
18
+ def process_range(range)
19
+ return unless range
20
+ edges = range.tr(',', '').strip.split('-')
21
+ edges[0].to_f..edges[1].to_f
22
+ end
23
+
24
+ def process_float(float)
25
+ return unless float
26
+ float.tr(',', '').to_f
27
+ end
28
+ end
29
+ end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
- module BloombergCurrency
3
- VERSION = '0.1.0'.freeze
2
+
3
+ module BC
4
+ VERSION = '1.0.0'.freeze
4
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bloomberg_currency
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Jimenez Velez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-11 00:00:00.000000000 Z
11
+ date: 2018-01-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,53 +67,73 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0.11'
69
69
  - !ruby/object:Gem::Dependency
70
- name: nokogiri
70
+ name: rubocop
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '1.6'
75
+ version: 0.49.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.49.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
76
90
  - - ">="
77
91
  - !ruby/object:Gem::Version
78
- version: 1.6.8
92
+ version: 1.0.0
79
93
  type: :development
80
94
  prerelease: false
81
95
  version_requirements: !ruby/object:Gem::Requirement
82
96
  requirements:
83
97
  - - "~>"
84
98
  - !ruby/object:Gem::Version
85
- version: '1.6'
99
+ version: '1.0'
86
100
  - - ">="
87
101
  - !ruby/object:Gem::Version
88
- version: 1.6.8
102
+ version: 1.0.0
89
103
  - !ruby/object:Gem::Dependency
90
- name: rubocop
104
+ name: pry-byebug
91
105
  requirement: !ruby/object:Gem::Requirement
92
106
  requirements:
93
107
  - - "~>"
94
108
  - !ruby/object:Gem::Version
95
- version: 0.45.0
109
+ version: 3.5.1
96
110
  type: :development
97
111
  prerelease: false
98
112
  version_requirements: !ruby/object:Gem::Requirement
99
113
  requirements:
100
114
  - - "~>"
101
115
  - !ruby/object:Gem::Version
102
- version: 0.45.0
116
+ version: 3.5.1
103
117
  - !ruby/object:Gem::Dependency
104
- name: codeclimate-test-reporter
118
+ name: nokogiri
105
119
  requirement: !ruby/object:Gem::Requirement
106
120
  requirements:
107
121
  - - "~>"
108
122
  - !ruby/object:Gem::Version
109
- version: 1.0.0
110
- type: :development
123
+ version: '1.6'
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: 1.6.8
127
+ type: :runtime
111
128
  prerelease: false
112
129
  version_requirements: !ruby/object:Gem::Requirement
113
130
  requirements:
114
131
  - - "~>"
115
132
  - !ruby/object:Gem::Version
116
- version: 1.0.0
133
+ version: '1.6'
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: 1.6.8
117
137
  description: Provides an easy to use API to get currency rates data from the Bloomberg
118
138
  website
119
139
  email:
@@ -122,6 +142,7 @@ executables: []
122
142
  extensions: []
123
143
  extra_rdoc_files: []
124
144
  files:
145
+ - ".byebug_history"
125
146
  - ".gitignore"
126
147
  - ".rspec"
127
148
  - ".rubocop.yml"
@@ -139,7 +160,8 @@ files:
139
160
  - lib/bloomberg_currency.rb
140
161
  - lib/bloomberg_currency/api/host.rb
141
162
  - lib/bloomberg_currency/api/site.rb
142
- - lib/bloomberg_currency/models/quote.rb
163
+ - lib/bloomberg_currency/quote.rb
164
+ - lib/bloomberg_currency/quote_detail.rb
143
165
  - lib/bloomberg_currency/version.rb
144
166
  homepage: https://github.com/sebasjimenez10/bloomberg_currency
145
167
  licenses:
@@ -162,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
162
184
  version: '0'
163
185
  requirements: []
164
186
  rubyforge_project:
165
- rubygems_version: 2.5.1
187
+ rubygems_version: 2.6.11
166
188
  signing_key:
167
189
  specification_version: 4
168
190
  summary: Bloomberg Currency helps you pull currency rates data from the Bloomberg
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
- module BloombergCurrency
3
- module Models
4
- # Quote Information
5
- class Quote
6
- attr_accessor :price, :price_datetime, :quote_details
7
- def initialize(hash)
8
- @price = hash[:price]
9
- @price_datetime = hash[:price_datetime]
10
- @quote_details = hash[:quote_details]
11
- end
12
- end
13
- end
14
- end