fanforce 0.6.1 → 0.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/lib/fanforce/api/error.rb +51 -9
- data/lib/fanforce/api/response.rb +43 -33
- data/lib/fanforce/api/utils.rb +0 -33
- data/lib/fanforce/api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NTBmNDMzZTVhYzQwNDc3YzNkNTBkYzcyMDNmYjM3ZGYxMDY0YTI1Mg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZDY5NmM5MjllNjAzYzM5ODYxYmZiZmJlNjA1OTEyMWMyMTc1NjBjMQ==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MzFjZjQ2NGJlMDQ2Y2Q0MjFlZmUzZDdhNzMyNDJiMTI5MmYzOGVmNzZjYTc1
|
10
|
+
NTZhMDk1YTY2OTA1MjgxNDQxMTNmZjQ2NTcwMDZhZjI1MDVjZmU4YjUyMGZj
|
11
|
+
YTM2Zjg1MTQ2YjQ1MjE3Njk3ZWIxNDdkM2FlZmEyMTRmZGJlYzE=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Y2M5ZWQ3N2Q0ZjE1OWE3Y2IzNDMyNWVmYmU0YWEwYTk3OGQ3OWFmZTY2ODQ5
|
14
|
+
NTkyMDk0ZjVkNDQxMGVkMDFjOTVhOTRhNGQ4ZjJiYzVhYzYyMGZlMDI2YWIz
|
15
|
+
NzkxODk1MjA4MjFjZTZhOGI4NjMwYjI0ZDNlYjhjODU4N2FmZjM=
|
data/lib/fanforce/api/error.rb
CHANGED
@@ -1,26 +1,68 @@
|
|
1
1
|
require_relative 'utils'
|
2
2
|
|
3
3
|
class Fanforce::Api::Error < StandardError
|
4
|
-
attr_accessor :
|
4
|
+
attr_accessor :response, :request, :requested_url, :requested_params, :errors
|
5
5
|
|
6
|
-
def initialize(response, request,
|
7
|
-
@
|
8
|
-
@path = path
|
6
|
+
def initialize(message, response, request, requested_url, requested_params)
|
7
|
+
@response = response
|
9
8
|
@request = request
|
9
|
+
@requested_url = requested_url
|
10
|
+
@requested_params = requested_params
|
11
|
+
@message = message
|
12
|
+
super(message)
|
13
|
+
end
|
14
|
+
|
15
|
+
def curl_command
|
16
|
+
method = begin @request.method rescue nil end
|
17
|
+
Fanforce::Utils.curl_command(method, @requested_url, @requested_params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def code; @response.respond_to?(:code) ? @response.code : 500 end
|
21
|
+
def to_s; @message end
|
22
|
+
def to_hash; {} end
|
23
|
+
def to_json; to_hash.to_json end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Fanforce::Api::ServerError < Fanforce::Api::Error
|
27
|
+
|
28
|
+
def initialize(response, request, requested_url, requested_params)
|
29
|
+
@response_body = response.to_s
|
30
|
+
|
10
31
|
begin
|
11
|
-
@
|
32
|
+
@response_hash = Fanforce::Utils.decode_json(response)
|
33
|
+
@errors = @response_hash[:errors]
|
12
34
|
rescue Exception => e
|
13
|
-
|
35
|
+
raise Fanforce::Api::DecodingError.new(e, response, request, requested_url, requested_params)
|
14
36
|
end
|
15
|
-
|
16
|
-
super(
|
37
|
+
|
38
|
+
super(response.to_s, response, request, requested_url, requested_params)
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_hash
|
42
|
+
@response_hash
|
17
43
|
end
|
18
44
|
|
19
45
|
def curl_command
|
20
46
|
method = begin @request.method rescue nil end
|
21
|
-
Fanforce::Utils.curl_command(method, @
|
47
|
+
Fanforce::Utils.curl_command(method, @requested_url, @requested_params)
|
22
48
|
end
|
23
49
|
|
24
50
|
def code; @response.code end
|
25
51
|
|
26
52
|
end
|
53
|
+
|
54
|
+
class Fanforce::Api::DecodingError < Fanforce::Api::Error
|
55
|
+
|
56
|
+
def initialize(e, response, request, requested_url, requested_params)
|
57
|
+
@message = "There was a Fanforce gem error while decoding JSON response: #{e.message}"
|
58
|
+
@errors = [{message: @message}]
|
59
|
+
super(@message, response, request, requested_url, requested_params)
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_hash
|
63
|
+
{errors: @errors}
|
64
|
+
end
|
65
|
+
|
66
|
+
def code; 500 end
|
67
|
+
|
68
|
+
end
|
@@ -1,49 +1,59 @@
|
|
1
1
|
class Fanforce::Api::Response
|
2
|
-
attr_reader :curl_command, :
|
3
|
-
|
4
|
-
def initialize(response, request,
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
if request.method == :get and @response_json[:current_page] > 1
|
23
|
-
prev_page = @response_json[:current_page] - 1
|
24
|
-
@prev_url = "#{url}?#{Fanforce::Utils.to_query_string(req_params.merge(page: prev_page))}"
|
25
|
-
end
|
26
|
-
else
|
27
|
-
@result = @response_json
|
28
|
-
end
|
29
|
-
rescue
|
30
|
-
raise
|
31
|
-
end
|
2
|
+
attr_reader :curl_command, :requested_url, :requested_params
|
3
|
+
|
4
|
+
def initialize(response, request, requested_url, requested_params)
|
5
|
+
@response = response
|
6
|
+
@request = request
|
7
|
+
@requested_url = requested_url
|
8
|
+
@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)
|
19
|
+
|
20
|
+
if @response_hash[:results].nil?
|
21
|
+
@result = @response_hash
|
32
22
|
else
|
33
|
-
|
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
|
34
36
|
end
|
35
37
|
end
|
36
38
|
|
39
|
+
def code
|
40
|
+
@response.code
|
41
|
+
end
|
42
|
+
|
37
43
|
def to_json
|
38
44
|
to_hash.to_json
|
39
45
|
end
|
40
46
|
|
41
47
|
def to_hash
|
42
|
-
@
|
48
|
+
@response_hash
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
@response_body
|
43
53
|
end
|
44
54
|
|
45
55
|
def [](key)
|
46
|
-
@
|
56
|
+
@response_hash[key]
|
47
57
|
end
|
48
58
|
|
49
59
|
def result
|
data/lib/fanforce/api/utils.rb
CHANGED
@@ -65,37 +65,4 @@ module Fanforce::Api::Utils
|
|
65
65
|
params.clone.delete_if { |k,v| [:api_key].include? k }
|
66
66
|
end
|
67
67
|
|
68
|
-
def log(str)
|
69
|
-
#puts "Fanforce: #{str}"
|
70
|
-
end
|
71
|
-
|
72
|
-
def decode_json(str, symbolize_keys=true)
|
73
|
-
log str
|
74
|
-
MultiJson.load(str, :symbolize_keys => symbolize_keys)
|
75
|
-
end
|
76
|
-
|
77
|
-
# Creates a string representation of a javascript object for $.tmpl
|
78
|
-
def compile_jquery_tmpls(options={}, &block)
|
79
|
-
begin require 'haml' rescue LoadError raise "You must have the haml gem installed for Fanforce.compile_jquery_templates to work." end
|
80
|
-
context = Object.new
|
81
|
-
class << context
|
82
|
-
include Haml::Helpers
|
83
|
-
end
|
84
|
-
context.init_haml_helpers
|
85
|
-
|
86
|
-
format = if options[:format] == 'html' then :html else options[:callback].present? ? :jsonp : :json end
|
87
|
-
|
88
|
-
return context.capture_haml(&block) if format == :html
|
89
|
-
single_line_html = context.capture_haml(&block).split(/\r?\n/).inject('') {|sl, l| sl += l.strip + ' ' }
|
90
|
-
matches = single_line_html.scan(/<script id=[\"'](.*?)[\"'](?:.*?)>(.*?)(?:<\/script>)/mi)
|
91
|
-
|
92
|
-
templates = matches.inject({}) {|t,m| t[m[0]] = m[1]; t }
|
93
|
-
if format == :jsonp
|
94
|
-
"#{options[:callback]}(#{templates.to_json})"
|
95
|
-
else
|
96
|
-
templates.to_json
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
|
101
68
|
end
|
data/lib/fanforce/api/version.rb
CHANGED