fanforce 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +67 -1
- data/lib/fanforce/main.rb +43 -36
- data/lib/fanforce/utils.rb +42 -29
- data/lib/fanforce/version.rb +1 -1
- data/test/fanforce_test.rb +8 -4
- metadata +1 -1
data/README.md
CHANGED
@@ -14,4 +14,70 @@ $ cd fanforce-ruby
|
|
14
14
|
$ git checkout master
|
15
15
|
$ rake gem
|
16
16
|
$ gem install pkg/fanforce-{version}
|
17
|
-
```
|
17
|
+
```
|
18
|
+
|
19
|
+
## Getting Started
|
20
|
+
|
21
|
+
### Set It Up
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
require 'rubygems' # not necessary with ruby 1.9 but included for completeness
|
25
|
+
require 'fanforce'
|
26
|
+
|
27
|
+
# put your own credentials here
|
28
|
+
api_key = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
29
|
+
|
30
|
+
# set up a client to talk to the Fanforce API
|
31
|
+
@ff = Fanforce.new api_key
|
32
|
+
```
|
33
|
+
|
34
|
+
### Making Your First Call
|
35
|
+
|
36
|
+
``` ruby
|
37
|
+
# get your api access info
|
38
|
+
@ff.get('/access_info', {})
|
39
|
+
```
|
40
|
+
|
41
|
+
## Full REST Access
|
42
|
+
|
43
|
+
### GET
|
44
|
+
|
45
|
+
``` ruby
|
46
|
+
ff.get('/', {})
|
47
|
+
```
|
48
|
+
|
49
|
+
### POST
|
50
|
+
|
51
|
+
``` ruby
|
52
|
+
ff.post('/', {})
|
53
|
+
```
|
54
|
+
|
55
|
+
### PUT
|
56
|
+
|
57
|
+
``` ruby
|
58
|
+
ff.put('/', {})
|
59
|
+
```
|
60
|
+
|
61
|
+
### DELETE
|
62
|
+
|
63
|
+
``` ruby
|
64
|
+
ff.delete('/')
|
65
|
+
```
|
66
|
+
|
67
|
+
## Error Handling
|
68
|
+
|
69
|
+
begin
|
70
|
+
|
71
|
+
rescue Fanforce::Error => e
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
e.curl_command
|
76
|
+
e.response_code
|
77
|
+
e.response_body
|
78
|
+
e.request_url
|
79
|
+
e.request_params
|
80
|
+
|
81
|
+
## More Information
|
82
|
+
|
83
|
+
Visit the Fanforce Developers site to explore the full API access methods available.
|
data/lib/fanforce/main.rb
CHANGED
@@ -6,82 +6,89 @@ class Fanforce
|
|
6
6
|
include Fanforce::Utils
|
7
7
|
|
8
8
|
########################################################################
|
9
|
-
def initialize(
|
10
|
-
auth(
|
9
|
+
def initialize(auth_data=nil)
|
10
|
+
auth(auth_data) if is_present?(auth_data)
|
11
11
|
end
|
12
12
|
|
13
|
-
def get(path,
|
14
|
-
|
15
|
-
|
16
|
-
RestClient.get(
|
17
|
-
handle_response(response, request,
|
13
|
+
def get(path, params={})
|
14
|
+
url = complete_url(path)
|
15
|
+
params = apply_auth(params)
|
16
|
+
RestClient.get(url, {:params => params, :accept => :json}) do |response, request, result, &block|
|
17
|
+
handle_response(response, request, url, params)
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
def get_url(path, params={})
|
22
|
+
url = complete_url(path)
|
23
|
+
params = apply_auth(params)
|
24
|
+
"#{url}?#{to_query_string(params)}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def post(path, params={})
|
28
|
+
url = complete_url(path)
|
29
|
+
params = apply_auth(params)
|
30
|
+
RestClient.post(url, params, {:accept => :json}) do |response, request, result, &block|
|
31
|
+
handle_response(response, request, url, params)
|
26
32
|
end
|
27
33
|
end
|
28
34
|
|
29
|
-
def put(path,
|
30
|
-
|
31
|
-
|
32
|
-
RestClient.put(
|
33
|
-
handle_response(response, request,
|
35
|
+
def put(path, params={})
|
36
|
+
url = complete_url(path)
|
37
|
+
params = apply_auth(params)
|
38
|
+
RestClient.put(url, params, {:accept => :json}) do |response, request, result, &block|
|
39
|
+
handle_response(response, request, url, params)
|
34
40
|
end
|
35
41
|
end
|
36
42
|
|
37
|
-
def delete(path,
|
38
|
-
|
39
|
-
|
40
|
-
RestClient.delete(
|
41
|
-
handle_response(response, request,
|
43
|
+
def delete(path, params={})
|
44
|
+
url = complete_url(path)
|
45
|
+
params = apply_auth(url)
|
46
|
+
RestClient.delete(url, {:query => params, :accept => :json}) do |response, request, result, &block|
|
47
|
+
handle_response(response, request, url, params)
|
42
48
|
end
|
43
49
|
end
|
44
50
|
|
45
|
-
def handle_response(response, request,
|
51
|
+
def handle_response(response, request, url, params)
|
46
52
|
case response.code
|
47
53
|
when 200, 201
|
48
54
|
begin
|
49
55
|
response = decode_json(response)
|
50
56
|
rescue
|
51
|
-
raise UnknownError.new(response, request,
|
57
|
+
raise UnknownError.new(response, request, url, params)
|
52
58
|
end
|
53
59
|
when 400
|
54
|
-
raise BadRequestError.new(response, request,
|
60
|
+
raise BadRequestError.new(response, request, url, params)
|
55
61
|
when 403
|
56
|
-
raise ForbiddenError.new(response, request,
|
62
|
+
raise ForbiddenError.new(response, request, url, params)
|
57
63
|
when 404
|
58
|
-
raise NotFoundError.new(response, request,
|
64
|
+
raise NotFoundError.new(response, request, url, params)
|
59
65
|
when 422
|
60
|
-
raise UnprocessableEntityError.new(response, request,
|
66
|
+
raise UnprocessableEntityError.new(response, request, url, params)
|
61
67
|
else
|
62
|
-
raise UnknownError.new(response, request,
|
68
|
+
raise UnknownError.new(response, request, url, params)
|
63
69
|
end
|
64
70
|
response
|
65
71
|
end
|
66
72
|
|
67
|
-
def auth(
|
68
|
-
if
|
69
|
-
|
70
|
-
@auth_hash
|
71
|
-
@auth_hash[:
|
73
|
+
def auth(auth_data=nil)
|
74
|
+
if is_present?(auth_data)
|
75
|
+
auth_data = auth_data.is_a?(Hash) ? auth_data.symbolize_keys : {api_key: auth_data.to_s}
|
76
|
+
@auth_hash ||= {}
|
77
|
+
@auth_hash[:api_key] = auth_data[:api_key] if is_present?(auth_data[:api_key])
|
78
|
+
@auth_hash[:fanforce_id] = auth_data[:fanforce_id] if is_present?(auth_data[:fanforce_id])
|
72
79
|
end
|
73
80
|
@auth_hash
|
74
81
|
end
|
75
82
|
|
76
83
|
def valid_auth?
|
77
|
-
|
84
|
+
is_present?(@auth_hash) and is_present?(@auth_hash[:api_key])
|
78
85
|
end
|
79
86
|
|
80
87
|
def apply_auth(params)
|
81
88
|
params.merge(@auth_hash || {})
|
82
89
|
end
|
83
90
|
|
84
|
-
def
|
91
|
+
def complete_url(path)
|
85
92
|
'http://' + $API_DOMAIN + path
|
86
93
|
end
|
87
94
|
|
data/lib/fanforce/utils.rb
CHANGED
@@ -4,48 +4,48 @@ require 'query_string_parser'
|
|
4
4
|
module Fanforce::Utils
|
5
5
|
def self.included(base) base.extend(self) end
|
6
6
|
|
7
|
-
def
|
7
|
+
def is_blank?(obj)
|
8
8
|
obj.respond_to?(:empty?) ? obj.empty? : !obj
|
9
9
|
end
|
10
10
|
|
11
|
-
def
|
12
|
-
!
|
11
|
+
def is_present?(obj)
|
12
|
+
!is_blank?(obj)
|
13
13
|
end
|
14
14
|
|
15
15
|
def valid_request?(params)
|
16
16
|
return false if !params.is_a?(Hash)
|
17
|
-
return false if
|
18
|
-
return false if
|
17
|
+
return false if is_blank?(params[:fanforce_id])
|
18
|
+
return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id])
|
19
19
|
return true
|
20
20
|
end
|
21
21
|
|
22
22
|
def valid_install_request?(params)
|
23
23
|
return false if !params.is_a?(Hash)
|
24
|
-
return false if
|
25
|
-
return false if
|
26
|
-
return false if
|
24
|
+
return false if is_blank?(params[:fanforce_id])
|
25
|
+
return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id])
|
26
|
+
return false if is_blank?(params[:api_key])
|
27
27
|
return true
|
28
28
|
end
|
29
29
|
|
30
30
|
def valid_uninstall_request?(params)
|
31
31
|
return false if !params.is_a?(Hash)
|
32
|
-
return false if
|
33
|
-
return false if
|
34
|
-
return false if
|
32
|
+
return false if is_blank?(params[:fanforce_id])
|
33
|
+
return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id])
|
34
|
+
return false if is_blank?(params[:api_key])
|
35
35
|
return true
|
36
36
|
end
|
37
37
|
|
38
38
|
def parse_params(params)
|
39
|
-
if
|
39
|
+
if is_present?(params[:app_id])
|
40
40
|
plugin_type = :app
|
41
41
|
plugin_id = params[:app_id]
|
42
|
-
elsif
|
42
|
+
elsif is_present?(params[:behavior_id])
|
43
43
|
plugin_type = :behavior
|
44
44
|
plugin_id = params[:behavior_id]
|
45
|
-
elsif
|
45
|
+
elsif is_present?(params[:module_id])
|
46
46
|
plugin_type = :module
|
47
47
|
plugin_id = params[:module_id]
|
48
|
-
elsif
|
48
|
+
elsif is_present?(params[:widget_id])
|
49
49
|
plugin_type = :widget
|
50
50
|
plugin_id = params[:widget_id]
|
51
51
|
end
|
@@ -53,34 +53,47 @@ module Fanforce::Utils
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def parse_url(raw_url)
|
56
|
-
return if
|
56
|
+
return if is_blank?(raw_url)
|
57
57
|
url = URI::parse(raw_url)
|
58
58
|
query_params = QueryStringParser.qs_parse(url.query).inject({}) do |result, (k,v)|
|
59
59
|
result[k] = v if k !~ /^ff_.+/
|
60
60
|
result
|
61
61
|
end
|
62
|
-
query_string = to_query_string(Hash[query_params.sort]) if
|
62
|
+
query_string = to_query_string(Hash[query_params.sort]) if is_present?(query_params)
|
63
63
|
_external_id = url.host + url.path
|
64
|
-
clean_url = "#{url.scheme}://#{url.host}#{(if ![80,443].include?(url.port) then ":#{url.port}" end)}#{url.path}#{(if
|
64
|
+
clean_url = "#{url.scheme}://#{url.host}#{(if ![80,443].include?(url.port) then ":#{url.port}" end)}#{url.path}#{(if is_present?(query_string) then "?#{query_string}" end)}"
|
65
65
|
|
66
66
|
{ _external_id: _external_id, clean_url: clean_url, raw_url: raw_url, query_params: query_params, query_string: query_string, scheme: url.scheme, host: url.host, port: url.port, path: url.path, fragment: url.fragment }
|
67
67
|
end
|
68
68
|
|
69
|
-
def to_query_string(
|
70
|
-
if
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
69
|
+
def to_query_string(obj, namespace=nil)
|
70
|
+
return '' if is_blank?(obj)
|
71
|
+
if obj.is_a?(Array)
|
72
|
+
obj.collect { |value| to_query_string(value, "#{namespace}[]") }.join '&'
|
73
|
+
elsif obj.is_a?(Hash)
|
74
|
+
obj.collect { |key, value| to_query_string(value, namespace ? "#{namespace}[#{key}]" : key) }.sort * '&'
|
75
|
+
elsif obj.is_a?(Object)
|
76
|
+
require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
|
77
|
+
"#{CGI.escape(to_param(namespace))}=#{CGI.escape(to_param(obj).to_s)}"
|
76
78
|
else
|
77
|
-
raise "
|
79
|
+
raise "Argument must be an object, hash, or array; instead it was a #{obj.class}"
|
78
80
|
end
|
79
81
|
end
|
80
82
|
|
81
|
-
def
|
82
|
-
|
83
|
-
|
83
|
+
def to_param(obj, namespace=nil)
|
84
|
+
if obj.is_a?(Object)
|
85
|
+
obj.to_s
|
86
|
+
elsif obj.is_a?(Array)
|
87
|
+
collect { |e| e.to_param }.join '/'
|
88
|
+
elsif obj.is_a?(Hash)
|
89
|
+
to_query_string(obj, namespace)
|
90
|
+
elsif obj.is_a?(NilClass)
|
91
|
+
obj
|
92
|
+
elsif obj.is_a?(TrueClass)
|
93
|
+
obj
|
94
|
+
elsif obj.is_a?(FalseClass)
|
95
|
+
obj
|
96
|
+
end
|
84
97
|
end
|
85
98
|
|
86
99
|
def decode_json(str, symbolize_keys=true)
|
data/lib/fanforce/version.rb
CHANGED
data/test/fanforce_test.rb
CHANGED
@@ -7,12 +7,16 @@ require 'fanforce'
|
|
7
7
|
describe Fanforce do
|
8
8
|
|
9
9
|
it "should test if var is blank" do
|
10
|
-
assert Fanforce.
|
11
|
-
assert !Fanforce.
|
10
|
+
assert Fanforce.is_blank?(nil)
|
11
|
+
assert !Fanforce.is_blank?("value")
|
12
12
|
|
13
13
|
ff = Fanforce.new
|
14
|
-
assert ff.
|
15
|
-
assert !ff.
|
14
|
+
assert ff.is_blank?(nil)
|
15
|
+
assert !ff.is_blank?("value")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should create new instance of fanforce with api_key" do
|
19
|
+
Fanforce.new('13d90b01-6df7-4618-881c-c79320a0dc21')
|
16
20
|
end
|
17
21
|
|
18
22
|
end
|