fanforce 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -84,12 +84,50 @@ BadRequestError
84
84
 
85
85
  ## Utils
86
86
 
87
- ff.curl_command(method, path, query_params)
87
+ ff.get_url(path, query_params)
88
88
 
89
- ff.url(path, query_params)
89
+ ff.curl_command(method, path, query_params)
90
90
 
91
91
  ff.validate_auth
92
92
 
93
+ ff.to_query_string
94
+
95
+ ff.compile_jquery_tmpls
96
+
97
+ ff.decode_json
98
+
99
+ ff.parse_url
100
+
101
+ ff.remove_sensitive_params
102
+
103
+ ff.remove_internal_params
104
+
105
+ ## Handling Results
106
+
107
+ response.result
108
+ response.results
109
+
110
+ response.total_results
111
+ response.current_results
112
+ response.current_page
113
+ response.total_pages
114
+
115
+ ## Paging
116
+
117
+ response.prev_url
118
+ response.next_url
119
+
120
+ ## Logging
121
+
122
+ By default logging is turned off.
123
+ ```ruby
124
+ Fanforce.config do |config|
125
+ config.logging = true
126
+ end
127
+ ```
128
+
129
+ For more info, see RestClient.logging.
130
+
93
131
  ## More Information
94
132
 
95
133
  Visit the Fanforce Developers site to explore the full API access methods available.
@@ -13,7 +13,7 @@ class Fanforce
13
13
  rescue Exception => e
14
14
  @response = "Error decoding JSON in Fanforce gem: #{e.message}"
15
15
  end
16
- @request_url = "#{@path}?#{Fanforce::Utils.to_param(@query_params)}"
16
+ @request_url = "#{@path}?#{Fanforce::Utils.to_query_string(@query_params)}"
17
17
  super(@response.is_a?(Hash) ? @response[:msg] : @response)
18
18
  end
19
19
 
data/lib/fanforce/main.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require_relative 'errors'
2
2
  require_relative 'utils'
3
+ require_relative 'response'
3
4
  require 'rest-client'
4
5
 
5
6
  class Fanforce
@@ -23,10 +24,9 @@ class Fanforce
23
24
  end
24
25
 
25
26
  def get(path, req_params={})
26
- url = complete_url(path)
27
27
  req_params = apply_auth(req_params)
28
- RestClient.get(url, {:params => req_params, :accept => :json}) do |response, request, result, &block|
29
- handle_response(response, request, url, 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)
30
30
  end
31
31
  end
32
32
 
@@ -44,7 +44,7 @@ class Fanforce
44
44
  url = complete_url(path)
45
45
  req_params = apply_auth(req_params)
46
46
  RestClient.post(url, req_params.to_json, :content_type => :json, :accept => :json) do |response, request, result, &block|
47
- handle_response(response, request, url, req_params)
47
+ Fanforce::Response.new(response, request, url, req_params)
48
48
  end
49
49
  end
50
50
 
@@ -52,15 +52,15 @@ class Fanforce
52
52
  url = complete_url(path)
53
53
  req_params = apply_auth(req_params)
54
54
  RestClient.put(url, req_params.to_json, :content_type => :json, :accept => :json) do |response, request, result, &block|
55
- handle_response(response, request, url, req_params)
55
+ Fanforce::Response.new(response, request, 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 => req_params, :accept => :json}) do |response, request, result, &block|
63
- handle_response(response, request, url, 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)
64
64
  end
65
65
  end
66
66
 
@@ -74,32 +74,6 @@ class Fanforce
74
74
  {success: true}
75
75
  end
76
76
 
77
- def handle_response(response, request, url, req_params)
78
- log request.url
79
- log request.payload.to_json
80
- case response.code
81
- when 200, 201
82
- begin
83
- response = decode_json(response)
84
- rescue
85
- raise UnknownError.new(response, request, url, req_params)
86
- end
87
- when 400
88
- raise BadRequestError.new(response, request, url, req_params)
89
- when 401
90
- raise Unauthorized.new(response, request, url, req_params)
91
- when 403
92
- raise ForbiddenError.new(response, request, url, req_params)
93
- when 404
94
- raise NotFoundError.new(response, request, url, req_params)
95
- when 422
96
- raise UnprocessableEntityError.new(response, request, url, req_params)
97
- else
98
- raise UnknownError.new(response, request, url, req_params)
99
- end
100
- response
101
- end
102
-
103
77
  def auth(auth_data=nil)
104
78
  @auth_hash ||= {}
105
79
  return @auth_hash if is_blank?(auth_data)
@@ -0,0 +1,88 @@
1
+ class Fanforce::Response
2
+
3
+ def initialize(response, request, url, req_params)
4
+ case response.code
5
+ when 200, 201
6
+ 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
16
+ @next_url = "#{url}?#{Fanforce::Utils.to_query_string(req_params.merge(page: next_page))}"
17
+ end
18
+ if request.method == :get and @raw_response[:current_page] > 1
19
+ prev_page = @raw_response[:current_page] - 1
20
+ @prev_url = "#{url}?#{Fanforce::Utils.to_query_string(req_params.merge(page: prev_page))}"
21
+ end
22
+ else
23
+ @result = raw_json
24
+ end
25
+ rescue
26
+ raise Fanforce::UnknownError.new(response, request, url, req_params)
27
+ end
28
+ when 400
29
+ raise Fanforce::BadRequestError.new(response, request, url, req_params)
30
+ when 401
31
+ raise Fanforce::Unauthorized.new(response, request, url, req_params)
32
+ when 403
33
+ raise Fanforce::ForbiddenError.new(response, request, url, req_params)
34
+ when 404
35
+ raise Fanforce::NotFoundError.new(response, request, url, req_params)
36
+ when 422
37
+ raise Fanforce::UnprocessableEntityError.new(response, request, url, req_params)
38
+ else
39
+ raise Fanforce::UnknownError.new(response, request, url, req_params)
40
+ end
41
+ response
42
+ end
43
+
44
+ def to_json
45
+ to_hash.to_json
46
+ end
47
+
48
+ def to_hash
49
+ @raw_response
50
+ end
51
+
52
+ def [](key)
53
+ @raw_response[key]
54
+ end
55
+
56
+ def result
57
+ @result || (raise NoMethodError, 'This API response returns more than 1 record. Try using "results".')
58
+ end
59
+
60
+ def results
61
+ @results || (raise NoMethodError, 'This API response only returns 1 record. Try using "result".')
62
+ end
63
+
64
+ def total_results
65
+ @total_results || 1
66
+ end
67
+
68
+ def current_results
69
+ @current_results || 1
70
+ end
71
+
72
+ def current_page
73
+ @current_page || 1
74
+ end
75
+
76
+ def total_pages
77
+ @total_pages || 1
78
+ end
79
+
80
+ def prev_url
81
+ @prev_url || nil
82
+ end
83
+
84
+ def next_url
85
+ @next_url || nil
86
+ end
87
+
88
+ end
@@ -106,7 +106,7 @@ module Fanforce::Utils
106
106
 
107
107
  def to_param(obj, namespace=nil)
108
108
  if obj.is_a?(Array)
109
- collect { |e| e.to_param }.join '/'
109
+ collect { |e| to_param(e) }.join '/'
110
110
  elsif obj.is_a?(Hash)
111
111
  to_query_string(obj, namespace)
112
112
  elsif obj.is_a?(NilClass)
@@ -121,7 +121,7 @@ module Fanforce::Utils
121
121
  end
122
122
 
123
123
  def log(str)
124
- puts "Fanforce: #{str}"
124
+ #puts "Fanforce: #{str}"
125
125
  end
126
126
 
127
127
  def decode_json(str, symbolize_keys=true)
@@ -155,13 +155,13 @@ module Fanforce::Utils
155
155
  def curl_command(method, url, req_params)
156
156
  case method
157
157
  when :get
158
- "curl \"#{url}?#{to_param(req_params)}\""
158
+ "curl \"#{url}?#{to_query_string(req_params)}\""
159
159
  when :post
160
- "curl -X POST -d \"#{to_param(req_params)}\" #{url}"
160
+ "curl -X POST -d \"#{to_query_string(req_params)}\" #{url}"
161
161
  when :put
162
- "curl -X PUT -d \"#{to_param(req_params)}\" #{url.to_json}"
162
+ "curl -X PUT -d \"#{to_query_string(req_params)}\" #{url.to_json}"
163
163
  when :delete
164
- "curl --request DELETE \"#{url}?#{to_param(req_params)}\""
164
+ "curl --request DELETE \"#{url}?#{to_query_string(req_params)}\""
165
165
  else
166
166
  raise 'Unknown request method'
167
167
  end
@@ -1,3 +1,3 @@
1
1
  class Fanforce
2
- VERSION = '0.5.2'
2
+ VERSION = '0.5.3'
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.2
4
+ version: 0.5.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -94,6 +94,7 @@ files:
94
94
  - lib/fanforce/config.rb
95
95
  - lib/fanforce/errors.rb
96
96
  - lib/fanforce/main.rb
97
+ - lib/fanforce/response.rb
97
98
  - lib/fanforce/utils.rb
98
99
  - lib/fanforce/version.rb
99
100
  - lib/ff_globals.rb