Class: Fanforce::API
- Inherits:
-
Object
show all
- Includes:
- Utils, Errors
- Defined in:
- lib/fanforce/api.rb,
lib/fanforce/api/_main.rb,
lib/fanforce/api/version.rb
Defined Under Namespace
Modules: Utils
Classes: DecodingError, Error, Response, Result, Results, ServerError
Constant Summary
collapse
- VERSION =
'0.17.0'
Instance Method Summary
collapse
-
#add_params(params_to_add) ⇒ Object
-
#apply_auth(req_params) ⇒ Object
-
#auth(auth_data = nil) ⇒ Object
-
#complete_url(path) ⇒ Object
-
#curl_command(method, path, req_params = {}) ⇒ Object
-
#delete(path, req_params = {}) ⇒ Object
-
#get(path, req_params = {}) ⇒ Object
-
#identify(req_params) ⇒ Object
-
#initialize(arg = {}) ⇒ API
constructor
-
#params ⇒ Object
-
#post(path, req_params = {}, retries = 0) ⇒ Object
-
#put(path, req_params = {}, retries = 0) ⇒ Object
-
#track(plugin_id, req_params) ⇒ Object
-
#url(path, req_params = {}) ⇒ Object
-
#valid_auth? ⇒ Boolean
Methods included from Utils
#collect_known_params, included, #remove_internal_params, #remove_nil_values, #remove_sensitive_params, #valid_fanforce_js_request?, #valid_fanforce_request?, #valid_install_request?, #valid_uninstall_request?
Constructor Details
#initialize(arg = {}) ⇒ API
Returns a new instance of API.
9
10
11
12
13
14
15
16
|
# File 'lib/fanforce/api/_main.rb', line 9
def initialize(arg={})
if arg.is_a?(Hash)
add_params(arg)
auth(@params) if @params.length > 0
elsif arg.is_a?(String)
auth(arg)
end
end
|
Instance Method Details
#add_params(params_to_add) ⇒ Object
22
23
24
|
# File 'lib/fanforce/api/_main.rb', line 22
def add_params(params_to_add)
params.merge!(collect_known_params params_to_add)
end
|
#apply_auth(req_params) ⇒ Object
98
99
100
|
# File 'lib/fanforce/api/_main.rb', line 98
def apply_auth(req_params)
req_params.merge(@auth_hash || {})
end
|
#auth(auth_data = nil) ⇒ Object
85
86
87
88
89
90
91
92
|
# File 'lib/fanforce/api/_main.rb', line 85
def auth(auth_data=nil)
@auth_hash ||= {}
return @auth_hash if is_blank?(auth_data)
auth_data = auth_data.is_a?(Hash) ? symbolize_keys(auth_data) : {api_key: auth_data.to_s}
@auth_hash.merge! remove_nil_values(api_key: auth_data[:api_key], organization_id: auth_data[:organization_id], organization_slug: auth_data[:organization_slug])
params.merge!(@auth_hash)
valid_auth?
end
|
#complete_url(path) ⇒ Object
102
103
104
|
# File 'lib/fanforce/api/_main.rb', line 102
def complete_url(path)
'http://' + Fanforce.api_domain + path
end
|
#curl_command(method, path, req_params = {}) ⇒ Object
31
32
33
34
|
# File 'lib/fanforce/api/_main.rb', line 31
def curl_command(method, path, req_params={})
req_params = apply_auth(req_params)
Fanforce::Utils.curl_command(method, complete_url(path), req_params)
end
|
#delete(path, req_params = {}) ⇒ Object
69
70
71
72
73
74
75
|
# File 'lib/fanforce/api/_main.rb', line 69
def delete(path, req_params={})
url = complete_url(path)
req_params = apply_auth(req_params)
RestClient.delete(url(path, req_params), :accept => :json) do |response, request, result, &block|
Fanforce::API::Response.process(response, request, url, req_params)
end
end
|
#get(path, req_params = {}) ⇒ Object
36
37
38
39
40
41
|
# File 'lib/fanforce/api/_main.rb', line 36
def get(path, req_params={})
req_params = apply_auth(req_params)
RestClient.get(url(path, req_params), :accept => :json) do |response, request, result, &block|
Fanforce::API::Response.process(response, request, complete_url(path), req_params)
end
end
|
#identify(req_params) ⇒ Object
77
78
79
|
# File 'lib/fanforce/api/_main.rb', line 77
def identify(req_params)
post '/bie/identify', req_params
end
|
#params ⇒ Object
18
19
20
|
# File 'lib/fanforce/api/_main.rb', line 18
def params
@params ||= {}
end
|
#post(path, req_params = {}, retries = 0) ⇒ Object
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/fanforce/api/_main.rb', line 43
def post(path, req_params={}, retries=0)
url = complete_url(path)
req_params = apply_auth(req_params) unless retries > 0
RestClient.post(url, MultiJson.dump(req_params), :content_type => :json, :accept => :json) do |response, request, result, &block|
if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2
puts "retried #{path} #{retries+1} times"
post(path, req_params, retries+1)
else
Fanforce::API::Response.process(response, request, url, req_params)
end
end
end
|
#put(path, req_params = {}, retries = 0) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/fanforce/api/_main.rb', line 56
def put(path, req_params={}, retries=0)
url = complete_url(path)
req_params = apply_auth(req_params) unless retries > 0
RestClient.put(url, MultiJson.dump(req_params), :content_type => :json, :accept => :json) do |response, request, result, &block|
if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2
puts "retried #{path} #{retries+1} times"
post(path, req_params, retries+1)
else
Fanforce::API::Response.process(response, request, url, req_params)
end
end
end
|
#track(plugin_id, req_params) ⇒ Object
81
82
83
|
# File 'lib/fanforce/api/_main.rb', line 81
def track(plugin_id, req_params)
post '/bie/track', req_params.merge(plugin_id: plugin_id)
end
|
#url(path, req_params = {}) ⇒ Object
26
27
28
29
|
# File 'lib/fanforce/api/_main.rb', line 26
def url(path, req_params={})
req_params = apply_auth(req_params)
"#{complete_url(path)}?#{to_query_string(req_params)}"
end
|
#valid_auth? ⇒ Boolean
94
95
96
|
# File 'lib/fanforce/api/_main.rb', line 94
def valid_auth?
is_present?(@auth_hash) and is_present?(@auth_hash[:api_key]) and is_present?(@auth_hash[:organization_id])
end
|