Class: NBP::ExchangeRate
- Inherits:
-
Object
- Object
- NBP::ExchangeRate
- Extended by:
- Commons
- Defined in:
- lib/nbp/exchange_rate.rb
Constant Summary
Constants included from Commons
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#nbp_file_name ⇒ Object
you can pass nbp_file_name directly.
Class Method Summary collapse
- .all_exchange_rates(date) ⇒ Object
- .by_file(file_name) ⇒ Object
- .on_date(date, table_name:, table_number:) ⇒ Object
Instance Method Summary collapse
- #fetch ⇒ Object
-
#initialize(base_file_name = nil, constant_element = 'z', file_extension = '.xml', **fields) ⇒ ExchangeRate
constructor
A new instance of ExchangeRate.
Methods included from Commons
extended, nbp_date_format_hash
Constructor Details
#initialize(base_file_name = nil, constant_element = 'z', file_extension = '.xml', **fields) ⇒ ExchangeRate
Returns a new instance of ExchangeRate.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/nbp/exchange_rate.rb', line 46 def initialize(base_file_name = nil, constant_element = 'z', file_extension = '.xml', **fields) if base_file_name @nbp_file_name = base_file_name + file_extension else @nbp_file_name = fields[:table_name] + fields[:table_number] + constant_element + fields[:year] + fields[:month] + fields[:day] + file_extension end rescue NoMethodError raise ExchangeError, 'One or more fields are missing. Unable to build the object.' end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
10 11 12 |
# File 'lib/nbp/exchange_rate.rb', line 10 def data @data end |
#nbp_file_name ⇒ Object
you can pass nbp_file_name directly
9 10 11 |
# File 'lib/nbp/exchange_rate.rb', line 9 def nbp_file_name @nbp_file_name end |
Class Method Details
.all_exchange_rates(date) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/nbp/exchange_rate.rb', line 26 def all_exchange_rates(date) file_names = files_names_on_date(date) mutex = Mutex.new threads = [] data = file_names.each_with_object([]) do |file_name, arr| threads << Thread.new do mutex.synchronize { arr << new(file_name).fetch['tabela_kursow']['pozycja'] } end end threads.map(&:join) data.flatten end |
.by_file(file_name) ⇒ Object
22 23 24 |
# File 'lib/nbp/exchange_rate.rb', line 22 def by_file(file_name) new(file_name) end |
.on_date(date, table_name:, table_number:) ⇒ Object
18 19 20 |
# File 'lib/nbp/exchange_rate.rb', line 18 def on_date(date, table_name:, table_number:) new({ table_name: table_name, table_number: table_number }.merge nbp_date_format_hash(date)) end |
Instance Method Details
#fetch ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/nbp/exchange_rate.rb', line 57 def fetch full_file_path = Commons::CORE_WEB_PATH + nbp_file_name @data = ::Nori.new(parser: :rexml).parse open(full_file_path).read fail ExchangeError, 'Missing field tabela_kursow in fetched data' unless @data.key?('tabela_kursow') return @data rescue OpenURI::HTTPError raise ExchangeError, 'Problem with connection or requested information not found.' end |