Class: FourRuby::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/four_ruby/base.rb
Constant Summary
collapse
- BASE_URL =
'https://api.foursquare.com/v2'
- ENDPOINTS =
[:users, :venues, :tips, :settings, :multi]
- POST_ACTIONS =
[:request, :unfriend, :approve, :deny, :setpings, :marktodo, :flag, :proposeedit, :markdone, :unmark, :add, :set]
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(oauth2) ⇒ Base
16
17
18
19
20
21
22
|
# File 'lib/four_ruby/base.rb', line 16
def initialize(oauth2)
@oauth2 = oauth2
@query = Hashie::Clash.new
@endpoint = nil
@result = nil
@post = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, params = {}) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/four_ruby/base.rb', line 24
def method_missing(method_name, params={})
if ENDPOINTS.include?(method_name)
@endpoint = method_name
@query.send(@endpoint, params)
elsif @endpoint
if @query[@endpoint][method_name]
@query[@endpoint].merge!( {method_name => params.merge(@query[@endpoint][method_name])})
else
@post = POST_ACTIONS.include? method_name
@query[@endpoint].merge!({ method_name => params })
end
else
raise BadRequest, "You must specify an one of #{ENDPOINTS * ", "}."
end
@result = nil
self
end
|
Instance Attribute Details
#query ⇒ Object
Returns the value of attribute query.
14
15
16
|
# File 'lib/four_ruby/base.rb', line 14
def query
@query
end
|
Instance Method Details
#[](i) ⇒ Object
67
68
69
|
# File 'lib/four_ruby/base.rb', line 67
def [](i)
self.to_json[i]
end
|
#clear ⇒ Object
85
86
87
88
89
90
|
# File 'lib/four_ruby/base.rb', line 85
def clear
@query = Hashie::Clash.new
@endpoint = nil
@result = nil
@post = false
end
|
#parse_response(response) ⇒ Object
80
81
82
83
|
# File 'lib/four_ruby/base.rb', line 80
def parse_response(response)
raise_errors(response)
Crack::JSON.parse(response.body)
end
|
#send ⇒ Object
76
77
78
|
# File 'lib/four_ruby/base.rb', line 76
def send
parse_response(@oauth2.get(self.to_url))
end
|
#to_json ⇒ Object
71
72
73
74
|
# File 'lib/four_ruby/base.rb', line 71
def to_json
return {} if @query.blank?
@result ||= send
end
|
#to_url ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/four_ruby/base.rb', line 43
def to_url
return BASE_URL if @endpoint.nil?
url = "#{BASE_URL}/#{@endpoint.to_s}#{@query[@endpoint][:id].nil? ? "" : "/" + @query[@endpoint][:id].to_s}"
unless @post
@query[@endpoint].each do |k,v|
next if k == :id
url << "/#{k}?"
url << stringify_keys(v)
end
end
if (@query[@endpoint].size <= 1 && @query[@endpoint][:id]) || (@query[@endpoint].size == 0 && url[-1..url.length] != '?')
url << "?"
else
url << "&" if url[-1..url.length] != "?"
end
url << "client_id=#{@oauth2.id}&client_secret=#{@oauth2.secret}"
url = URI.escape(url)
url
end
|