fanforce 0.5.10 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NTcxYmYwYzc5NmYzMGU4N2I4OTE2MGNmMTU1Nzc0MDZlYTdhODJkZg==
5
+ data.tar.gz: !binary |-
6
+ MzUyYWVkNzcwMGY3Nzc1Y2YxYjk5OTU5YzY4MTc4NDA0M2E1ZWQ1MQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MmQyNmI5MzZlMTBkZTUzNTk2NGZiMTA3NmZlMTA0NWFlZjQ5Y2M1NWRkZWRh
10
+ ZGVlOGIzZDFkYWM3Njc5NDYwZDAyYTEwMmQ1NzE2ZmJmMmZlNmI3YTk1ZDIw
11
+ MzhlMTQ5YzdjODZjZGEzN2ZkNDg3ZDNlNGVkZTkzYmI5MzJmMDE=
12
+ data.tar.gz: !binary |-
13
+ NTM1MzllMTg5ZmRjOGY5NmU3MWM1ZTJjZmJjZjhkN2U5NmUwNDg3YTY3Njhh
14
+ Y2FlZjI4NDZlN2FjZTUxNDU5YjhkMTU1OTI1NDIzYjc0NTRmNGI1MDNjOWQ0
15
+ Yjk5NDczOTllNWY4NDcwYzdiNWE0YTEyYjdkYTJkMzgyNmJlZTM=
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source "http://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
  gemspec
data/fanforce.gemspec CHANGED
@@ -1,24 +1,24 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "fanforce/version"
4
+ require 'fanforce/api/version'
5
5
 
6
6
  Gem::Specification.new do |gem|
7
- gem.name = "fanforce"
8
- gem.version = Fanforce::VERSION
7
+ gem.name = 'fanforce'
8
+ gem.version = Fanforce::Api::VERSION
9
9
  gem.date = '2012-10-19'
10
10
  gem.summary = %q{Ruby API wrapper for Fanforce}
11
11
  gem.description = %q{Ruby API wrapper that interfaces with all six Fanforce APIs.}
12
- gem.authors = ["Caleb Clark"]
13
- gem.email = ["cclark@fanforce.com"]
14
- gem.homepage = "http://github.com/mlabs/fanforce-ruby"
12
+ gem.authors = ['Caleb Clark']
13
+ gem.email = ['cclark@fanforce.com']
14
+ gem.homepage = 'http://github.com/mlabs/fanforce-ruby'
15
15
 
16
16
  gem.files = `git ls-files`.split($/)
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
18
+ gem.require_paths = ['lib']
19
19
 
20
- gem.add_development_dependency 'rake'
21
20
  gem.add_runtime_dependency 'multi_json', '~> 1.3'
22
21
  gem.add_runtime_dependency 'rest-client', '~> 1.6.7'
23
- gem.add_runtime_dependency 'rack', '> 1.0'
22
+
23
+ gem.add_runtime_dependency 'fanforce-internal-errors'
24
24
  end
@@ -1,10 +1,12 @@
1
- require_relative 'errors'
1
+ require 'rest-client'
2
2
  require_relative 'utils'
3
3
  require_relative 'response'
4
- require 'rest-client'
5
4
 
6
- class Fanforce
7
- include Fanforce::Utils
5
+ module Fanforce::Api
6
+ include Fanforce::Api::Utils
7
+ include Fanforce::Errors
8
+
9
+ def self.included(base) base.extend(self) end
8
10
 
9
11
  def initialize(arg={})
10
12
  if arg.is_a?(Hash)
@@ -44,7 +46,7 @@ class Fanforce
44
46
  url = complete_url(path)
45
47
  req_params = apply_auth(req_params)
46
48
  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)
49
+ Fanforce::Api::Response.new(response, request, url, req_params)
48
50
  end
49
51
  end
50
52
 
@@ -52,7 +54,7 @@ class Fanforce
52
54
  url = complete_url(path)
53
55
  req_params = apply_auth(req_params)
54
56
  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)
57
+ Fanforce::Api::Response.new(response, request, url, req_params)
56
58
  end
57
59
  end
58
60
 
@@ -60,7 +62,7 @@ class Fanforce
60
62
  url = complete_url(path)
61
63
  req_params = apply_auth(req_params)
62
64
  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)
65
+ Fanforce::Api::Response.new(response, request, url, req_params)
64
66
  end
65
67
  end
66
68
 
@@ -0,0 +1,26 @@
1
+ require_relative 'utils'
2
+
3
+ class Fanforce::Api::Error < StandardError
4
+ attr_accessor :path, :query_params, :request, :response, :request_url, :http_status
5
+
6
+ def initialize(response, request, path, query_params)
7
+ @query_params = query_params
8
+ @path = path
9
+ @request = request
10
+ begin
11
+ @response = MultiJson.load(response, :symbolize_keys => true)
12
+ rescue Exception => e
13
+ @response = "Error decoding JSON in Fanforce gem: #{e.message}"
14
+ end
15
+ @request_url = "#{@path}?#{Fanforce::Utils.to_query_string(@query_params)}"
16
+ super(@response.is_a?(Hash) ? @response[:msg] : @response)
17
+ end
18
+
19
+ def curl_command
20
+ method = begin @request.method rescue nil end
21
+ Fanforce::Utils.curl_command(method, @path, @query_params)
22
+ end
23
+
24
+ def code; @response.code end
25
+
26
+ end
File without changes
@@ -0,0 +1,81 @@
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
32
+ else
33
+ raise Fanforce::Api::Error.new(response, request, url, req_params)
34
+ end
35
+ end
36
+
37
+ def to_json
38
+ to_hash.to_json
39
+ end
40
+
41
+ def to_hash
42
+ @response_json
43
+ end
44
+
45
+ def [](key)
46
+ @response_json[key]
47
+ end
48
+
49
+ def result
50
+ @result || (raise NoMethodError, 'This API response returns more than 1 record. Try using "results".')
51
+ end
52
+
53
+ def results
54
+ @results || (raise NoMethodError, 'This API response only returns 1 record. Try using "result".')
55
+ end
56
+
57
+ def current_results
58
+ @current_results || 1
59
+ end
60
+
61
+ def total_results
62
+ @total_results || 1
63
+ end
64
+
65
+ def current_page
66
+ @current_page || 1
67
+ end
68
+
69
+ def total_pages
70
+ @total_pages || 1
71
+ end
72
+
73
+ def prev_url
74
+ @prev_url || nil
75
+ end
76
+
77
+ def next_url
78
+ @next_url || nil
79
+ end
80
+
81
+ end
@@ -1,19 +1,10 @@
1
- require 'uri'
2
1
  require 'rack/utils'
3
- require 'forwardable'
4
- require 'multi_json'
2
+ require 'fanforce/utils'
5
3
 
6
- module Fanforce::Utils
7
- extend Fanforce::Utils
8
- def self.included(base) base.extend(self) end
9
-
10
- def is_blank?(obj)
11
- obj.respond_to?(:empty?) ? obj.empty? : !obj
12
- end
13
-
14
- def is_present?(obj)
15
- !is_blank?(obj)
16
- end
4
+ module Fanforce::Api::Utils
5
+ include Fanforce::Utils
6
+ extend Fanforce::Api::Utils
7
+ def self.included(base) base.extend(self) end
17
8
 
18
9
  def valid_fanforce_js_request?(params=@params)
19
10
  return false if !params.is_a?(Hash)
@@ -74,52 +65,6 @@ module Fanforce::Utils
74
65
  params.clone.delete_if { |k,v| [:api_key].include? k }
75
66
  end
76
67
 
77
- def parse_url(raw_url)
78
- return if is_blank?(raw_url)
79
- url = URI::parse(raw_url)
80
- query_params = Rack::Utils.parse_query(url.query).inject({}) do |result, (k,v)|
81
- if k !~ /^ff_.+/
82
- result[k] = v
83
- end
84
- result
85
- end
86
- query_string = to_query_string(Hash[query_params.sort]) if is_present?(query_params)
87
- _external_id = url.host + url.path
88
- clean_url = "#{url.scheme}://#{url.host}#{(if ![80,443].include?(url.port) then ":#{url.port}" end)}#{url.path}#{(if is_present?(query_string) then "?#{query_string}" end)}"
89
-
90
- { _external_id: _external_id, clean_url: clean_url, raw_url: raw_url, query_params: query_params, query_string: query_string, scheme: url.scheme, host: url.host, port: url.port, path: url.path, fragment: url.fragment }
91
- end
92
-
93
- def to_query_string(obj, namespace=nil)
94
- return '' if is_blank?(obj)
95
- if obj.is_a?(Array)
96
- obj.collect { |value| to_query_string(value, "#{namespace}[]") }.join '&'
97
- elsif obj.is_a?(Hash)
98
- obj.collect { |key, value| to_query_string(value, namespace ? "#{namespace}[#{key}]" : key) }.sort * '&'
99
- elsif obj.is_a?(Object)
100
- require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
101
- "#{CGI.escape(to_param(namespace))}=#{CGI.escape(to_param(obj).to_s)}"
102
- else
103
- raise "Argument must be an object, hash, or array; instead it was a #{obj.class}"
104
- end
105
- end
106
-
107
- def to_param(obj, namespace=nil)
108
- if obj.is_a?(Array)
109
- collect { |e| to_param(e) }.join '/'
110
- elsif obj.is_a?(Hash)
111
- to_query_string(obj, namespace)
112
- elsif obj.is_a?(NilClass)
113
- obj
114
- elsif obj.is_a?(TrueClass)
115
- obj
116
- elsif obj.is_a?(FalseClass)
117
- obj
118
- elsif obj.is_a?(Object)
119
- obj.to_s
120
- end
121
- end
122
-
123
68
  def log(str)
124
69
  #puts "Fanforce: #{str}"
125
70
  end
@@ -152,19 +97,5 @@ module Fanforce::Utils
152
97
  end
153
98
  end
154
99
 
155
- def curl_command(method, url, req_params)
156
- case method
157
- when :get
158
- "curl \"#{url}?#{to_query_string(req_params)}\""
159
- when :post
160
- "curl -X POST -d \"#{to_query_string(req_params)}\" #{url}"
161
- when :put
162
- "curl -X PUT -d \"#{to_query_string(req_params)}\" #{url.to_json}"
163
- when :delete
164
- "curl --request DELETE \"#{url}?#{to_query_string(req_params)}\""
165
- else
166
- raise 'Unknown request method'
167
- end
168
- end
169
100
 
170
101
  end
@@ -0,0 +1,5 @@
1
+ class Fanforce
2
+ module Api
3
+ VERSION = '0.6.0'
4
+ end
5
+ end
data/lib/fanforce.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  class Fanforce
2
- require_relative 'ff_globals'
3
- require_relative 'fanforce/version'
4
- require_relative 'fanforce/config'
5
- require_relative 'fanforce/main'
2
+ require 'fanforce/errors'
3
+ require_relative 'fanforce/api/version'
4
+ require_relative 'fanforce/api/_main'
5
+ require_relative 'fanforce/api/ff_globals'
6
+ require_relative 'fanforce/api/error'
7
+ include Fanforce::Api
6
8
  end
7
9
 
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fanforce
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.10
5
- prerelease:
4
+ version: 0.6.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Caleb Clark
@@ -11,26 +10,9 @@ bindir: bin
11
10
  cert_chain: []
12
11
  date: 2012-10-19 00:00:00.000000000 Z
13
12
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: rake
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
13
  - !ruby/object:Gem::Dependency
31
14
  name: multi_json
32
15
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
16
  requirements:
35
17
  - - ~>
36
18
  - !ruby/object:Gem::Version
@@ -38,7 +20,6 @@ dependencies:
38
20
  type: :runtime
39
21
  prerelease: false
40
22
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
23
  requirements:
43
24
  - - ~>
44
25
  - !ruby/object:Gem::Version
@@ -46,7 +27,6 @@ dependencies:
46
27
  - !ruby/object:Gem::Dependency
47
28
  name: rest-client
48
29
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
30
  requirements:
51
31
  - - ~>
52
32
  - !ruby/object:Gem::Version
@@ -54,27 +34,24 @@ dependencies:
54
34
  type: :runtime
55
35
  prerelease: false
56
36
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
37
  requirements:
59
38
  - - ~>
60
39
  - !ruby/object:Gem::Version
61
40
  version: 1.6.7
62
41
  - !ruby/object:Gem::Dependency
63
- name: rack
42
+ name: fanforce-internal-errors
64
43
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
44
  requirements:
67
- - - ! '>'
45
+ - - ! '>='
68
46
  - !ruby/object:Gem::Version
69
- version: '1.0'
47
+ version: '0'
70
48
  type: :runtime
71
49
  prerelease: false
72
50
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
51
  requirements:
75
- - - ! '>'
52
+ - - ! '>='
76
53
  - !ruby/object:Gem::Version
77
- version: '1.0'
54
+ version: '0'
78
55
  description: Ruby API wrapper that interfaces with all six Fanforce APIs.
79
56
  email:
80
57
  - cclark@fanforce.com
@@ -91,37 +68,35 @@ files:
91
68
  - Rakefile
92
69
  - fanforce.gemspec
93
70
  - lib/fanforce.rb
94
- - lib/fanforce/config.rb
95
- - lib/fanforce/errors.rb
96
- - lib/fanforce/main.rb
97
- - lib/fanforce/response.rb
98
- - lib/fanforce/utils.rb
99
- - lib/fanforce/version.rb
100
- - lib/ff_globals.rb
71
+ - lib/fanforce/api/_main.rb
72
+ - lib/fanforce/api/error.rb
73
+ - lib/fanforce/api/ff_globals.rb
74
+ - lib/fanforce/api/response.rb
75
+ - lib/fanforce/api/utils.rb
76
+ - lib/fanforce/api/version.rb
101
77
  - test/fanforce_test.rb
102
78
  homepage: http://github.com/mlabs/fanforce-ruby
103
79
  licenses: []
80
+ metadata: {}
104
81
  post_install_message:
105
82
  rdoc_options: []
106
83
  require_paths:
107
84
  - lib
108
85
  required_ruby_version: !ruby/object:Gem::Requirement
109
- none: false
110
86
  requirements:
111
87
  - - ! '>='
112
88
  - !ruby/object:Gem::Version
113
89
  version: '0'
114
90
  required_rubygems_version: !ruby/object:Gem::Requirement
115
- none: false
116
91
  requirements:
117
92
  - - ! '>='
118
93
  - !ruby/object:Gem::Version
119
94
  version: '0'
120
95
  requirements: []
121
96
  rubyforge_project:
122
- rubygems_version: 1.8.23
97
+ rubygems_version: 2.0.3
123
98
  signing_key:
124
- specification_version: 3
99
+ specification_version: 4
125
100
  summary: Ruby API wrapper for Fanforce
126
101
  test_files:
127
102
  - test/fanforce_test.rb
@@ -1,14 +0,0 @@
1
- class Fanforce
2
- SERVERS = {
3
- dev: 'api.fanforce.gg',
4
- qa: 'api.fanforce.us',
5
- prod: 'api.fanforce.com'
6
- }
7
-
8
- TEST_ACCOUNT = {
9
- _id: '',
10
- _slug: '',
11
- _password: '',
12
- _api_key: ''
13
- }
14
- end
@@ -1,55 +0,0 @@
1
- require_relative 'utils'
2
-
3
- class Fanforce
4
- class Error < StandardError
5
- attr_accessor :path, :query_params, :request, :response, :request_url, :http_status
6
-
7
- def initialize(response, request, path, query_params)
8
- @query_params = query_params
9
- @path = path
10
- @request = request
11
- begin
12
- @response = MultiJson.load(response, :symbolize_keys => true)
13
- rescue Exception => e
14
- @response = "Error decoding JSON in Fanforce gem: #{e.message}"
15
- end
16
- @request_url = "#{@path}?#{Fanforce::Utils.to_query_string(@query_params)}"
17
- super(@response.is_a?(Hash) ? @response[:msg] : @response)
18
- end
19
-
20
- def curl_command
21
- method = begin @request.method rescue nil end
22
- Fanforce::Utils.curl_command(method, @path, @query_params)
23
- end
24
-
25
- def code; end
26
- end
27
-
28
- class BadRequestError < Error
29
- def code; @http_status = 400 end
30
- end
31
-
32
- class Unauthorized < Error
33
- def code; @http_status = 401 end
34
- end
35
-
36
- class ForbiddenError < Error
37
- def code; @http_status = 403 end
38
- end
39
-
40
- class NotFoundError < Error
41
- def code; @http_status = 404 end
42
- end
43
-
44
- class UnprocessableEntityError < Error
45
- def code; @http_status = 422 end
46
- end
47
-
48
- class UnknownError < Error
49
- def code; @http_status ||= 500; 500 end
50
- def initialize(response, request, path, query_params)
51
- @http_status = response.code
52
- super(response, request, path, query_params)
53
- end
54
- end
55
- end
@@ -1,92 +0,0 @@
1
- class Fanforce::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
32
- when 400
33
- raise Fanforce::BadRequestError.new(response, request, url, req_params)
34
- when 401
35
- raise Fanforce::Unauthorized.new(response, request, url, req_params)
36
- when 403
37
- raise Fanforce::ForbiddenError.new(response, request, url, req_params)
38
- when 404
39
- raise Fanforce::NotFoundError.new(response, request, url, req_params)
40
- when 422
41
- raise Fanforce::UnprocessableEntityError.new(response, request, url, req_params)
42
- else
43
- raise Fanforce::UnknownError.new(response, request, url, req_params)
44
- end
45
- response
46
- end
47
-
48
- def to_json
49
- to_hash.to_json
50
- end
51
-
52
- def to_hash
53
- @response_json
54
- end
55
-
56
- def [](key)
57
- @response_json[key]
58
- end
59
-
60
- def result
61
- @result || (raise NoMethodError, 'This API response returns more than 1 record. Try using "results".')
62
- end
63
-
64
- def results
65
- @results || (raise NoMethodError, 'This API response only returns 1 record. Try using "result".')
66
- end
67
-
68
- def total_results
69
- @total_results || 1
70
- end
71
-
72
- def current_results
73
- @current_results || 1
74
- end
75
-
76
- def current_page
77
- @current_page || 1
78
- end
79
-
80
- def total_pages
81
- @total_pages || 1
82
- end
83
-
84
- def prev_url
85
- @prev_url || nil
86
- end
87
-
88
- def next_url
89
- @next_url || nil
90
- end
91
-
92
- end
@@ -1,3 +0,0 @@
1
- class Fanforce
2
- VERSION = '0.5.10'
3
- end