Class: Unicafe::Lunch
- Inherits:
-
Object
- Object
- Unicafe::Lunch
- Defined in:
- lib/unicafe/lunch.rb
Instance Attribute Summary collapse
-
#date ⇒ Object
readonly
Returns the value of attribute date.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#price ⇒ Object
readonly
Returns the value of attribute price.
Class Method Summary collapse
- .format_data(data) ⇒ Object
- .format_lunch(date, data) ⇒ Object
- .format_lunches_of_date(data) ⇒ Object
- .format_name(data) ⇒ Object
- .format_price(data) ⇒ Object
- .lunches_for_restaurant(id) ⇒ Object
- .parse_data(data) ⇒ Object
- .parse_date(date) ⇒ Object
Instance Method Summary collapse
-
#initialize(name, price, date) ⇒ Lunch
constructor
A new instance of Lunch.
Constructor Details
#initialize(name, price, date) ⇒ Lunch
Returns a new instance of Lunch.
11 12 13 14 15 |
# File 'lib/unicafe/lunch.rb', line 11 def initialize name, price, date @name = name @price = price @date = date end |
Instance Attribute Details
#date ⇒ Object (readonly)
Returns the value of attribute date.
9 10 11 |
# File 'lib/unicafe/lunch.rb', line 9 def date @date end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/unicafe/lunch.rb', line 9 def name @name end |
#price ⇒ Object (readonly)
Returns the value of attribute price.
9 10 11 |
# File 'lib/unicafe/lunch.rb', line 9 def price @price end |
Class Method Details
.format_data(data) ⇒ Object
27 28 29 |
# File 'lib/unicafe/lunch.rb', line 27 def self.format_data data data.entries.map{|date| self.format_lunches_of_date(date)}.flatten.compact end |
.format_lunch(date, data) ⇒ Object
38 39 40 41 |
# File 'lib/unicafe/lunch.rb', line 38 def self.format_lunch date, data self.new self.format_name(data), self.format_price(data), date rescue Exception => e end |
.format_lunches_of_date(data) ⇒ Object
31 32 33 34 35 36 |
# File 'lib/unicafe/lunch.rb', line 31 def self.format_lunches_of_date data date = self.parse_date data.title Nokogiri::HTML::DocumentFragment.parse(data.summary).children.map{ |lunch| self.format_lunch date, lunch } end |
.format_name(data) ⇒ Object
48 49 50 51 52 |
# File 'lib/unicafe/lunch.rb', line 48 def self.format_name data name_span = data.children.select{|elem| elem.name == 'span' && elem[:class] == "meal"}.first text_element = name_span.children.first text_element.to_s end |
.format_price(data) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/unicafe/lunch.rb', line 54 def self.format_price data name_span = data.children.select{|elem| elem.name == 'span' && elem[:class] == "priceinfo"}.first text_element = name_span.children.first parser = ::Unicafe::PriceParser.new parser.parse text_element.to_s end |
.lunches_for_restaurant(id) ⇒ Object
17 18 19 20 |
# File 'lib/unicafe/lunch.rb', line 17 def self.lunches_for_restaurant id content = ::Net::HTTP.get(URI.parse("http://www.hyyravintolat.fi/rss/fin/#{id}/")) self.parse_data content end |
.parse_data(data) ⇒ Object
22 23 24 25 |
# File 'lib/unicafe/lunch.rb', line 22 def self.parse_data data parsed_data = Feedzirra::Feed.parse data self.format_data parsed_data end |
.parse_date(date) ⇒ Object
43 44 45 46 |
# File 'lib/unicafe/lunch.rb', line 43 def self.parse_date date require 'date' Date.parse date end |