Class: TripIt::Base

Inherits:
Object
  • Object
show all
Extended by:
ParamUtil
Defined in:
lib/trip_it/base.rb

Instance Method Summary collapse

Methods included from ParamUtil

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

Instance Method Details

#Boolean(string) ⇒ Object

Raises:

  • (ArgumentError)


99
100
101
102
103
# File 'lib/trip_it/base.rb', line 99

def Boolean(string)
  return true if string == true || string =~ /^true$/i
  return false if string == false || string.nil? || string =~ /^false$/i
  raise ArgumentError.new("Invalid value for Boolean: \"#{string}\"")
end

#camelize(word) ⇒ Object



105
106
107
# File 'lib/trip_it/base.rb', line 105

def camelize(word)
  word.split(/[^a-z0-9]/i).map{|w| w.capitalize}.join
end

#chkAndPopulate(iVar, objType, prop) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/trip_it/base.rb', line 5

def chkAndPopulate(iVar, objType, prop)
  return if prop.nil?
  if prop.is_a?(Array)
    prop.each do |value|
      iVar << objType.new(value)
    end
  else
    iVar << objType.new(prop)
  end
end

#chkObjAndPopulate(client, iVar, objType, prop) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/trip_it/base.rb', line 16

def chkObjAndPopulate(client, iVar, objType, prop)
  return if prop.nil?
  if prop.is_a?(Array)
    prop.each do |value|
      iVar << objType.new(client, value["id"], value)
    end
  else
    iVar << objType.new(client, prop["id"], prop)
  end
end

#convertDT(tpitDT) ⇒ Object

Convert a TripIt DateTime Object to a Ruby DateTime Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/trip_it/base.rb', line 28

def convertDT(tpitDT)
  return nil if tpitDT.nil?
  date = tpitDT["date"]
  time = tpitDT["time"]
  offset = tpitDT["utc_offset"]
  if time.nil?
    # Just return a date
    Date.parse(date)
  elsif date.nil?
    # Or just a time
    Time.parse(time)
  else
    # Ideally both
    DateTime.parse("#{date}T#{time}#{offset}")
  end
end

#to_hashObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/trip_it/base.rb', line 76

def to_hash
  hash = {}
  self.instance_variables.each do |key|
    next if key == "@client"
    value = self.instance_variable_get(key)
    if value.is_a?(Array)
      # We have an array of objects. First get the type of class
      objectType = value.first.class.name.split("::").last
      # Now get all of the objects' to_hash values
      hashArr = value.map {|mem| mem.to_hash}
      hash[camelize(key[1..-1]).to_sym] = { objectType => hashArr }
    elsif value.class.name.split("::").first == "TripIt"
      # If it's a single one of our objects, call its to_hash method
      hash[camelize(key[1..-1]).to_sym] = value.to_hash
    elsif key=~/date_/
      hash[camelize(key[1..-1]).to_sym] = TripIt::TpDateTime.new(value).to_hash
    else
      hash[key[1..-1].to_sym] = value
    end
  end
  hash
end

#to_jsonObject



72
73
74
# File 'lib/trip_it/base.rb', line 72

def to_json
  { self.class.name.split("::").last => self.to_hash.to_json }
end

#to_xmlObject

Convert object to (crude) XML for create (API does not seem to accept JSON)



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
# File 'lib/trip_it/base.rb', line 46

def to_xml
  self.class.name == "TripIt::TpDateTime" ? xmlstr = "<DateTime>" : xmlstr = "<#{self.class.name.split("::").last}>"
  self.respond_to?("sequence") ? arr = self.sequence : arr = self.instance_variables
  arr.each do |key|
    next if key == "@client" # Skip the OAuth client
    value = self.instance_variable_get(key)
    next if value.nil?
    if value.is_a?(Array)
      next if value.empty?
      # We have an array of objects. First get the type of class
      objectType = value.first.class.name.split("::").last
      # Now get all of the objects' to_xml values    
      xmlArr = value.map { |mem| mem.to_xml }
      xmlstr << "<#{camelize(key[1..-1])}>#{xmlArr}</#{camelize(key[1..-1])}>"
    elsif value.class.name.split("::").first == "TripIt"
      # If it's a single one of our objects, call its to_xml method
      xmlstr << value.to_xml
    elsif key=~/date_/
      xmlstr << TripIt::TpDateTime.new(value).to_xml
    else
      xmlstr << "<#{key[1..-1]}>#{value}</#{key[1..-1]}>"
    end
  end
  self.class.name == "TripIt::TpDateTime" ? xmlstr << "</DateTime>" : xmlstr << "</#{self.class.name.split("::").last}>"
end