Class: NytJourneys::Journeys
- Inherits:
-
Object
- Object
- NytJourneys::Journeys
- Defined in:
- lib/nyt_journeys/journeys.rb
Constant Summary collapse
- @@all =
[]
Instance Attribute Summary collapse
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
- .all ⇒ Object
- .create_from_collection(trip_array) ⇒ Object
- .find(id) ⇒ Object
- .find_by_type(type) ⇒ Object
- .types ⇒ Object
Instance Method Summary collapse
- #cost ⇒ Object
- #dates ⇒ Object
- #doc ⇒ Object
-
#initialize(trip_hash) ⇒ Journeys
constructor
A new instance of Journeys.
- #itinerary ⇒ Object
- #length ⇒ Object
Constructor Details
#initialize(trip_hash) ⇒ Journeys
Returns a new instance of Journeys.
6 7 8 9 |
# File 'lib/nyt_journeys/journeys.rb', line 6 def initialize(trip_hash) trip_hash.each {|k,v| self.send("#{k}=",v)} @@all << self end |
Instance Attribute Details
#description ⇒ Object
Returns the value of attribute description.
2 3 4 |
# File 'lib/nyt_journeys/journeys.rb', line 2 def description @description end |
#name ⇒ Object
Returns the value of attribute name.
2 3 4 |
# File 'lib/nyt_journeys/journeys.rb', line 2 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
2 3 4 |
# File 'lib/nyt_journeys/journeys.rb', line 2 def type @type end |
#url ⇒ Object
Returns the value of attribute url.
2 3 4 |
# File 'lib/nyt_journeys/journeys.rb', line 2 def url @url end |
Class Method Details
.all ⇒ Object
15 16 17 |
# File 'lib/nyt_journeys/journeys.rb', line 15 def self.all @@all end |
.create_from_collection(trip_array) ⇒ Object
11 12 13 |
# File 'lib/nyt_journeys/journeys.rb', line 11 def self.create_from_collection(trip_array) trip_array.each {|trip| self.new(trip)} end |
.find(id) ⇒ Object
19 20 21 |
# File 'lib/nyt_journeys/journeys.rb', line 19 def self.find(id) all[id - 1] end |
.find_by_type(type) ⇒ Object
27 28 29 |
# File 'lib/nyt_journeys/journeys.rb', line 27 def self.find_by_type(type) all.select {|journey| journey.type == type} end |
.types ⇒ Object
23 24 25 |
# File 'lib/nyt_journeys/journeys.rb', line 23 def self.types all.collect {|journey| journey.type}.uniq end |
Instance Method Details
#cost ⇒ Object
43 44 45 |
# File 'lib/nyt_journeys/journeys.rb', line 43 def cost cost ||= doc.css("div.price p").text[/\$\S+/] end |
#dates ⇒ Object
51 52 53 |
# File 'lib/nyt_journeys/journeys.rb', line 51 def dates dates ||= doc.css("div.departures a.departure-link").collect {|date_range| date_range.text} end |
#doc ⇒ Object
31 32 33 |
# File 'lib/nyt_journeys/journeys.rb', line 31 def doc Nokogiri::HTML(open(url)) end |
#itinerary ⇒ Object
55 56 57 58 59 |
# File 'lib/nyt_journeys/journeys.rb', line 55 def itinerary itinerary ||= doc.css("div.primary-information").collect do |day| "#{day.css("div.day-number").text} - #{day.css("h3.day-title").text}" end end |
#length ⇒ Object
47 48 49 |
# File 'lib/nyt_journeys/journeys.rb', line 47 def length length ||= doc.css("div.itinerary-info > p").text.strip[/.+s/] end |