Class: ThreeTapsAPI::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/three_taps_api/search.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#results

Instance Method Summary collapse

Methods inherited from Base

api_key, #auth_token_hash

Constructor Details

#initialize(args = {}) ⇒ Search

Returns a new instance of Search.



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/three_taps_api/search.rb', line 17

def initialize(args = {})
  @parameters = args
  @location = Location.new(args.delete(:location) || {})
  @postings = OpenStruct.new

  #args.delete :location

  args.each do |k, v|
    create_getter_and_setter k if ThreeTapsAPI.valid_parameter? k.to_s
    self.send "#{k.to_s}=", v
  end 
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/three_taps_api/search.rb', line 38

def method_missing(name, *args, &block)
  # TODO: return if not a setter function
  name = name.to_s.chop if name.to_s.reverse[0] == '='
  p "#{name} is not a valid parameter." and return if ThreeTapsAPI.invalid_parameter? name
  @parameters[name.to_sym] = args[0]
  create_getter_and_setter name
  self.send "#{name.to_s}=".to_sym, args[0]
end

Instance Attribute Details

#locationObject

Returns the value of attribute location.



8
9
10
# File 'lib/three_taps_api/search.rb', line 8

def location
  @location
end

#parametersObject (readonly)

Returns the value of attribute parameters.



7
8
9
# File 'lib/three_taps_api/search.rb', line 7

def parameters
  @parameters
end

#postingsObject (readonly)

Returns the value of attribute postings.



6
7
8
# File 'lib/three_taps_api/search.rb', line 6

def postings
  @postings
end

Instance Method Details

#create_getter_and_setter(name) ⇒ Object



10
11
12
13
14
15
# File 'lib/three_taps_api/search.rb', line 10

def create_getter_and_setter(name)
  get = Proc.new { instance_variable_get "@#{name}" }
  set = Proc.new { |val| instance_variable_set "@#{name}", val; @parameters[name.to_sym] = val }
  self.class.send :define_method, "#{name}", get
  self.class.send :define_method, "#{name}=", set
end

#searchObject



30
31
32
33
34
35
36
# File 'lib/three_taps_api/search.rb', line 30

def search
  opts = {}
  opts.merge!(auth_token_hash)
    .merge!(@parameters)
  @results = self.class.get self.class.base_uri, { query: opts }
  @postings = @results.parsed_response['postings'].map { |p| ThreeTapsAPI.rec_hash_to_openstruct p }
end