Class: ExpediaApi::HotelResponseList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/expedia_api/hotel_response_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entries: [], response: nil, exception: nil) ⇒ HotelResponseList

Returns a new instance of HotelResponseList.



6
7
8
9
10
# File 'lib/expedia_api/hotel_response_list.rb', line 6

def initialize(entries: [], response: nil, exception: nil)
  @response = response
  self.entries = extract_entries_from_response(response)
  @exception = exception
end

Instance Attribute Details

#exceptionObject (readonly)

Returns the value of attribute exception.



4
5
6
# File 'lib/expedia_api/hotel_response_list.rb', line 4

def exception
  @exception
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/expedia_api/hotel_response_list.rb', line 4

def response
  @response
end

Instance Method Details

#each(&block) ⇒ Object



12
13
14
# File 'lib/expedia_api/hotel_response_list.rb', line 12

def each(&block)
  @entries.each(&:block)
end

#entriesObject



28
29
30
# File 'lib/expedia_api/hotel_response_list.rb', line 28

def entries
  @entries
end

#entries=(entries) ⇒ Object



32
33
34
# File 'lib/expedia_api/hotel_response_list.rb', line 32

def entries=(entries)
  @entries = entries.map {|e| ExpediaApi::Entities::SearchEntity.new(e.with_indifferent_access) }
end

#error?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/expedia_api/hotel_response_list.rb', line 40

def error?
  !!exception
end

#extract_entries_from_response(response) ⇒ Object



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/expedia_api/hotel_response_list.rb', line 47

def extract_entries_from_response(response)
  # probably an error ocurred connecting
  if response.nil?
    return []
  end
  body = nil
  begin
    body = JSON.parse(response.body)
  rescue JSON::ParserError => e
    @exception = e
    return []
  end
  if !body.is_a?(Hash) || body.nil?
    return []
  end
  hotel_count = body["HotelCount"].to_i
  if hotel_count == 1
    [body["HotelInfoList"]["HotelInfo"]]
  elsif hotel_count > 1
    body["HotelInfoList"]["HotelInfo"]
  else
    []
  end
end

#firstObject



20
21
22
# File 'lib/expedia_api/hotel_response_list.rb', line 20

def first
  @entries.first
end

#lastObject



24
25
26
# File 'lib/expedia_api/hotel_response_list.rb', line 24

def last
  @entries.last
end

#map(&block) ⇒ Object



16
17
18
# File 'lib/expedia_api/hotel_response_list.rb', line 16

def map(&block)
  @entries.map(&:block)
end

#response_bodyObject



44
45
# File 'lib/expedia_api/hotel_response_list.rb', line 44

def response_body
end

#success?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/expedia_api/hotel_response_list.rb', line 36

def success?
  exception.nil?
end