fanforce 0.6.4 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDE3NjVjZjMyODBmY2MzYmM4MDRlOTg0ZDc5MmZkZmJmZWJjYzM4YQ==
4
+ YThmODJmMjE2M2EzZDRmYTBmNjY0YmJlNjM5MmU0MTg3YzA1YTBlOA==
5
5
  data.tar.gz: !binary |-
6
- OWJiMWE5OWIxMGQ5Njg4NjY1NWFkODQ1ZDBlZmE4YTY4YzE2ODQ3OQ==
6
+ YzJlZTljMTZkOWI3YzA3ZDgxNjlkYTM3NjEzOTdlYzNlNjVhODk5YQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NDZlMzU3ZDYxN2ZkZDI3ZmE0ZGE0ZjhkYTliNjMxMjJjMGViYmM1MmE5NGE5
10
- ZGVmOGMxNWYyYjNjYzQ0ZTRiZjUyYWM2ZWVmZTlkN2VlYTAwM2U0OTczZjdj
11
- NzY0Zjk2NDQzNmUyYWRmMWE3ZmRiODUxODg4YzdhNDc0NzAwNjg=
9
+ OTcxMmM5NWQ4NzNkZDZkMTM4YTAxYTVmYTU3YTMxODQzM2JlYTZkZmY0Mzc3
10
+ MDVhYjI1NjliOTA5YmQxNGZiYzc0YTI2MzEyMDJhMTlhYTYzMzg3NDc3MDAw
11
+ YjJhZjkwNmI0M2MwMzBhN2E5ZmYxMGYyNWQ5M2I1YTU0NGY3ZGM=
12
12
  data.tar.gz: !binary |-
13
- MzMwZjNmMGUyNjcyNmRiY2EwMTY1YTk0YmZiM2YwMmZkMTM1MmFjZWFlM2M3
14
- MThlODY4ZmVjNWU4NmNjMjhhOWU0YTYyNWU2MjllNjQxMDVhNDExMDk2YTA0
15
- ZDllNDg1MDdmMTJmN2IwOTVhZDQ2ZGFiMDgxNGRhNTkzOWNiYWU=
13
+ ZDA4NGQxZjRmNDdlMzMzYWE3NmE1OTUyMGIwNzAyYTA5NjJkZTkzNTY5ZTFh
14
+ MjlhNDc0YmUwYTkwNzQ5YzA1MDYxNDA3MTg2MTM4ZDFjYTYwZjFiNTc3NDcy
15
+ Y2UxMzFmNmU3M2Q4M2JmYTRlYjgzMmRlYjY1MGFkMTAxMDMzNzk=
@@ -28,7 +28,7 @@ module Fanforce::Api
28
28
  def get(path, req_params={})
29
29
  req_params = apply_auth(req_params)
30
30
  RestClient.get(url(path, req_params), :accept => :json) do |response, request, result, &block|
31
- Fanforce::Response.new(response, request, complete_url(path), req_params)
31
+ Fanforce::Response.process(response, request, complete_url(path), req_params)
32
32
  end
33
33
  end
34
34
 
@@ -46,7 +46,7 @@ module Fanforce::Api
46
46
  url = complete_url(path)
47
47
  req_params = apply_auth(req_params)
48
48
  RestClient.post(url, req_params.to_json, :content_type => :json, :accept => :json) do |response, request, result, &block|
49
- Fanforce::Api::Response.new(response, request, url, req_params)
49
+ Fanforce::Api::Response.process(response, request, url, req_params)
50
50
  end
51
51
  end
52
52
 
@@ -54,7 +54,7 @@ module Fanforce::Api
54
54
  url = complete_url(path)
55
55
  req_params = apply_auth(req_params)
56
56
  RestClient.put(url, req_params.to_json, :content_type => :json, :accept => :json) do |response, request, result, &block|
57
- Fanforce::Api::Response.new(response, request, url, req_params)
57
+ Fanforce::Api::Response.process(response, request, url, req_params)
58
58
  end
59
59
  end
60
60
 
@@ -62,7 +62,7 @@ module Fanforce::Api
62
62
  url = complete_url(path)
63
63
  req_params = apply_auth(req_params)
64
64
  RestClient.delete(url, {:params => to_query_string(req_params), :accept => :json}) do |response, request, result, &block|
65
- Fanforce::Api::Response.new(response, request, url, req_params)
65
+ Fanforce::Api::Response.process(response, request, url, req_params)
66
66
  end
67
67
  end
68
68
 
@@ -1,87 +1,103 @@
1
1
  class Fanforce::Api::Response
2
2
  attr_reader :curl_command, :requested_url, :requested_params
3
3
 
4
- def initialize(response, request, requested_url, requested_params)
4
+ def self.process(response, request, requested_url, requested_params)
5
+ raise Fanforce::Api::ServerError.new(response, request, requested_url, requested_params) if response.code > 201
6
+ begin response_hash = Fanforce::Utils.decode_json(response)
7
+ rescue Exception => e; raise Fanforce::Api::DecodingError.new(e, response, request, requested_url, requested_params) end
8
+ if response_hash[:results]
9
+ Fanforce::Api::Results.new(response_hash, response, request, requested_url, requested_params)
10
+ else
11
+ Fanforce::Api::Result.new(response_hash, response, request, requested_url, requested_params)
12
+ end
13
+ end
14
+
15
+ end
16
+
17
+ class Fanforce::Api::Result < Hash
18
+ attr_reader :requested_url, :requested_params
19
+
20
+ def initialize(response_hash, response, request, requested_url, requested_params)
5
21
  @response = response
6
22
  @request = request
7
23
  @requested_url = requested_url
8
24
  @requested_params = requested_params
9
- raise Fanforce::Api::ServerError.new(response, request, requested_url, requested_params) if response.code > 201
10
-
11
- begin
12
- @response_hash = Fanforce::Utils.decode_json(response)
13
- rescue Exception => e
14
- raise Fanforce::Api::DecodingError.new(e, response, request, requested_url, requested_params)
15
- end
16
-
17
- @response_body = response.to_s
18
- @curl_command = Fanforce::Utils.curl_command(request.method, requested_url, requested_params)
25
+ super(response_hash)
26
+ end
19
27
 
20
- if @response_hash[:results].nil?
21
- @result = @response_hash
22
- else
23
- @results = @response_hash[:results]
24
- @current_results = @response_hash[:current_results]
25
- @total_results = @response_hash[:total_results]
26
- @current_page = @response_hash[:current_page]
27
- @total_pages = @response_hash[:total_pages]
28
- if request.method == :get and @response_hash[:total_pages] > @response_hash[:current_page]
29
- next_page = @response_hash[:current_page] + 1
30
- @next_url = "#{requested_url}?#{Fanforce::Utils.to_query_string(requested_params.merge(page: next_page))}"
31
- end
32
- if request.method == :get and @response_hash[:current_page] > 1
33
- prev_page = @response_hash[:current_page] - 1
34
- @prev_url = "#{requested_url}?#{Fanforce::Utils.to_query_string(requested_params.merge(page: prev_page))}"
35
- end
36
- end
28
+ def data
29
+ self
37
30
  end
31
+ alias result data
38
32
 
39
- def code
40
- @response.code
33
+ def curl_command
34
+ @curl_command ||= Fanforce::Utils.curl_command(@request.method, @requested_url, @requested_params)
41
35
  end
42
36
 
43
- def to_hash
44
- @response_hash
37
+ def body;
38
+ @response.to_s
45
39
  end
46
40
 
47
- def to_s
48
- @response_body
41
+ def current_results; 1 end
42
+ def total_results; 1 end
43
+ def current_page; 1 end
44
+ def total_pages; 1 end
45
+ def prev_url; nil end
46
+ def next_url; nil end
47
+ def code; @response.code end
48
+ end
49
+
50
+ class Fanforce::Api::Results < Array
51
+ attr_reader :requested_url, :requested_params
52
+
53
+ def initialize(response_hash, response, request, requested_url, requested_params)
54
+ @response = response
55
+ @request = request
56
+ @requested_url = requested_url
57
+ @requested_params = requested_params
58
+ super(response_hash[:results])
49
59
  end
50
60
 
51
- def [](key)
52
- @response_hash[key]
61
+ def data
62
+ self
53
63
  end
64
+ alias results data
54
65
 
55
- def result
56
- @result || (raise NoMethodError, 'This API response returns more than 1 record. Try using "results".')
66
+ def curl_command
67
+ @curl_command ||= Fanforce::Utils.curl_command(@request.method, @requested_url, @requested_params)
57
68
  end
58
69
 
59
- def results
60
- @results || (raise NoMethodError, 'This API response only returns 1 record. Try using "result".')
70
+ def body;
71
+ @response.to_s
61
72
  end
62
73
 
63
74
  def current_results
64
- @current_results || 1
75
+ @current_results ||= @response_hash[:current_results]
65
76
  end
66
77
 
67
- def total_results
68
- @total_results || 1
78
+ def total_results;
79
+ @total_results ||= @response_hash[:total_results]
69
80
  end
70
81
 
71
- def current_page
72
- @current_page || 1
82
+ def current_page;
83
+ @current_page ||= @response_hash[:current_page]
73
84
  end
74
85
 
75
- def total_pages
76
- @total_pages || 1
86
+ def total_pages;
87
+ @total_pages ||= @response_hash[:total_pages]
77
88
  end
78
89
 
79
- def prev_url
80
- @prev_url || nil
90
+ def prev_url;
91
+ @prev_url ||= if request.method == :get and current_page > 1
92
+ "#{@requested_url}?#{Fanforce::Utils.to_query_string(@requested_params.merge(page: current_page-1))}"
93
+ end
81
94
  end
82
95
 
83
- def next_url
84
- @next_url || nil
96
+ def next_url;
97
+ @next_url ||= if @request.method == :get and total_pages > current_page
98
+ "#{@requested_url}?#{Fanforce::Utils.to_query_string(@requested_params.merge(page: current_page+1))}"
99
+ end
85
100
  end
101
+ def code; @response.code end
86
102
 
87
- end
103
+ end
@@ -1,5 +1,5 @@
1
1
  class Fanforce
2
2
  module Api
3
- VERSION = '0.6.4'
3
+ VERSION = '0.7.0'
4
4
  end
5
5
  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.6.4
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Clark