fanforce 0.5.8 → 0.5.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -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(response, request, path, query_params)
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(response, request, path, query_params)
50
+ def initialize(request, response, path, query_params)
51
51
  @http_status = response.code
52
- super(response, request, path, query_params)
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 |response, request, result, &block|
29
- Fanforce::Response.new(response, request, complete_url(path), req_params)
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 |response, request, result, &block|
47
- Fanforce::Response.new(response, request, url, req_params)
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 |response, request, result, &block|
55
- Fanforce::Response.new(response, request, url, req_params)
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 |response, request, result, &block|
63
- Fanforce::Response.new(response, request, url, req_params)
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
 
@@ -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(response, request, url, req_params)
4
+ def initialize(request, response, url, req_params)
4
5
  case response.code
5
6
  when 200, 201
6
7
  begin
7
- @raw_response = Fanforce::Utils.decode_json(response)
8
- if !@raw_response[:results].nil?
9
- @results = @raw_response[:results]
10
- @current_results = @raw_response[:current_results]
11
- @total_results = @raw_response[:total_results]
12
- @current_page = @raw_response[:current_page]
13
- @total_pages = @raw_response[:total_pages]
14
- if request.method == :get and @raw_response[:total_pages] > @raw_response[:current_page]
15
- next_page = @raw_response[:current_page] + 1
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 @raw_response[:current_page] > 1
19
- prev_page = @raw_response[:current_page] - 1
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 = @raw_response
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(response, request, url, req_params)
33
+ raise Fanforce::BadRequestError.new(request, response, url, req_params)
30
34
  when 401
31
- raise Fanforce::Unauthorized.new(response, request, url, req_params)
35
+ raise Fanforce::Unauthorized.new(request, response, url, req_params)
32
36
  when 403
33
- raise Fanforce::ForbiddenError.new(response, request, url, req_params)
37
+ raise Fanforce::ForbiddenError.new(request, response, url, req_params)
34
38
  when 404
35
- raise Fanforce::NotFoundError.new(response, request, url, req_params)
39
+ raise Fanforce::NotFoundError.new(request, response, url, req_params)
36
40
  when 422
37
- raise Fanforce::UnprocessableEntityError.new(response, request, url, req_params)
41
+ raise Fanforce::UnprocessableEntityError.new(request, response, url, req_params)
38
42
  else
39
- raise Fanforce::UnknownError.new(response, request, url, req_params)
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
- @raw_response
53
+ @response_json
50
54
  end
51
55
 
52
56
  def [](key)
53
- @raw_response[key]
57
+ @response_json[key]
54
58
  end
55
59
 
56
60
  def result
@@ -1,3 +1,3 @@
1
1
  class Fanforce
2
- VERSION = '0.5.8'
2
+ VERSION = '0.5.9'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fanforce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.5.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: