Class: TAPI::V3::GenericSearch

Inherits:
Object
  • Object
show all
Extended by:
TAPI::Validations
Includes:
Utils, Configurable
Defined in:
lib/tapi/v3/generic_search.rb

Direct Known Subclasses

Flights::Search, Hotels::Search

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TAPI::Validations

inherited_validations, validate, validates_date_format_of, validates_numericality_of, validates_presence_of, validations

Methods included from Utils

append_query, coerce_date, parse_date, symbolize_keys

Methods included from Configurable

#config, included

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/tapi/v3/generic_search.rb', line 87

def method_missing(key, *args)
  begin
    @client.send(key, *args)
  rescue NoMethodError
    raise NoMethodError, "undefined method `#{key}' for #{self.class}", caller
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/tapi/v3/generic_search.rb', line 8

def client
  @client
end

#client_urlsObject (readonly)

Returns the value of attribute client_urls.



8
9
10
# File 'lib/tapi/v3/generic_search.rb', line 8

def client_urls
  @client_urls
end

#errorsObject

Returns the value of attribute errors.



9
10
11
# File 'lib/tapi/v3/generic_search.rb', line 9

def errors
  @errors
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



8
9
10
# File 'lib/tapi/v3/generic_search.rb', line 8

def start_time
  @start_time
end

#started_atObject (readonly)

Returns the value of attribute started_at.



8
9
10
# File 'lib/tapi/v3/generic_search.rb', line 8

def started_at
  @started_at
end

Instance Method Details

#add_error(name, message) ⇒ Object



26
27
28
# File 'lib/tapi/v3/generic_search.rb', line 26

def add_error(name, message)
  (errors[name] ||= []) << message
end

#api_keyObject



54
55
56
# File 'lib/tapi/v3/generic_search.rb', line 54

def api_key
  config[:key]
end

#api_urlObject



49
50
51
52
# File 'lib/tapi/v3/generic_search.rb', line 49

def api_url
  port = config[:port] == 80 ? '' : ":#{config[:port]}"
  "http://#{ config[:host] }#{ port }#{ config[:path] }"
end

#get(path, options = {}) ⇒ Object



62
63
64
# File 'lib/tapi/v3/generic_search.rb', line 62

def get(path, options={})
  TAPI::V3::Client.new_from_get(api_url + path + '.json', options.merge(:key => api_key)).first
end

#has_errors?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/tapi/v3/generic_search.rb', line 40

def has_errors?
  errors.any?
end

#idObject



11
12
13
14
15
16
17
# File 'lib/tapi/v3/generic_search.rb', line 11

def id
  if @id
    @id
  elsif @client and @client.respond_to?(:id)
    @client.id
  end
end

#id=(id) ⇒ Object



19
20
21
# File 'lib/tapi/v3/generic_search.rb', line 19

def id=(id)
  @id = id
end

#load_clientObject



73
74
75
76
77
78
# File 'lib/tapi/v3/generic_search.rb', line 73

def load_client
  if @client_urls
    client, etag = TAPI::V3::Client.new_from_get(@client_urls[:search_url], {}, @etag)
    @client, @etag = client.search, etag unless @etag == etag
  end
end

#post_urlObject



58
59
60
# File 'lib/tapi/v3/generic_search.rb', line 58

def post_url
  api_url + '/' + item_path + '/searches.json'
end

#reloadObject



44
45
46
47
# File 'lib/tapi/v3/generic_search.rb', line 44

def reload
  load_client
  self
end

#restart!Object



80
81
82
83
84
85
# File 'lib/tapi/v3/generic_search.rb', line 80

def restart!
  if @client_urls
    TAPI::V3::Client.new_from_post(@client_urls[:restart_url], {})
    load_client
  end
end

#start!Object



66
67
68
69
70
71
# File 'lib/tapi/v3/generic_search.rb', line 66

def start!
  @started_at = Time.now
  @client = TAPI::V3::Client.new_from_post(post_url, parameters).search
  @start_time = Time.now - @started_at
  @client_urls = @client.urls
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
# File 'lib/tapi/v3/generic_search.rb', line 30

def valid?
  errors.clear

  self.class.inherited_validations.each do |validation|
    instance_eval(&validation)
  end

  errors.empty?
end