fanforce 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +16 -4
- data/lib/fanforce/errors.rb +31 -25
- data/lib/fanforce/main.rb +7 -17
- data/lib/fanforce/utils.rb +17 -1
- data/lib/fanforce/version.rb +1 -1
- data/lib/fanforce.rb +5 -4
- metadata +1 -1
data/README.md
CHANGED
@@ -28,14 +28,14 @@ require 'fanforce'
|
|
28
28
|
api_key = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
29
29
|
|
30
30
|
# set up a client to talk to the Fanforce API
|
31
|
-
|
31
|
+
ff = Fanforce.new api_key
|
32
32
|
```
|
33
33
|
|
34
34
|
### Making Your First Call
|
35
35
|
|
36
36
|
``` ruby
|
37
37
|
# get your api access info
|
38
|
-
|
38
|
+
ff.get('/access_info', {})
|
39
39
|
```
|
40
40
|
|
41
41
|
## Full REST Access
|
@@ -66,11 +66,13 @@ ff.delete('/')
|
|
66
66
|
|
67
67
|
## Error Handling
|
68
68
|
|
69
|
+
```ruby
|
69
70
|
begin
|
70
|
-
|
71
|
+
ff.get('/bad_page')
|
71
72
|
rescue Fanforce::Error => e
|
72
|
-
|
73
|
+
puts e.curl_command
|
73
74
|
end
|
75
|
+
```
|
74
76
|
|
75
77
|
e.curl_command
|
76
78
|
e.response_code
|
@@ -78,6 +80,16 @@ e.response_body
|
|
78
80
|
e.request_url
|
79
81
|
e.request_params
|
80
82
|
|
83
|
+
BadRequestError
|
84
|
+
|
85
|
+
## Utils
|
86
|
+
|
87
|
+
ff.curl_command(method, path, query_params)
|
88
|
+
|
89
|
+
ff.url(path, query_params)
|
90
|
+
|
91
|
+
ff.validate_auth
|
92
|
+
|
81
93
|
## More Information
|
82
94
|
|
83
95
|
Visit the Fanforce Developers site to explore the full API access methods available.
|
data/lib/fanforce/errors.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
require_relative 'utils'
|
2
2
|
|
3
3
|
class Fanforce
|
4
|
-
class Error < StandardError
|
5
|
-
|
6
|
-
attr_accessor :path, :query_params, :request, :response, :request_url
|
4
|
+
class Error < StandardError
|
5
|
+
attr_accessor :path, :query_params, :request, :response, :request_url, :http_status
|
7
6
|
|
8
7
|
def initialize(response, request, path, query_params)
|
9
8
|
@query_params = query_params
|
@@ -12,38 +11,45 @@ class Fanforce
|
|
12
11
|
begin
|
13
12
|
@response = MultiJson.load(response, :symbolize_keys => true)
|
14
13
|
rescue Exception => e
|
15
|
-
@response = "
|
14
|
+
@response = "Error decoding JSON in Fanforce gem: #{e.message}"
|
16
15
|
end
|
17
|
-
@request_url = "#{@path}?#{to_param(@query_params)}"
|
16
|
+
@request_url = "#{@path}?#{Fanforce::Utils.to_param(@query_params)}"
|
18
17
|
super(@response.is_a?(Hash) ? @response[:msg] : @response)
|
19
18
|
end
|
20
19
|
|
21
20
|
def curl_command
|
22
21
|
method = begin @request.method rescue nil end
|
23
|
-
|
24
|
-
when :get
|
25
|
-
"curl \"#{@path}?#{to_param(@query_params)}\""
|
26
|
-
when :post
|
27
|
-
"curl -X POST -d \"#{to_param(@query_params)}\" #{@path}"
|
28
|
-
when :put
|
29
|
-
"curl -X PUT -d \"#{to_param(@query_params)}\" #{@path.to_json}"
|
30
|
-
when :delete
|
31
|
-
"curl --request DELETE \"#{@path}?#{to_param(@query_params)}\""
|
32
|
-
else
|
33
|
-
"Unknown request method so used POST: curl -X POST -d \"#{to_param(@query_params)}\" #{@path}"
|
34
|
-
end
|
22
|
+
Fanforce::Utils.curl_command(method, @path, @query_params)
|
35
23
|
end
|
36
|
-
|
24
|
+
|
25
|
+
def code; end
|
26
|
+
end
|
27
|
+
|
28
|
+
class BadRequestError < Error
|
29
|
+
def code; @http_status = 400 end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Unauthorized < Error
|
33
|
+
def code; @http_status = 401 end
|
34
|
+
end
|
35
|
+
|
36
|
+
class ForbiddenError < Error
|
37
|
+
def code; @http_status = 403 end
|
38
|
+
end
|
39
|
+
|
40
|
+
class NotFoundError < Error
|
41
|
+
def code; @http_status = 404 end
|
42
|
+
end
|
43
|
+
|
44
|
+
class UnprocessableEntityError < Error
|
45
|
+
def code; @http_status = 422 end
|
37
46
|
end
|
38
47
|
|
39
|
-
class
|
40
|
-
|
41
|
-
class NotFoundError < Error; def code() 404 end end
|
42
|
-
class UnprocessableEntityError < Error; def code() 422 end end
|
43
|
-
class UnknownError < Error;
|
44
|
-
def code() @code ||= 500 end
|
48
|
+
class UnknownError < Error
|
49
|
+
def code; @http_status ||= 500; 500 end
|
45
50
|
def initialize(response, request, path, query_params)
|
46
|
-
@
|
51
|
+
@http_status = response.code
|
52
|
+
super(response, request, path, query_params)
|
47
53
|
end
|
48
54
|
end
|
49
55
|
end
|
data/lib/fanforce/main.rb
CHANGED
@@ -5,6 +5,7 @@ require 'rest-client'
|
|
5
5
|
class Fanforce
|
6
6
|
include Fanforce::Utils
|
7
7
|
########################################################################
|
8
|
+
|
8
9
|
def initialize(arg={})
|
9
10
|
if arg.is_a?(Hash)
|
10
11
|
add_params(arg)
|
@@ -30,27 +31,14 @@ class Fanforce
|
|
30
31
|
end
|
31
32
|
end
|
32
33
|
|
33
|
-
def
|
34
|
-
url = complete_url(path)
|
34
|
+
def url(path, req_params={})
|
35
35
|
req_params = apply_auth(req_params)
|
36
|
-
"#{
|
36
|
+
"#{complete_url(path)}?#{to_query_string(req_params)}"
|
37
37
|
end
|
38
38
|
|
39
|
-
def
|
39
|
+
def curl_command(method, path, req_params={})
|
40
40
|
req_params = apply_auth(req_params)
|
41
|
-
|
42
|
-
case method
|
43
|
-
when :get
|
44
|
-
"curl \"#{url}?#{to_param(req_params)}\""
|
45
|
-
when :post
|
46
|
-
"curl -X POST -d \"#{to_param(req_params)}\" #{url}"
|
47
|
-
when :put
|
48
|
-
"curl -X PUT -d \"#{to_param(req_params)}\" #{url.to_json}"
|
49
|
-
when :delete
|
50
|
-
"curl --request DELETE \"#{url}?#{to_param(req_params)}\""
|
51
|
-
else
|
52
|
-
raise 'Unknown request method'
|
53
|
-
end
|
41
|
+
Fanforce::Utils.curl_command(method, complete_url(path), req_params)
|
54
42
|
end
|
55
43
|
|
56
44
|
def post(path, req_params={})
|
@@ -97,6 +85,8 @@ class Fanforce
|
|
97
85
|
end
|
98
86
|
when 400
|
99
87
|
raise BadRequestError.new(response, request, url, req_params)
|
88
|
+
when 401
|
89
|
+
raise Unauthorized.new(response, request, url, req_params)
|
100
90
|
when 403
|
101
91
|
raise ForbiddenError.new(response, request, url, req_params)
|
102
92
|
when 404
|
data/lib/fanforce/utils.rb
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
require 'uri'
|
2
2
|
require 'rack/utils'
|
3
|
+
require 'forwardable'
|
3
4
|
|
4
5
|
module Fanforce::Utils
|
5
|
-
|
6
|
+
extend Fanforce::Utils
|
6
7
|
|
7
8
|
def is_blank?(obj)
|
8
9
|
obj.respond_to?(:empty?) ? obj.empty? : !obj
|
@@ -144,4 +145,19 @@ module Fanforce::Utils
|
|
144
145
|
end
|
145
146
|
end
|
146
147
|
|
148
|
+
def curl_command(method, url, req_params)
|
149
|
+
case method
|
150
|
+
when :get
|
151
|
+
"curl \"#{url}?#{to_param(req_params)}\""
|
152
|
+
when :post
|
153
|
+
"curl -X POST -d \"#{to_param(req_params)}\" #{url}"
|
154
|
+
when :put
|
155
|
+
"curl -X PUT -d \"#{to_param(req_params)}\" #{url.to_json}"
|
156
|
+
when :delete
|
157
|
+
"curl --request DELETE \"#{url}?#{to_param(req_params)}\""
|
158
|
+
else
|
159
|
+
raise 'Unknown request method'
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
147
163
|
end
|
data/lib/fanforce/version.rb
CHANGED
data/lib/fanforce.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
class Fanforce
|
2
|
+
require_relative 'ff_globals'
|
3
|
+
require_relative 'fanforce/version'
|
4
|
+
require_relative 'fanforce/config'
|
5
|
+
require_relative 'fanforce/main'
|
2
6
|
end
|
3
|
-
|
4
|
-
require_relative 'fanforce/version'
|
5
|
-
require_relative 'fanforce/config'
|
6
|
-
require_relative 'fanforce/main'
|
7
|
+
|