Class: NytJourneys::Journeys

Inherits:
Object
  • Object
show all
Defined in:
lib/nyt_journeys/journeys.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#descriptionObject

Returns the value of attribute description.



2
3
4
# File 'lib/nyt_journeys/journeys.rb', line 2

def description
  @description
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/nyt_journeys/journeys.rb', line 2

def name
  @name
end

#typeObject

Returns the value of attribute type.



2
3
4
# File 'lib/nyt_journeys/journeys.rb', line 2

def type
  @type
end

#urlObject

Returns the value of attribute url.



2
3
4
# File 'lib/nyt_journeys/journeys.rb', line 2

def url
  @url
end

Class Method Details

.allObject



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

.typesObject



23
24
25
# File 'lib/nyt_journeys/journeys.rb', line 23

def self.types
  all.collect {|journey| journey.type}.uniq
end

Instance Method Details

#costObject



43
44
45
# File 'lib/nyt_journeys/journeys.rb', line 43

def cost
  cost ||= doc.css("div.price p").text[/\$\S+/]
end

#datesObject



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

#docObject



31
32
33
# File 'lib/nyt_journeys/journeys.rb', line 31

def doc
  Nokogiri::HTML(open(url))
end

#itineraryObject



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

#lengthObject



47
48
49
# File 'lib/nyt_journeys/journeys.rb', line 47

def length
  length ||= doc.css("div.itinerary-info > p").text.strip[/.+s/]
end