Class: Suitcase::CarRental

Inherits:
Object
  • Object
show all
Defined in:
lib/suitcase/car_rental.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ CarRental

Returns a new instance of CarRental.



5
6
7
8
9
10
11
# File 'lib/suitcase/car_rental.rb', line 5

def initialize(data)
  @seating = data["CarTypeSeating"]
  @type_name = data["CarTypeName"]
  @type_code = data["CarTypeCode"]
  @possible_features = data["PossibleFeatures"].split(", ")
  @possible_models = data["PossibleModels"].split(", ")
end

Instance Attribute Details

#possible_featuresObject

Returns the value of attribute possible_features.



3
4
5
# File 'lib/suitcase/car_rental.rb', line 3

def possible_features
  @possible_features
end

#possible_modelsObject

Returns the value of attribute possible_models.



3
4
5
# File 'lib/suitcase/car_rental.rb', line 3

def possible_models
  @possible_models
end

#seatingObject

Returns the value of attribute seating.



3
4
5
# File 'lib/suitcase/car_rental.rb', line 3

def seating
  @seating
end

#type_codeObject

Returns the value of attribute type_code.



3
4
5
# File 'lib/suitcase/car_rental.rb', line 3

def type_code
  @type_code
end

#type_nameObject

Returns the value of attribute type_name.



3
4
5
# File 'lib/suitcase/car_rental.rb', line 3

def type_name
  @type_name
end

Class Method Details

.build_url(info) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/suitcase/car_rental.rb', line 23

def build_url(info)
  base_url = "http://api.hotwire.com/v1/search/car"
  info["apikey"] = Configuration.hotwire_api_key
  info["format"] = "JSON"
  if Configuration.use_hotwire_linkshare_id?
    info["linkshareid"] = Configuration.hotwire_linkshare_id
  end
  info["dest"] = info.delete(:destination)
  info["startdate"] = info.delete(:start_date)
  info["enddate"] = info.delete(:end_date)
  info["pickuptime"] = info.delete(:pickup_time)
  info["dropofftime"] = info.delete(:dropoff_time)
  base_url += "?" + parameterize(info)

  URI.parse(URI.escape(base_url))
end

.find(info) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/suitcase/car_rental.rb', line 14

def find(info)
  parsed = parse_json(build_url(info))
  parse_errors(parsed)

  parsed["MetaData"]["CarMetaData"]["CarTypes"].map do |data|
    CarRental.new(data)
  end
end

.parameterize(info) ⇒ Object



40
41
42
# File 'lib/suitcase/car_rental.rb', line 40

def parameterize(info)
  info.map { |key, value| "#{key}=#{value}" }.join("&")
end

.parse_errors(parsed) ⇒ Object



50
51
52
53
54
# File 'lib/suitcase/car_rental.rb', line 50

def parse_errors(parsed)
  if parsed["Errors"] && !parsed["Errors"].empty?
    parsed["Errors"].each { |e| raise e }
  end
end

.parse_json(uri) ⇒ Object



44
45
46
47
48
# File 'lib/suitcase/car_rental.rb', line 44

def parse_json(uri)
  response = Net::HTTP.get_response(uri)
  raise "Data not valid." if response.code != "200"
  JSON.parse(response.body)
end