fanforce 0.5.8 → 0.5.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/fanforce/errors.rb +3 -3
- data/lib/fanforce/main.rb +8 -8
- data/lib/fanforce/response.rb +25 -21
- data/lib/fanforce/version.rb +1 -1
- metadata +1 -1
data/lib/fanforce/errors.rb
CHANGED
@@ -4,7 +4,7 @@ class Fanforce
|
|
4
4
|
class Error < StandardError
|
5
5
|
attr_accessor :path, :query_params, :request, :response, :request_url, :http_status
|
6
6
|
|
7
|
-
def initialize(
|
7
|
+
def initialize(request, response, path, query_params)
|
8
8
|
@query_params = query_params
|
9
9
|
@path = path
|
10
10
|
@request = request
|
@@ -47,9 +47,9 @@ class Fanforce
|
|
47
47
|
|
48
48
|
class UnknownError < Error
|
49
49
|
def code; @http_status ||= 500; 500 end
|
50
|
-
def initialize(
|
50
|
+
def initialize(request, response, path, query_params)
|
51
51
|
@http_status = response.code
|
52
|
-
super(
|
52
|
+
super(request, response, path, query_params)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
end
|
data/lib/fanforce/main.rb
CHANGED
@@ -25,8 +25,8 @@ class Fanforce
|
|
25
25
|
|
26
26
|
def get(path, req_params={})
|
27
27
|
req_params = apply_auth(req_params)
|
28
|
-
RestClient.get(url(path, req_params), :accept => :json) do |
|
29
|
-
Fanforce::Response.new(
|
28
|
+
RestClient.get(url(path, req_params), :accept => :json) do |request, response, result, &block|
|
29
|
+
Fanforce::Response.new(request, response, complete_url(path), req_params)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
@@ -43,24 +43,24 @@ class Fanforce
|
|
43
43
|
def post(path, req_params={})
|
44
44
|
url = complete_url(path)
|
45
45
|
req_params = apply_auth(req_params)
|
46
|
-
RestClient.post(url, req_params.to_json, :content_type => :json, :accept => :json) do |
|
47
|
-
Fanforce::Response.new(
|
46
|
+
RestClient.post(url, req_params.to_json, :content_type => :json, :accept => :json) do |request, response, result, &block|
|
47
|
+
Fanforce::Response.new(request, response, url, req_params)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
51
|
def put(path, req_params={})
|
52
52
|
url = complete_url(path)
|
53
53
|
req_params = apply_auth(req_params)
|
54
|
-
RestClient.put(url, req_params.to_json, :content_type => :json, :accept => :json) do |
|
55
|
-
Fanforce::Response.new(
|
54
|
+
RestClient.put(url, req_params.to_json, :content_type => :json, :accept => :json) do |request, response, result, &block|
|
55
|
+
Fanforce::Response.new(request, response, url, req_params)
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
59
|
def delete(path, req_params={})
|
60
60
|
url = complete_url(path)
|
61
61
|
req_params = apply_auth(req_params)
|
62
|
-
RestClient.delete(url, {:params => to_query_string(req_params), :accept => :json}) do |
|
63
|
-
Fanforce::Response.new(
|
62
|
+
RestClient.delete(url, {:params => to_query_string(req_params), :accept => :json}) do |request, response, result, &block|
|
63
|
+
Fanforce::Response.new(request, response, url, req_params)
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
data/lib/fanforce/response.rb
CHANGED
@@ -1,42 +1,46 @@
|
|
1
1
|
class Fanforce::Response
|
2
|
+
attr_reader :curl_command, :request_url, :response_body, :response_json
|
2
3
|
|
3
|
-
def initialize(
|
4
|
+
def initialize(request, response, url, req_params)
|
4
5
|
case response.code
|
5
6
|
when 200, 201
|
6
7
|
begin
|
7
|
-
@
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@
|
13
|
-
@
|
14
|
-
|
15
|
-
|
8
|
+
@response_body = response.to_s
|
9
|
+
@response_json = Fanforce::Utils.decode_json(response)
|
10
|
+
@curl_command = Fanforce::Utils.curl_command(request.method, url, req_params)
|
11
|
+
@request_url = url
|
12
|
+
if !@response_json[:results].nil?
|
13
|
+
@results = @response_json[:results]
|
14
|
+
@current_results = @response_json[:current_results]
|
15
|
+
@total_results = @response_json[:total_results]
|
16
|
+
@current_page = @response_json[:current_page]
|
17
|
+
@total_pages = @response_json[:total_pages]
|
18
|
+
if request.method == :get and @response_json[:total_pages] > @response_json[:current_page]
|
19
|
+
next_page = @response_json[:current_page] + 1
|
16
20
|
@next_url = "#{url}?#{Fanforce::Utils.to_query_string(req_params.merge(page: next_page))}"
|
17
21
|
end
|
18
|
-
if request.method == :get and @
|
19
|
-
prev_page = @
|
22
|
+
if request.method == :get and @response_json[:current_page] > 1
|
23
|
+
prev_page = @response_json[:current_page] - 1
|
20
24
|
@prev_url = "#{url}?#{Fanforce::Utils.to_query_string(req_params.merge(page: prev_page))}"
|
21
25
|
end
|
22
26
|
else
|
23
|
-
@result = @
|
27
|
+
@result = @response_json
|
24
28
|
end
|
25
29
|
rescue
|
26
30
|
raise
|
27
31
|
end
|
28
32
|
when 400
|
29
|
-
raise Fanforce::BadRequestError.new(
|
33
|
+
raise Fanforce::BadRequestError.new(request, response, url, req_params)
|
30
34
|
when 401
|
31
|
-
raise Fanforce::Unauthorized.new(
|
35
|
+
raise Fanforce::Unauthorized.new(request, response, url, req_params)
|
32
36
|
when 403
|
33
|
-
raise Fanforce::ForbiddenError.new(
|
37
|
+
raise Fanforce::ForbiddenError.new(request, response, url, req_params)
|
34
38
|
when 404
|
35
|
-
raise Fanforce::NotFoundError.new(
|
39
|
+
raise Fanforce::NotFoundError.new(request, response, url, req_params)
|
36
40
|
when 422
|
37
|
-
raise Fanforce::UnprocessableEntityError.new(
|
41
|
+
raise Fanforce::UnprocessableEntityError.new(request, response, url, req_params)
|
38
42
|
else
|
39
|
-
raise Fanforce::UnknownError.new(
|
43
|
+
raise Fanforce::UnknownError.new(request, response, url, req_params)
|
40
44
|
end
|
41
45
|
response
|
42
46
|
end
|
@@ -46,11 +50,11 @@ class Fanforce::Response
|
|
46
50
|
end
|
47
51
|
|
48
52
|
def to_hash
|
49
|
-
@
|
53
|
+
@response_json
|
50
54
|
end
|
51
55
|
|
52
56
|
def [](key)
|
53
|
-
@
|
57
|
+
@response_json[key]
|
54
58
|
end
|
55
59
|
|
56
60
|
def result
|
data/lib/fanforce/version.rb
CHANGED