Class: Dolarblue::Exchange
- Inherits:
-
Object
- Object
- Dolarblue::Exchange
- Defined in:
- lib/dolarblue/exchange.rb
Overview
Used to build dollar exchange value object
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#source_url ⇒ Object
readonly
Returns the value of attribute source_url.
-
#updated_at ⇒ Object
readonly
Returns the value of attribute updated_at.
Instance Method Summary collapse
-
#buy_value ⇒ Float
Buy (lowest) dollar exchange value usually obtained from sell value multiplied by sell factor.
-
#initialize(name, screen_name, regexp, buy_sell_factor) ⇒ self
constructor
Create a new dollar exchange value instance.
-
#output_link ⇒ String
Return a string suitable for user output with the link to the sources.
-
#output_values ⇒ String
Return a string suitable for user output about exchange values.
-
#sell_value ⇒ Float
Sell (highest) dollar exchange value usually obtained directly from the source.
-
#update! ⇒ self
Connect to the source and retrieve dollar exchange value.
-
#updated_ago ⇒ String
Return the last updated time from now humanized in English words.
-
#valid? ⇒ true, false
Returns valid state.
Constructor Details
#initialize(name, screen_name, regexp, buy_sell_factor) ⇒ self
Using ruby 2 feature: Keyworded Arguments
Create a new dollar exchange value instance
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/dolarblue/exchange.rb', line 22 def initialize(name, screen_name, regexp, buy_sell_factor) fail ArgumentError, "name, screen_name, regexp and buy_sell_factor are all required" unless name && screen_name && regexp && buy_sell_factor @name = name @screen_name = screen_name @regexp = regexp @buy_sell_factor = buy_sell_factor @sell_value = nil @source_url = nil @updated_at = nil self end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/dolarblue/exchange.rb', line 8 def name @name end |
#source_url ⇒ Object (readonly)
Returns the value of attribute source_url.
8 9 10 |
# File 'lib/dolarblue/exchange.rb', line 8 def source_url @source_url end |
#updated_at ⇒ Object (readonly)
Returns the value of attribute updated_at.
8 9 10 |
# File 'lib/dolarblue/exchange.rb', line 8 def updated_at @updated_at end |
Instance Method Details
#buy_value ⇒ Float
Buy (lowest) dollar exchange value
usually obtained from sell value multiplied by sell factor
38 39 40 41 |
# File 'lib/dolarblue/exchange.rb', line 38 def buy_value ensure_valid (@sell_value * @buy_sell_factor).round(2) end |
#output_link ⇒ String
Return a string suitable for user output with the link to the sources
91 92 93 94 95 |
# File 'lib/dolarblue/exchange.rb', line 91 def output_link ensure_valid dots = '.' * (15 - name.size).abs "#{name}#{dots}: #{source_url}" end |
#output_values ⇒ String
Return a string suitable for user output about exchange values
82 83 84 85 86 |
# File 'lib/dolarblue/exchange.rb', line 82 def output_values ensure_valid dots = '.' * (15 - name.size).abs "- #{name}#{dots}: #{'%.2f' %buy_value} / #{'%.2f' %sell_value} (Updated #{updated_ago})" end |
#sell_value ⇒ Float
Sell (highest) dollar exchange value
usually obtained directly from the source
47 48 49 50 |
# File 'lib/dolarblue/exchange.rb', line 47 def sell_value ensure_valid @sell_value.round(2) end |
#update! ⇒ self
Connect to the source and retrieve dollar exchange value
62 63 64 65 66 67 68 69 |
# File 'lib/dolarblue/exchange.rb', line 62 def update! tweet = Twitter::Client.new.last_tweet(@screen_name) fail "Couldn't obtain value out of #{tweet.text}" unless tweet.text =~ @regexp @sell_value = Float("#{$1}.#{$2}") @updated_at = tweet.created_at @source_url = tweet.status_url self end |
#updated_ago ⇒ String
Return the last updated time from now humanized in English words
74 75 76 77 |
# File 'lib/dolarblue/exchange.rb', line 74 def updated_ago ensure_valid @updated_at.to_time.ago_in_words end |
#valid? ⇒ true, false
Returns valid state
55 56 57 |
# File 'lib/dolarblue/exchange.rb', line 55 def valid? @sell_value && @sell_value.kind_of?(Numeric) && @sell_value > 0 end |