Class: SimplySuggest::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/simply_suggest/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



9
10
11
12
# File 'lib/simply_suggest/request.rb', line 9

def initialize
  @path_parts = []
  @params     = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



14
15
16
17
18
19
# File 'lib/simply_suggest/request.rb', line 14

def method_missing(method, *args)
  @path_parts << method.to_s.downcase
  @path_parts << args if args.length > 0
  @path_parts.flatten!
  self
end

Class Method Details

.method_missing(sym, *args, &block) ⇒ Object



4
5
6
# File 'lib/simply_suggest/request.rb', line 4

def method_missing(sym, *args, &block)
  new.send(sym, *args, &block)
end

Instance Method Details

#add_param(name, value) ⇒ Object



52
53
54
55
# File 'lib/simply_suggest/request.rb', line 52

def add_param name, value
  @params[name.to_sym] = value
  self
end

#add_params(params = {}) ⇒ Object



57
58
59
60
61
62
# File 'lib/simply_suggest/request.rb', line 57

def add_params params = {}
  params.each_pair do |name, value|
    @params[name.to_sym] = value
  end
  self
end

#get(*args) ⇒ Object



21
22
23
24
25
26
# File 'lib/simply_suggest/request.rb', line 21

def get *args
  @path_parts += args.to_a
  values = api_request.get(path, @params)
  reset
  values
end

#limit(num) ⇒ Object



42
43
44
45
# File 'lib/simply_suggest/request.rb', line 42

def limit num
  add_param :limit, num
  self
end

#page(num) ⇒ Object



47
48
49
50
# File 'lib/simply_suggest/request.rb', line 47

def page num
  add_param :page, num
  self
end

#patch(*args) ⇒ Object



35
36
37
38
39
40
# File 'lib/simply_suggest/request.rb', line 35

def patch *args
  @path_parts += args.to_a
  values = api_request.patch(path, @params)
  reset
  values
end

#pathObject



64
65
66
# File 'lib/simply_suggest/request.rb', line 64

def path
  @path_parts.join('/')
end

#post(*args) ⇒ Object



28
29
30
31
32
33
# File 'lib/simply_suggest/request.rb', line 28

def post *args
  @path_parts += args.to_a
  values = api_request.post(path, @params)
  reset
  values
end

#resetObject



68
69
70
# File 'lib/simply_suggest/request.rb', line 68

def reset
  @path_parts = []
end