Created
August 18, 2012 20:51
-
-
Save regedarek/3389802 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> hash = { "One" => [{"Oneone" => "1", "Onetwo" => "2"}, {"Oneone" => "3", "Onetwo" => "4"}], "Two" => [{"Twoone" => "5", "Twotwo" => "6"}, {"Twoone" => "7", "Twotwo" => "8"}] } | |
{ | |
"One" => [ | |
[0] { | |
"Oneone" => "1", | |
"Onetwo" => "2" | |
}, | |
[1] { | |
"Oneone" => "3", | |
"Onetwo" => "4" | |
} | |
], | |
"Two" => [ | |
[0] { | |
"Twoone" => "5", | |
"Twotwo" => "6" | |
}, | |
[1] { | |
"Twoone" => "7", | |
"Twotwo" => "8" | |
} | |
] | |
} |
Author
regedarek
commented
Aug 18, 2012
@deep_link = data["DeepLink"]
@total_price = data["TotalPrice"]
@daily_rate = data["DailyRate"]
Hash[hash.keys.join,
hash.values.transpose.map { |hashes| hashes.inject &:merge }
]
[
[0] "12",
[1] [
[0] {
"TypicalSeating" => "4 adults",
"CarTypeName" => "Mid-size SUV",
"CarTypeCode" => "IFAR",
"PossibleFeatures" => "Automatic Transmission, Air Conditioning",
"PossibleModels" => "Ford Escape, Toyota Rav4 or similar",
"CurrencyCode" => "USD",
"DeepLink" => "http://www.hotwire.com/car/deeplink-details.jsp?resultId=NjcxMzE0ODgwMjoxOTQwNDA5NjMzNjg-&sid=0&bid=0&inputId=api-results&actionType=303",
"ResultId" => "NjcxMzE0ODgwMjoxOTQwNDA5NjMzNjg-",
"HWRefNumber" => "4380679554",
"SubTotal" => "343.80",
"TaxesAndFees" => "141.07",
"TotalPrice" => "484.87",
"DailyRate" => "85.95",
"DropoffDay" => "08/23/2012",
"DropoffTime" => "13:30",
"PickupDay" => "08/20/2012",
"PickupTime" => "10:00",
"LocationDescription" => "In Terminal",
"MileageDescription" => "Unlimited",
"PickupAirport" => "SEA",
"RentalDays" => "4"
},
[1] {
"TypicalSeating" => "5 adults",
"CarTypeName" => "Luxury car",
"CarTypeCode" => "PCAR",
"PossibleFeatures" => "Automatic Transmission, Power Steering, Air Conditioning, Air Bags, Anti-Lock Brakes, Cruise Control, AM/FM Stereo, CD Player",
"PossibleModels" => "Lincoln Town Car, Cadillac DTS, or similar",
"CurrencyCode" => "USD",
"DeepLink" => "http://www.hotwire.com/car/deeplink-details.jsp?resultId=NjcxMzE0ODgwMjoxOTQwNDA5NjMzMTE-&sid=0&bid=0&inputId=api-results&actionType=303",
"ResultId" => "NjcxMzE0ODgwMjoxOTQwNDA5NjMzMTE-",
"HWRefNumber" => "4572069055",
"SubTotal" => "383.80",
"TaxesAndFees" => "148.24",
"TotalPrice" => "532.04",
"DailyRate" => "95.95",
"DropoffDay" => "08/23/2012",
"DropoffTime" => "13:30",
"PickupDay" => "08/20/2012",
"PickupTime" => "10:00",
"LocationDescription" => "In Terminal",
"MileageDescription" => "Unlimited",
"PickupAirport" => "SEA",
"RentalDays" => "4"
},
[2] {
"TypicalSeating" => "5 adults",
"CarTypeName" => "Premium car",
"CarTypeCode" => "LCAR",
"PossibleFeatures" => "Automatic Transmission, Power Steering, Air Conditioning, Air Bags, Anti-Lock Brakes, Cruise Control, AM/FM Stereo, CD Player",
"PossibleModels" => "Ford Crown Victoria, Buick Lucerne, or similar",
"CurrencyCode" => "USD",
"DeepLink" => "http://www.hotwire.com/car/deeplink-details.jsp?resultId=NjcxMzE0ODgwMjoxOTQwNDA4OTgyNzU-&sid=0&bid=0&inputId=api-results&actionType=303",
"ResultId" => "NjcxMzE0ODgwMjoxOTQwNDA4OTgyNzU-",
"HWRefNumber" => "3863821393",
"SubTotal" => "411.80",
"TaxesAndFees" => "155.40",
"TotalPrice" => "567.20",
"DailyRate" => "102.95",
"DropoffDay" => "08/23/2012",
"DropoffTime" => "13:30",
"PickupDay" => "08/20/2012",
"PickupTime" => "10:00",
"LocationDescription" => "In Terminal",
"MileageDescription" => "Unlimited",
"PickupAirport" => "SEA",
"RentalDays" => "4"
},
[3] {
"TypicalSeating" => "5 adults",
"CarTypeName" => "Standard SUV",
"CarTypeCode" => "SFAR",
"PossibleFeatures" => "Automatic Transmission, Power Steering, Air Conditioning, Air Bags, Anti-Lock Brakes, Cruise Control, AM/FM Stereo, CD Player",
"PossibleModels" => "Jeep Grand Cherokee or similar",
"CurrencyCode" => "USD",
"DeepLink" => "http://www.hotwire.com/car/deeplink-details.jsp?resultId=NjcxMzE0ODgwMjoxOTQwNDA4OTgyNzI-&sid=0&bid=0&inputId=api-results&actionType=303",
"ResultId" => "NjcxMzE0ODgwMjoxOTQwNDA4OTgyNzI-",
"HWRefNumber" => "3669172053",
"SubTotal" => "411.80",
"TaxesAndFees" => "162.57",
"TotalPrice" => "574.37",
"DailyRate" => "102.95",
"DropoffDay" => "08/23/2012",
"DropoffTime" => "13:30",
"PickupDay" => "08/20/2012",
"PickupTime" => "10:00",
"LocationDescription" => "In Terminal",
"MileageDescription" => "Unlimited",
"PickupAirport" => "SEA",
"RentalDays" => "4"
}
]
]
module Suitcase
class CarRental
attr_accessor :seating, :type_name, :type_code, :possible_features, :possible_models
def initialize(data)
binding.pry_remote
@deep_link = data["DeepLink"]
@total_price = data["TotalPrice"]
@daily_rate = data["DailyRate"]
@seating = data["CarTypeSeating"]
@type_name = data["CarTypeName"]
@type_code = data["CarTypeCode"]
@possible_features = data["PossibleFeatures"].split(", ")
@possible_models = data["PossibleModels"].split(", ")
end
class << self
def find(info)
parsed = parse_json(build_url(info))
parse_errors(parsed)
h = Hash.new
h["1"] = parsed["MetaData"]["CarMetaData"]["CarTypes"]
h["2"] = parsed["Result"]
results = Hash[
h.keys.join,
h.values.transpose.map { |hashes| hashes.inject &:merge }
]
results.map do |data|
CarRental.new(data)
end
end
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
def parameterize(info)
info.map { |key, value| "#{key}=#{value}" }.join("&")
end
def parse_json(uri)
response = Net::HTTP.get_response(uri)
raise "Data not valid." if response.code != "200"
JSON.parse(response.body)
end
def parse_errors(parsed)
if parsed["Errors"] && !parsed["Errors"].empty?
binding.pry_remote
parsed["Errors"].each { |e| raise e }
end
end
end
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment