Class: Dolarblue::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/dolarblue/exchange.rb

Overview

Used to build dollar exchange value object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, screen_name, regexp, buy_sell_factor) ⇒ self

Note:

Using ruby 2 feature: Keyworded Arguments

Create a new dollar exchange value instance

Parameters:

  • name (String)

    the name of the object actual values are ‘Blue’ and ‘Official’

  • screen_name (String)

    the twitter screen_name from which to retrieve the values

  • regexp (Regexp)

    the regular expressions to be used to parse the dollar values from the tweet text

  • buy_sell_factor (Float)

    the factor to multiply sale value in order to get buy value difference is usually 1% so a factor of 0.99 would apply to get that



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

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/dolarblue/exchange.rb', line 8

def name
  @name
end

#source_urlObject (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_atObject (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_valueFloat

Buy (lowest) dollar exchange value

usually obtained from sell value multiplied by sell factor

Returns:

  • (Float)

    lowest dollar exchange value



38
39
40
41
# File 'lib/dolarblue/exchange.rb', line 38

def buy_value
  ensure_valid
  (@sell_value * @buy_sell_factor).round(2)
end

Return a string suitable for user output with the link to the sources

Returns:

  • (String)

    output links with sources from which the values where obtained



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_valuesString

Return a string suitable for user output about exchange values

Returns:

  • (String)

    output exchange values 1 line string



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_valueFloat

Sell (highest) dollar exchange value

usually obtained directly from the source

Returns:

  • (Float)

    highest dollar exchange value



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

Returns:

  • (self)


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_agoString

Return the last updated time from now humanized in English words

Returns:

  • (String)

    humanized ago date in English



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

Returns:

  • (true, false)

    valid boolean state



55
56
57
# File 'lib/dolarblue/exchange.rb', line 55

def valid?
  @sell_value && @sell_value.kind_of?(Numeric) && @sell_value > 0
end