fanforce 0.6.1 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MmE3Y2YxNWNiMzU2N2M1ZGY2OGU3ZWI5MTE3ZmU3ZGQ3MTIwMjNlMA==
4
+ NTBmNDMzZTVhYzQwNDc3YzNkNTBkYzcyMDNmYjM3ZGYxMDY0YTI1Mg==
5
5
  data.tar.gz: !binary |-
6
- M2U2YjRiMDgwMGFiMzdkM2FhZmI3YzY0NzU5ZmY4YWNmOWZmZjk2YQ==
6
+ ZDY5NmM5MjllNjAzYzM5ODYxYmZiZmJlNjA1OTEyMWMyMTc1NjBjMQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NmY1ZmE3YTUyOGI5NWU0YjdkY2RiM2U0MjUxMzZmMTQzNDRlNWQyOWJjMTQ5
10
- ZGVlMDY5MDlhYzVkOWM3ZTZmNGM5ODkxYjFkODc5NTAyYTM3ZTEzNjkyNWNl
11
- YThkM2Q3Y2RmYWI0MzdlNmVmNDhjZDUxYzY2NzAwZmU0NDQ1ODc=
9
+ MzFjZjQ2NGJlMDQ2Y2Q0MjFlZmUzZDdhNzMyNDJiMTI5MmYzOGVmNzZjYTc1
10
+ NTZhMDk1YTY2OTA1MjgxNDQxMTNmZjQ2NTcwMDZhZjI1MDVjZmU4YjUyMGZj
11
+ YTM2Zjg1MTQ2YjQ1MjE3Njk3ZWIxNDdkM2FlZmEyMTRmZGJlYzE=
12
12
  data.tar.gz: !binary |-
13
- OTk1NTQwMTRjNzEyODg3Y2FmZjNlMDY2NDI3YzJiMTllNDBlNmM0ZTQyN2Rm
14
- YmZjYjU3MGUxMWFjMWQwMWI3YmZjNzFiMGY0YjUzODU3YTcyZTdmZmRkMzA2
15
- ZWQyM2ZiYWI4MDVjZTNjZDQxOWNhYjMzODZlMGQxOTM2NTA1NjE=
13
+ Y2M5ZWQ3N2Q0ZjE1OWE3Y2IzNDMyNWVmYmU0YWEwYTk3OGQ3OWFmZTY2ODQ5
14
+ NTkyMDk0ZjVkNDQxMGVkMDFjOTVhOTRhNGQ4ZjJiYzVhYzYyMGZlMDI2YWIz
15
+ NzkxODk1MjA4MjFjZTZhOGI4NjMwYjI0ZDNlYjhjODU4N2FmZjM=
@@ -1,26 +1,68 @@
1
1
  require_relative 'utils'
2
2
 
3
3
  class Fanforce::Api::Error < StandardError
4
- attr_accessor :path, :query_params, :request, :response, :request_url, :http_status
4
+ attr_accessor :response, :request, :requested_url, :requested_params, :errors
5
5
 
6
- def initialize(response, request, path, query_params)
7
- @query_params = query_params
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
- @response = MultiJson.load(response, :symbolize_keys => true)
32
+ @response_hash = Fanforce::Utils.decode_json(response)
33
+ @errors = @response_hash[:errors]
12
34
  rescue Exception => e
13
- @response = "Error decoding JSON in Fanforce gem: #{e.message}"
35
+ raise Fanforce::Api::DecodingError.new(e, response, request, requested_url, requested_params)
14
36
  end
15
- @request_url = "#{@path}?#{Fanforce::Utils.to_query_string(@query_params)}"
16
- super(@response.is_a?(Hash) ? @response[:msg] : @response)
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, @path, @query_params)
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, :request_url, :response_body, :response_json
3
-
4
- def initialize(response, request, url, req_params)
5
- case response.code
6
- when 200, 201
7
- begin
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
20
- @next_url = "#{url}?#{Fanforce::Utils.to_query_string(req_params.merge(page: next_page))}"
21
- end
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
- raise Fanforce::Api::Error.new(response, request, url, req_params)
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
- @response_json
48
+ @response_hash
49
+ end
50
+
51
+ def to_s
52
+ @response_body
43
53
  end
44
54
 
45
55
  def [](key)
46
- @response_json[key]
56
+ @response_hash[key]
47
57
  end
48
58
 
49
59
  def result
@@ -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
@@ -1,5 +1,5 @@
1
1
  class Fanforce
2
2
  module Api
3
- VERSION = '0.6.1'
3
+ VERSION = '0.6.2'
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.1
4
+ version: 0.6.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caleb Clark