Class: TripIt::Trip

Inherits:
Base
  • Object
show all
Defined in:
lib/trip_it/classes/trip.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#Boolean, #camelize, #chkAndPopulate, #chkObjAndPopulate, #convertDT, #to_hash, #to_json, #to_xml

Methods included from ParamUtil

#address_param, #airportcode_param, #array_param, #boolean_param, #boolean_read_param, #camelize, #date_param, #datetime_param, #exceptions, #float_param, #integer_param, #string_param, #time_param, #traveler_array_param, #traveler_param

Constructor Details

#initialize(client, obj_id = nil, include_children = false, source = nil) ⇒ Trip

Returns a new instance of Trip.



9
10
11
12
13
14
15
16
# File 'lib/trip_it/classes/trip.rb', line 9

def initialize(client, obj_id = nil, include_children = false, source = nil)
  @client = client
  # If we get an Object ID, we'll load and populate it. Otherwise assume its a new Trip Object.
  unless obj_id.nil?
    @obj_id = obj_id
    include_children ? populate(source, :include_objects => true) : populate(source)
  end
end

Instance Attribute Details

#closeness_matchesObject (readonly)

Returns the value of attribute closeness_matches.



3
4
5
# File 'lib/trip_it/classes/trip.rb', line 3

def closeness_matches
  @closeness_matches
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/trip_it/classes/trip.rb', line 3

def id
  @id
end

#infoObject (readonly)

Returns the value of attribute info.



3
4
5
# File 'lib/trip_it/classes/trip.rb', line 3

def info
  @info
end

#relative_urlObject (readonly)

Returns the value of attribute relative_url.



3
4
5
# File 'lib/trip_it/classes/trip.rb', line 3

def relative_url
  @relative_url
end

#trip_crs_remarksObject (readonly)

Returns the value of attribute trip_crs_remarks.



3
4
5
# File 'lib/trip_it/classes/trip.rb', line 3

def trip_crs_remarks
  @trip_crs_remarks
end

#trip_inviteesObject (readonly)

Returns the value of attribute trip_invitees.



3
4
5
# File 'lib/trip_it/classes/trip.rb', line 3

def trip_invitees
  @trip_invitees
end

Instance Method Details

#activitiesObject



91
92
93
94
95
96
97
98
# File 'lib/trip_it/classes/trip.rb', line 91

def activities
  return @activities unless @activities.nil?
  actobj = @client.list("/object", {:trip_id => @id, :type => "activity"})["ActivityObject"]
  @activities = []
  return @activities if actobj.nil?
  chkObjAndPopulate(@client, @activities, TripIt::ActivityObject, actobj)
  return @activities
end

#airObject



100
101
102
103
104
105
106
107
# File 'lib/trip_it/classes/trip.rb', line 100

def air
  return @air unless @air.nil?
  airobj = @client.list("/object", {:trip_id => @id, :type => "air"})["AirObject"]
  @air = []
  return @air if airobj.nil?
  chkObjAndPopulate(@client, @air, TripIt::AirObject, airobj)
  return @air
end

#carsObject



109
110
111
112
113
114
115
116
# File 'lib/trip_it/classes/trip.rb', line 109

def cars
  return @cars unless @cars.nil?
  carobj = @client.list("/object", {:trip_id => @id, :type => "car"})["CarObject"]
  @cars = []
  return @cars if carobj.nil?
  chkObjAndPopulate(@client, @cars, TripIt::CarObject, carobj)
  return @cars
end

#cruisesObject



118
119
120
121
122
123
124
125
# File 'lib/trip_it/classes/trip.rb', line 118

def cruises
  return @cruises unless @cruises.nil?
  cruiseobj = @client.list("/object", {:trip_id => @id, :type => "cruise"})["CruiseObject"]
  @cruises = []
  return @cruises if cruiseobj.nil?
  chkObjAndPopulate(@client, @cruises, TripIt::CruiseObject, cruiseobj)
  return @cruises
end

#directionsObject



127
128
129
130
131
132
133
134
# File 'lib/trip_it/classes/trip.rb', line 127

def directions
  return @directions unless @directions.nil?
  directobj = @client.list("/object", {:trip_id => @id, :type => "directions"})["DirectionsObject"]
  @directions = []
  return @directions if directobj.nil?
  chkObjAndPopulate(@client, @directions, TripIt::DirectionsObject, directobj)
  return @directions
end

#lodgingsObject



136
137
138
139
140
141
142
143
# File 'lib/trip_it/classes/trip.rb', line 136

def lodgings
  return @lodgings unless @lodgings.nil?
  lodgingobj = @client.list("/object", {:trip_id => @id, :type => "lodging"})["LodgingObject"]
  @lodgings = []
  return @lodgings if lodgingobj.nil?
  chkObjAndPopulate(@client, @lodgings, TripIt::LodgingObject, lodgingobj)
  return @lodgings
end

#mapsObject



145
146
147
148
149
150
151
152
# File 'lib/trip_it/classes/trip.rb', line 145

def maps
  return @maps unless @maps.nil?
  mapsobj = @client.list("/object", {:trip_id => @id, :type => "map"})["MapObject"]
  @maps = []
  return @maps if mapsobj.nil?
  chkObjAndPopulate(@client, @maps, TripIt::MapObject, mapsobj)
  return @maps
end

#notesObject



154
155
156
157
158
159
160
161
# File 'lib/trip_it/classes/trip.rb', line 154

def notes
  return @notes unless @notes.nil?
  noteobj = @client.list("/object", {:trip_id => @id, :type => "note"})["NoteObject"]
  @notes = []
  return @notes if noteobj.nil?
  chkObjAndPopulate(@client, @notes, TripIt::NoteObject, noteobj)
  return @notes   
end

#populate(source, params = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/trip_it/classes/trip.rb', line 18

def populate(source, params = {})
  params.merge!( {:id => @obj_id} )
  info = source || @client.get("/trip", params)
        
  @id                         = info['Trip']['id']
  @primary_location           = info['Trip']['primary_location']
  @is_private                 = Boolean(info['Trip']['is_private'])
  @display_name               = info['Trip']['display_name']
  @description                = info['Trip']['description']
  @start_date                 = Date.parse(info['Trip']['start_date']) unless info['Trip']['start_date'].nil?
  @end_date                   = Date.parse(info['Trip']['end_date']) unless info['Trip']['end_date'].nil?
  @image_url                  = info['Trip']['image_url']
  @relative_url               = info['Trip']['relative_url']
  @trip_crs_remarks           = []
  @trip_invitees              = []
  @closeness_matches          = []
  
  chkAndPopulate(@trip_crs_remarks, TripIt::TripCrsRemark, info['Trip']['TripCrsRemarks']['TripCrsRemark']) unless info['Trip']['TripCrsRemarks'].nil?
  
  # Load associated profiles into Profile objects
  profileInfo                 = info['Profile']
  profiles                    = []
  if profileInfo.is_a?(Array)
    profileInfo.each do |pi|
      profiles << TripIt::Profile.new(@client, pi)
    end
  else
    profiles << TripIt::Profile.new(@client, profileInfo)
  end
  # And now match them into the invitees and closenessmatches array
  unless info['Trip']['ClosenessMatches'].nil?
    if info['Trip']['ClosenessMatches']['Match'].is_a?(Array)
      info['Trip']['ClosenessMatches']['Match'].each do |match|
        @closeness_matches << profiles.find { |profile| profile.ref == match['@attributes']['profile_ref'] }
      end
    else
      @closeness_matches << profiles.find { |profile| profile.ref == info['Trip']['ClosenessMatches']['Match']['@attributes']['profile_ref'] }
    end
  end
  
  unless info['Trip']['TripInvitees'].nil?
    if info['Trip']['TripInvitees']['Invitee'].is_a?(Array)
      info['Trip']['TripInvitees']['Invitee'].each do |invitee|
        @trip_invitees << TripIt::Invitee.new(invitee, profiles.find { |profile| profile.ref == invitee['@attributes']['profile_ref'] })
      end
    else
      @trip_invitees << TripIt::Invitee.new(info['Trip']['TripInvitees']['Invitee'], profiles.find { |profile| profile.ref == info['Trip']['TripInvitees']['Invitee']['@attributes']['profile_ref'] })
    end
  end
  
  # If include_objects is true, we need to fill them up here
  if params[:include_objects] == true
    [
      [info['ActivityObject'], @activities = [], TripIt::ActivityObject],
      [info['AirObject'], @air = [], TripIt::AirObject],
      [info['CarObject'], @cars = [], TripIt::CarObject],
      [info['CruiseObject'], @cruises = [], TripIt::CruiseObject],
      [info['DirectionsObject'], @directions = [], TripIt::DirectionsObject],
      [info['LodgingObject'], @lodgings = [], TripIt::LodgingObject],
      [info['MapObject'], @maps = [], TripIt::MapObject],
      [info['NoteObject'], @notes = [], TripIt::NoteObject],
      [info['RailObject'], @rail = [], TripIt::RailObject],
      [info['RestaurantObject'], @restaurants = [], TripIt::RestaurantObject],
      [info['TransportObject'], @transports = [], TripIt::TransportObject],
      [info['WeatherObject'], @weather = [], TripIt::WeatherObject]
    ].each do |obj|
      unless obj[0].nil?
        chkObjAndPopulate(@client, obj[1], obj[2], obj[0])
      end
    end
  end
end

#railObject



163
164
165
166
167
168
169
170
# File 'lib/trip_it/classes/trip.rb', line 163

def rail
  return @rail unless @rail.nil?
  railobj = @client.list("/object", {:trip_id => @id, :type => "rail"})["RailObject"]
  @rail = []
  return @rail if railobj.nil?
  chkObjAndPopulate(@client, @rail, TripIt::RailObject, railobj)
  return @rail
end

#restaurantsObject



172
173
174
175
176
177
178
179
# File 'lib/trip_it/classes/trip.rb', line 172

def restaurants
  return @restaurants unless @restaurants.nil?
  restobj = @client.list("/object", {:trip_id => @id, :type => "restaurant"})["RestaurantObject"]
  @restaurants = []
  return @rail if restobj.nil?
  chkObjAndPopulate(@client, @restaurants, TripIt::RestaurantObject, restobj)
  return @restaurants
end

#saveObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/trip_it/classes/trip.rb', line 203

def save
  [ @activities,
    @air,
    @cars,
    @cruises,
    @directions,
    @lodgings,
    @maps,
    @notes,
    @rail,
    @restaurants,
    @transports,
    @weather
  ].each do |obj|
      unless obj.nil?
        if obj.count > 1
          raise NotImplementedError, "TripIt can only save one complex object at a time."
        end
      end
    end
    
    # We only want to allow save for new objects for now.
    if @obj_id.nil?
      @client.create(self.xml)
    end
end

#sequenceObject



199
200
201
# File 'lib/trip_it/classes/trip.rb', line 199

def sequence
  ["@start_date", "@end_date", "@description", "@display_name","@image_url", "@is_private", "@primary_location"]
end

#transportsObject



181
182
183
184
185
186
187
188
# File 'lib/trip_it/classes/trip.rb', line 181

def transports
  return @transports unless @transports.nil?
  transobj = @client.list("/object", {:trip_id => @id, :type => "transport"})["TransportObject"]
  @transports = []
  return @transports if transobj.nil?
  chkObjAndPopulate(@client, @transports, TripIt::TransportObject, transobj)
  return @transports
end

#weatherObject



190
191
192
193
194
195
196
197
# File 'lib/trip_it/classes/trip.rb', line 190

def weather
  return @weather unless @weather.nil?
  wxobj = @client.list("/object", {:trip_id => @id, :type => "weather"})["WeatherObject"]
  @weather = []
  return @weather if wxobj.nil?
  chkObjAndPopulate(@client, @weather, TripIt::WeatherObject, wxobj)
  return @weather      
end