fanforce-api 0.31.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.ruby-version +1 -1
- data/.yardopts +5 -0
- data/.yardopts.rb +1 -0
- data/Gemfile +9 -0
- data/LICENSE.md +1 -1
- data/README.md +21 -90
- data/Rakefile +9 -3
- data/fanforce-api.gemspec +5 -5
- data/lib/fanforce/api.rb +7 -11
- data/lib/fanforce/api/errors.rb +24 -0
- data/lib/fanforce/api/errors/decoding.rb +15 -0
- data/lib/fanforce/api/errors/server.rb +31 -0
- data/lib/fanforce/api/main.rb +130 -0
- data/lib/fanforce/api/response.rb +80 -81
- data/lib/fanforce/api/utils.rb +3 -21
- data/lib/fanforce/api/version.rb +1 -1
- data/test/api/auth_test.rb +18 -0
- data/test/test_helper.rb +11 -0
- metadata +26 -27
- data/lib/fanforce/api/_main.rb +0 -111
- data/lib/fanforce/api/error.rb +0 -71
- data/test/fanforce_test.rb +0 -22
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2c6e98f070f07163c77ce43a3309a26ba8978c1e
|
4
|
+
data.tar.gz: 43909c75a46c9fb3767910ccf1d0f56d1c5b0c11
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d2170fbe9bac57593b3489ec0a31cf34adead0911fba830f2b09f499f6722a74a0ef7c27d7108de5466885cdce73182ed7d17d471852b0e8fff0dc30f8d27b7
|
7
|
+
data.tar.gz: d7f1cec939937d5cc7b065888d46535db3d322ed6bcfe0f763772b670c0da8dd6184243673e060e0aafe62d99d8eba809f5466d2d19f69cf61f73136d80a1f0b
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.0.0-p576
|
data/.yardopts
ADDED
data/.yardopts.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'redcarpet/compat'
|
data/Gemfile
CHANGED
data/LICENSE.md
CHANGED
data/README.md
CHANGED
@@ -1,107 +1,28 @@
|
|
1
|
-
## Install
|
2
|
-
|
3
|
-
Via rubygems.org:
|
4
|
-
|
5
|
-
```
|
6
|
-
$ gem install fanforce
|
7
|
-
```
|
8
|
-
|
9
|
-
To build and install the development branch yourself from the latest source:
|
10
|
-
|
11
|
-
```
|
12
|
-
$ git clone git@github.com:mlabs/fanforce-ruby.git
|
13
|
-
$ cd fanforce-ruby
|
14
|
-
$ git checkout master
|
15
|
-
$ rake gem
|
16
|
-
$ gem install pkg/fanforce-{version}
|
17
|
-
```
|
18
|
-
|
19
1
|
## Getting Started
|
20
2
|
|
21
|
-
|
3
|
+
Add fanforce-api to your Gemfile
|
22
4
|
|
23
5
|
``` ruby
|
24
|
-
|
25
|
-
require 'fanforce/api'
|
26
|
-
|
27
|
-
# put your own credentials here
|
28
|
-
api_key = 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
|
29
|
-
|
30
|
-
# set up a client to talk to the Fanforce API
|
31
|
-
ff = Fanforce::API.new api_key
|
6
|
+
gem 'fanforce-api'
|
32
7
|
```
|
33
8
|
|
34
|
-
|
9
|
+
Require fanforce/api and initialize an instance
|
35
10
|
|
36
11
|
``` ruby
|
37
|
-
|
38
|
-
ff.get('/access_info', {})
|
39
|
-
```
|
40
|
-
|
41
|
-
## Full REST Access
|
42
|
-
|
43
|
-
### GET
|
12
|
+
require 'fanforce/api'
|
44
13
|
|
45
|
-
|
46
|
-
ff.
|
14
|
+
# Initialize a new Fanforce::API instance with your api_key
|
15
|
+
ff = Fanforce::API.new('00000000-0000-0000-0000-000000000000')
|
47
16
|
```
|
48
17
|
|
49
|
-
|
18
|
+
## Making Your First Call
|
50
19
|
|
51
|
-
|
52
|
-
ff.post('/', {})
|
53
|
-
```
|
54
|
-
|
55
|
-
### PUT
|
20
|
+
Get your organization details...
|
56
21
|
|
57
22
|
``` ruby
|
58
|
-
ff.
|
23
|
+
ff.get('/organization')
|
59
24
|
```
|
60
25
|
|
61
|
-
### DELETE
|
62
|
-
|
63
|
-
``` ruby
|
64
|
-
ff.delete('/')
|
65
|
-
```
|
66
|
-
|
67
|
-
## Error Handling
|
68
|
-
|
69
|
-
```ruby
|
70
|
-
begin
|
71
|
-
ff.get('/bad_page')
|
72
|
-
rescue Fanforce::Error => e
|
73
|
-
puts e.curl_command
|
74
|
-
end
|
75
|
-
```
|
76
|
-
|
77
|
-
e.curl_command
|
78
|
-
e.response_code
|
79
|
-
e.response_body
|
80
|
-
e.request_url
|
81
|
-
e.request_params
|
82
|
-
|
83
|
-
BadRequestError
|
84
|
-
|
85
|
-
## Utils
|
86
|
-
|
87
|
-
ff.get_url(path, query_params)
|
88
|
-
|
89
|
-
ff.curl_command(method, path, query_params)
|
90
|
-
|
91
|
-
ff.validate_auth
|
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
26
|
## Handling Results
|
106
27
|
|
107
28
|
response.result
|
@@ -112,6 +33,7 @@ response.current_results
|
|
112
33
|
response.current_page
|
113
34
|
response.total_pages
|
114
35
|
|
36
|
+
|
115
37
|
## Paging
|
116
38
|
|
117
39
|
response.prev_url
|
@@ -121,12 +43,21 @@ response.next_url
|
|
121
43
|
|
122
44
|
By default logging is turned off.
|
123
45
|
```ruby
|
124
|
-
Fanforce.config do |config|
|
46
|
+
Fanforce::API.config do |config|
|
125
47
|
config.logging = true
|
126
48
|
end
|
127
49
|
```
|
128
50
|
|
129
|
-
|
51
|
+
|
52
|
+
## Error Handling
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
begin
|
56
|
+
ff.get('/nonexistent_method')
|
57
|
+
rescue Fanforce::API::Error => e
|
58
|
+
puts e.curl_command
|
59
|
+
end
|
60
|
+
```
|
130
61
|
|
131
62
|
## More Information
|
132
63
|
|
data/Rakefile
CHANGED
@@ -3,14 +3,20 @@ require 'rake/testtask'
|
|
3
3
|
require 'fileutils'
|
4
4
|
include FileUtils
|
5
5
|
|
6
|
-
|
6
|
+
task :run_test do
|
7
|
+
Rake::Task['test'].execute
|
8
|
+
puts '---------------------------------------------------------------------'
|
9
|
+
puts 'TESTS PASSED... READY TO BUILD...'
|
10
|
+
puts '---------------------------------------------------------------------'
|
11
|
+
end
|
12
|
+
task :build => :run_test
|
7
13
|
task :default => :build
|
8
14
|
|
9
15
|
########################################################################
|
10
16
|
|
11
17
|
Rake::TestTask.new do |t|
|
12
|
-
t.libs.push
|
13
|
-
t.libs.push
|
18
|
+
t.libs.push 'lib'
|
19
|
+
t.libs.push 'test'
|
14
20
|
t.pattern = 'test/**/*_test.rb'
|
15
21
|
t.verbose = false
|
16
22
|
end
|
data/fanforce-api.gemspec
CHANGED
@@ -8,17 +8,17 @@ Gem::Specification.new do |gem|
|
|
8
8
|
gem.version = Fanforce::API::VERSION
|
9
9
|
gem.date = Time.now.utc.strftime('%Y-%m-%d')
|
10
10
|
gem.summary = %q{Ruby API wrapper for Fanforce}
|
11
|
-
|
11
|
+
|
12
12
|
gem.authors = ['Caleb Clark']
|
13
13
|
gem.email = ['cclark@fanforce.com']
|
14
|
-
gem.homepage = 'http://github.com/fanforce/fanforce-api
|
14
|
+
gem.homepage = 'http://github.com/fanforce/gem-fanforce-api'
|
15
15
|
|
16
16
|
gem.files = `git ls-files`.split($/)
|
17
17
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
18
|
gem.require_paths = ['lib']
|
19
19
|
|
20
|
-
gem.add_runtime_dependency 'rest-client',
|
20
|
+
gem.add_runtime_dependency 'rest-client', '~> 1.6.7'
|
21
|
+
gem.add_runtime_dependency 'uuid', '~> 2.3.7'
|
21
22
|
|
22
|
-
gem.add_runtime_dependency 'fanforce-
|
23
|
-
gem.add_runtime_dependency 'fanforce-validations', '~> 0.8'
|
23
|
+
gem.add_runtime_dependency 'fanforce-base', '~> 1.0'
|
24
24
|
end
|
data/lib/fanforce/api.rb
CHANGED
@@ -1,12 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
require_relative 'api/error'
|
9
|
-
end
|
10
|
-
include Fanforce::Errors
|
11
|
-
end
|
1
|
+
require 'fanforce/base'
|
2
|
+
|
3
|
+
require_relative 'api/version'
|
4
|
+
require_relative 'api/utils'
|
5
|
+
require_relative 'api/errors'
|
6
|
+
require_relative 'api/response'
|
7
|
+
require_relative 'api/main'
|
12
8
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Fanforce::API::Error < StandardError
|
2
|
+
attr_accessor :response, :request, :requested_url, :requested_params, :errors
|
3
|
+
|
4
|
+
def initialize(message, response, request, requested_url, requested_params)
|
5
|
+
@response = response
|
6
|
+
@request = request
|
7
|
+
@requested_url = requested_url
|
8
|
+
@requested_params = requested_params
|
9
|
+
@message = message
|
10
|
+
super(message)
|
11
|
+
end
|
12
|
+
|
13
|
+
def curl_command
|
14
|
+
method = begin @request.method rescue nil end
|
15
|
+
Fanforce.curl_command(method, @requested_url, @requested_params)
|
16
|
+
end
|
17
|
+
|
18
|
+
def code; @response.respond_to?(:code) ? @response.code : 500 end
|
19
|
+
def to_s; @message end
|
20
|
+
def to_hash; {} end
|
21
|
+
end
|
22
|
+
|
23
|
+
require_relative 'errors/server'
|
24
|
+
require_relative 'errors/decoding'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class Fanforce::API::Error::Decoding < Fanforce::API::Error
|
2
|
+
|
3
|
+
def initialize(e, response, request, requested_url, requested_params)
|
4
|
+
@message = "There was a Fanforce gem error while decoding JSON response (#{e.is_a?(String) ? e : e.message}): #{response.body}"
|
5
|
+
@errors = [{message: @message}]
|
6
|
+
super(@message, response, request, requested_url, requested_params)
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_hash
|
10
|
+
{errors: @errors}
|
11
|
+
end
|
12
|
+
|
13
|
+
def code; 500 end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class Fanforce::API::Error::Server < Fanforce::API::Error
|
2
|
+
|
3
|
+
def initialize(response, request, requested_url, requested_params)
|
4
|
+
@response_body = response.to_s
|
5
|
+
|
6
|
+
if Fanforce.is_present?(response.body)
|
7
|
+
begin
|
8
|
+
@response_hash = MultiJson.load(response, symbolize_keys: true)
|
9
|
+
@errors = @response_hash[:errors]
|
10
|
+
rescue Exception => e
|
11
|
+
raise Fanforce::API::Error::Decoding.new(e, response, request, requested_url, requested_params)
|
12
|
+
end
|
13
|
+
else
|
14
|
+
@errors = [{message: "(Response body from #{response.code} error was empty: #{response.body})"}]
|
15
|
+
end
|
16
|
+
|
17
|
+
super(response.to_s, response, request, requested_url, requested_params)
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_hash
|
21
|
+
@response_hash
|
22
|
+
end
|
23
|
+
|
24
|
+
def curl_command
|
25
|
+
method = begin @request.method rescue nil end
|
26
|
+
Fanforce.curl_command(method, @requested_url, @requested_params)
|
27
|
+
end
|
28
|
+
|
29
|
+
def code; @response.code end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'uuid'
|
3
|
+
|
4
|
+
class Fanforce::API
|
5
|
+
include Fanforce::API::Utils
|
6
|
+
|
7
|
+
# Initialize a new API connection
|
8
|
+
# @param auth_data [String,Hash] is a string of api_key or hash with api_key
|
9
|
+
def initialize(auth_data=nil)
|
10
|
+
set_auth(auth_data) if auth_data
|
11
|
+
end
|
12
|
+
|
13
|
+
# Get the raw url for a request
|
14
|
+
# @param path [String]
|
15
|
+
# @param req_params [Hash]
|
16
|
+
# @return [String] url
|
17
|
+
def raw_url(path, req_params={})
|
18
|
+
req_params = inject_auth_params(req_params)
|
19
|
+
"#{complete_url(path)}?#{to_query_string(req_params) if req_params.size > 0}"
|
20
|
+
end
|
21
|
+
|
22
|
+
# Get the CURL command for a request
|
23
|
+
# @param method [Symbol] (get/post/put/delete)
|
24
|
+
# @param path [String]
|
25
|
+
# @param params [Hash]
|
26
|
+
# @return [String] curl command
|
27
|
+
def curl_command(method, path, params={})
|
28
|
+
params = inject_auth_params(params)
|
29
|
+
Fanforce.curl_command(method, complete_url(path), params)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Handles a GET request for Fanforce API
|
33
|
+
# @param path [String]
|
34
|
+
# @param params [Hash]
|
35
|
+
# @return [Fanforce::API::Response]
|
36
|
+
def get(path, params={})
|
37
|
+
params = inject_auth_params(params)
|
38
|
+
RestClient.get(url(path, params), :accept => :json) do |response, request, result, &block|
|
39
|
+
Fanforce::API::Response.process(response, request, complete_url(path), params)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# Handles a POST request for Fanforce API
|
44
|
+
# @param path [String]
|
45
|
+
# @param params [Hash]
|
46
|
+
# @return [Fanforce::API::Response]
|
47
|
+
def post(path, params={}, retries=0)
|
48
|
+
url = complete_url(path)
|
49
|
+
params = inject_auth_params(params) unless retries > 0
|
50
|
+
RestClient.post(url, MultiJson.dump(params), :content_type => :json, :accept => :json) do |response, request, result, &block|
|
51
|
+
if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2
|
52
|
+
puts "retried #{path} #{retries+1} times"
|
53
|
+
post(path, params, retries+1)
|
54
|
+
else
|
55
|
+
Fanforce::API::Response.process(response, request, url, params)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Handles a PUT request for Fanforce API
|
61
|
+
# @param path [String]
|
62
|
+
# @param params [Hash]
|
63
|
+
# @return [Fanforce::API::Response]
|
64
|
+
def put(path, params={}, retries=0)
|
65
|
+
url = complete_url(path)
|
66
|
+
params = inject_auth_params(params) unless retries > 0
|
67
|
+
RestClient.put(url, MultiJson.dump(params), :content_type => :json, :accept => :json) do |response, request, result, &block|
|
68
|
+
if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2
|
69
|
+
puts "retried #{path} #{retries+1} times"
|
70
|
+
post(path, params, retries+1)
|
71
|
+
else
|
72
|
+
Fanforce::API::Response.process(response, request, url, params)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
# Handles a DELETE request for Fanforce API
|
78
|
+
# @param path [String]
|
79
|
+
# @param params [Hash]
|
80
|
+
# @return [Fanforce::API::Response]
|
81
|
+
def delete(path, params={})
|
82
|
+
url = complete_url(path)
|
83
|
+
params = inject_auth_params(params)
|
84
|
+
RestClient.delete(url(path, params), :accept => :json) do |response, request, result, &block|
|
85
|
+
Fanforce::API::Response.process(response, request, url, params)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Get the auth params for current instance
|
90
|
+
# @return [Hash] current auth params
|
91
|
+
def auth_params
|
92
|
+
@auth_params.clone
|
93
|
+
end
|
94
|
+
|
95
|
+
# Auth the current instance. Will raise error if api_key not found or is not of UUID format.
|
96
|
+
# @param auth_data [String, Hash] either an api_key string or a hash containing it
|
97
|
+
# @return [nil]
|
98
|
+
def set_auth(auth_data={})
|
99
|
+
auth_data = {} if is_blank?(auth_data)
|
100
|
+
if auth_data.is_a?(String)
|
101
|
+
@auth_params = {api_key: auth_data}
|
102
|
+
elsif auth_data.is_a?(Hash)
|
103
|
+
auth_data = symbolize_keys(auth_data)
|
104
|
+
@auth_params = {
|
105
|
+
api_key: auth_data[:api_key],
|
106
|
+
organization_id: auth_data[:organization_id]
|
107
|
+
}
|
108
|
+
end
|
109
|
+
raise 'api_key is required to auth Fanforce::API' if !has_valid_api_key?
|
110
|
+
end
|
111
|
+
|
112
|
+
# Check if current instance has a valid api_key
|
113
|
+
# @return [Boolean]
|
114
|
+
def has_valid_api_key?
|
115
|
+
is_present?(@auth_params) and is_present?(@auth_params[:api_key]) and UUID.validate(@auth_params[:api_key])
|
116
|
+
end
|
117
|
+
alias_method :has_valid_auth?, :has_valid_api_key?
|
118
|
+
|
119
|
+
######################################################################
|
120
|
+
private
|
121
|
+
|
122
|
+
def inject_auth_params(params)
|
123
|
+
params.merge(@auth_params || {})
|
124
|
+
end
|
125
|
+
|
126
|
+
def complete_url(path)
|
127
|
+
'http://' + Fanforce.api_domain + path
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
@@ -3,113 +3,112 @@ class Fanforce::API::Response
|
|
3
3
|
|
4
4
|
def self.process(response, request, requested_url, requested_params)
|
5
5
|
return nil if response.code == 404 and request.method == :get
|
6
|
-
raise Fanforce::API::
|
6
|
+
raise Fanforce::API::Error::Server.new(response, request, requested_url, requested_params) if response.code > 201
|
7
7
|
|
8
|
-
begin response_hash =
|
9
|
-
rescue Exception => e; raise Fanforce::API::
|
8
|
+
begin response_hash = MultiJson.load(response, symbolize_keys: true)
|
9
|
+
rescue Exception => e; raise Fanforce::API::Error::Decoding.new(e, response, request, requested_url, requested_params) end
|
10
10
|
if !response_hash.is_a?(Hash)
|
11
|
-
raise Fanforce::API::
|
11
|
+
raise Fanforce::API::Error::Decoding.new('Server did not send a valid hash.', response, request, requested_url, requested_params)
|
12
12
|
elsif response_hash[:results]
|
13
|
-
|
13
|
+
MultipleResults.new(response_hash, response, request, requested_url, requested_params)
|
14
14
|
else
|
15
|
-
|
15
|
+
SingleResult.new(response_hash, response, request, requested_url, requested_params)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
class Fanforce::API::Result < Hash
|
22
|
-
attr_reader :requested_url, :requested_params
|
19
|
+
class MultipleResults < Array
|
20
|
+
attr_reader :requested_url, :requested_params
|
23
21
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
22
|
+
def initialize(response_hash, response, request, requested_url, requested_params)
|
23
|
+
@response = response
|
24
|
+
@request = request
|
25
|
+
@requested_url = requested_url
|
26
|
+
@requested_params = requested_params
|
27
|
+
@response_hash = response_hash
|
28
|
+
self.replace(response_hash[:results])
|
29
|
+
end
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
31
|
+
def data
|
32
|
+
self
|
33
|
+
end
|
34
|
+
alias results data
|
37
35
|
|
38
|
-
|
39
|
-
|
40
|
-
|
36
|
+
def curl_command
|
37
|
+
@curl_command ||= Fanforce.curl_command(@request.method, @requested_url, @requested_params)
|
38
|
+
end
|
41
39
|
|
42
|
-
|
43
|
-
|
44
|
-
|
40
|
+
def body
|
41
|
+
@response.to_s
|
42
|
+
end
|
45
43
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
def total_pages; 1 end
|
50
|
-
def prev_page; nil end
|
51
|
-
def next_page; nil end
|
52
|
-
def prev_page_url; nil end
|
53
|
-
def next_page_url; nil end
|
54
|
-
def code; @response.code end
|
55
|
-
end
|
44
|
+
def current_results
|
45
|
+
@current_results ||= @response_hash[:current_results]
|
46
|
+
end
|
56
47
|
|
57
|
-
|
58
|
-
|
48
|
+
def total_results;
|
49
|
+
@total_results ||= @response_hash[:total_results]
|
50
|
+
end
|
59
51
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
@requested_url = requested_url
|
64
|
-
@requested_params = requested_params
|
65
|
-
@response_hash = response_hash
|
66
|
-
self.replace(response_hash[:results])
|
67
|
-
end
|
52
|
+
def current_page;
|
53
|
+
@current_page ||= @response_hash[:current_page]
|
54
|
+
end
|
68
55
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
alias results data
|
56
|
+
def total_pages;
|
57
|
+
@total_pages ||= @response_hash[:total_pages]
|
58
|
+
end
|
73
59
|
|
74
|
-
|
75
|
-
|
76
|
-
|
60
|
+
def prev_page
|
61
|
+
@prev_page ||= (@request.method == :get and current_page > 1) ? current_page - 1 : nil
|
62
|
+
end
|
77
63
|
|
78
|
-
|
79
|
-
|
80
|
-
|
64
|
+
def next_page
|
65
|
+
@next_page ||= (@request.method == :get and total_pages > current_page) ? current_page + 1 : nil
|
66
|
+
end
|
81
67
|
|
82
|
-
|
83
|
-
|
84
|
-
|
68
|
+
def prev_page_url;
|
69
|
+
@prev_page_url ||= prev_page ? "#{@requested_url}?#{Fanforce.to_query_string(@requested_params.merge(page: current_page-1))}" : nil
|
70
|
+
end
|
85
71
|
|
86
|
-
|
87
|
-
|
72
|
+
def next_page_url;
|
73
|
+
@next_page_url ||= next_page ? "#{@requested_url}?#{Fanforce.to_query_string(@requested_params.merge(page: current_page+1))}" : nil
|
74
|
+
end
|
75
|
+
def code; @response.code end
|
88
76
|
end
|
89
77
|
|
90
|
-
|
91
|
-
|
92
|
-
end
|
78
|
+
class SingleResult < Hash
|
79
|
+
attr_reader :requested_url, :requested_params
|
93
80
|
|
94
|
-
|
95
|
-
|
96
|
-
|
81
|
+
def initialize(response_hash, response, request, requested_url, requested_params)
|
82
|
+
@response = response
|
83
|
+
@request = request
|
84
|
+
@requested_url = requested_url
|
85
|
+
@requested_params = requested_params
|
86
|
+
@response_hash = response_hash
|
87
|
+
self.replace(response_hash)
|
88
|
+
end
|
97
89
|
|
98
|
-
|
99
|
-
|
100
|
-
|
90
|
+
def data
|
91
|
+
self
|
92
|
+
end
|
93
|
+
alias result data
|
101
94
|
|
102
|
-
|
103
|
-
|
104
|
-
|
95
|
+
def curl_command
|
96
|
+
@curl_command ||= Fanforce.curl_command(@request.method, @requested_url, @requested_params)
|
97
|
+
end
|
105
98
|
|
106
|
-
|
107
|
-
|
108
|
-
|
99
|
+
def body;
|
100
|
+
@response.to_s
|
101
|
+
end
|
109
102
|
|
110
|
-
|
111
|
-
|
103
|
+
def current_results; 1 end
|
104
|
+
def total_results; 1 end
|
105
|
+
def current_page; 1 end
|
106
|
+
def total_pages; 1 end
|
107
|
+
def prev_page; nil end
|
108
|
+
def next_page; nil end
|
109
|
+
def prev_page_url; nil end
|
110
|
+
def next_page_url; nil end
|
111
|
+
def code; @response.code end
|
112
112
|
end
|
113
|
-
def code; @response.code end
|
114
113
|
|
115
114
|
end
|
data/lib/fanforce/api/utils.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
require 'fanforce/utils'
|
2
|
-
|
3
1
|
module Fanforce::API::Utils
|
4
|
-
|
5
|
-
extend Fanforce::API::Utils
|
2
|
+
extend self
|
6
3
|
def self.included(base) base.extend(self) end
|
7
4
|
|
5
|
+
include Fanforce::Base::Utils
|
6
|
+
|
8
7
|
def valid_fanforce_js_request?(params=@params)
|
9
8
|
return false if !params.is_a?(Hash)
|
10
9
|
return false if is_blank?(params[:organization_id]) or is_blank?(params[:api_key])
|
@@ -34,21 +33,4 @@ module Fanforce::API::Utils
|
|
34
33
|
return true
|
35
34
|
end
|
36
35
|
|
37
|
-
def remove_nil_values(hash)
|
38
|
-
hash.clone.delete_if {|k,v| v.nil? }
|
39
|
-
end
|
40
|
-
|
41
|
-
def collect_known_params(params)
|
42
|
-
params = symbolize_keys(params)
|
43
|
-
remove_nil_values(app_id: params[:app_id], organization_id: params[:organization_id], organization_slug: params[:organization_slug], api_key: params[:api_key], session_id: params[:session_id])
|
44
|
-
end
|
45
|
-
|
46
|
-
def remove_internal_params(params)
|
47
|
-
params.clone.delete_if { |k,v| [:app_id, :organization_id, :organization_slug, :api_key].include? k }
|
48
|
-
end
|
49
|
-
|
50
|
-
def remove_sensitive_params(params)
|
51
|
-
params.clone.delete_if { |k,v| [:api_key].include? k }
|
52
|
-
end
|
53
|
-
|
54
36
|
end
|
data/lib/fanforce/api/version.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe Fanforce::API do
|
4
|
+
|
5
|
+
it 'should test if var is blank' do
|
6
|
+
assert Fanforce.is_blank?(nil)
|
7
|
+
assert !Fanforce.is_blank?('value')
|
8
|
+
|
9
|
+
ff = Fanforce::API.new
|
10
|
+
assert ff.is_blank?(nil)
|
11
|
+
assert !ff.is_blank?('value')
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should create new instance of fanforce with api_key' do
|
15
|
+
assert Fanforce::API.new('13d90b01-6df7-4618-881c-c79320a0dc21')
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fanforce-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 1.0.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Caleb Clark
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2014-
|
11
|
+
date: 2014-11-25 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rest-client
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,44 +20,39 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 1.6.7
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
|
-
name:
|
28
|
+
name: uuid
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
33
|
+
version: 2.3.7
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
40
|
+
version: 2.3.7
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
|
-
name: fanforce-
|
42
|
+
name: fanforce-base
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ~>
|
52
46
|
- !ruby/object:Gem::Version
|
53
|
-
version: '0
|
47
|
+
version: '1.0'
|
54
48
|
type: :runtime
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ~>
|
60
53
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0
|
62
|
-
description:
|
54
|
+
version: '1.0'
|
55
|
+
description:
|
63
56
|
email:
|
64
57
|
- cclark@fanforce.com
|
65
58
|
executables: []
|
@@ -68,41 +61,47 @@ extra_rdoc_files: []
|
|
68
61
|
files:
|
69
62
|
- .gitignore
|
70
63
|
- .ruby-version
|
64
|
+
- .yardopts
|
65
|
+
- .yardopts.rb
|
71
66
|
- Gemfile
|
72
67
|
- LICENSE.md
|
73
68
|
- README.md
|
74
69
|
- Rakefile
|
75
70
|
- fanforce-api.gemspec
|
76
71
|
- lib/fanforce/api.rb
|
77
|
-
- lib/fanforce/api/
|
78
|
-
- lib/fanforce/api/
|
72
|
+
- lib/fanforce/api/errors.rb
|
73
|
+
- lib/fanforce/api/errors/decoding.rb
|
74
|
+
- lib/fanforce/api/errors/server.rb
|
75
|
+
- lib/fanforce/api/main.rb
|
79
76
|
- lib/fanforce/api/response.rb
|
80
77
|
- lib/fanforce/api/utils.rb
|
81
78
|
- lib/fanforce/api/version.rb
|
82
|
-
- test/
|
83
|
-
|
79
|
+
- test/api/auth_test.rb
|
80
|
+
- test/test_helper.rb
|
81
|
+
homepage: http://github.com/fanforce/gem-fanforce-api
|
84
82
|
licenses: []
|
83
|
+
metadata: {}
|
85
84
|
post_install_message:
|
86
85
|
rdoc_options: []
|
87
86
|
require_paths:
|
88
87
|
- lib
|
89
88
|
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
-
none: false
|
91
89
|
requirements:
|
92
|
-
- -
|
90
|
+
- - '>='
|
93
91
|
- !ruby/object:Gem::Version
|
94
92
|
version: '0'
|
95
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
-
none: false
|
97
94
|
requirements:
|
98
|
-
- -
|
95
|
+
- - '>='
|
99
96
|
- !ruby/object:Gem::Version
|
100
97
|
version: '0'
|
101
98
|
requirements: []
|
102
99
|
rubyforge_project:
|
103
|
-
rubygems_version:
|
100
|
+
rubygems_version: 2.0.14
|
104
101
|
signing_key:
|
105
|
-
specification_version:
|
102
|
+
specification_version: 4
|
106
103
|
summary: Ruby API wrapper for Fanforce
|
107
104
|
test_files:
|
108
|
-
- test/
|
105
|
+
- test/api/auth_test.rb
|
106
|
+
- test/test_helper.rb
|
107
|
+
has_rdoc:
|
data/lib/fanforce/api/_main.rb
DELETED
@@ -1,111 +0,0 @@
|
|
1
|
-
require 'rest-client'
|
2
|
-
require_relative 'utils'
|
3
|
-
require_relative 'response'
|
4
|
-
|
5
|
-
class Fanforce::API
|
6
|
-
include Fanforce::API::Utils
|
7
|
-
include Fanforce::Errors
|
8
|
-
|
9
|
-
def initialize(arg={})
|
10
|
-
if arg.is_a?(Hash)
|
11
|
-
add_params(arg)
|
12
|
-
auth(@params) if @params.length > 0
|
13
|
-
elsif arg.is_a?(String)
|
14
|
-
auth(arg)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def params
|
19
|
-
@params ||= {}
|
20
|
-
end
|
21
|
-
|
22
|
-
def add_params(params_to_add)
|
23
|
-
params.merge!(collect_known_params params_to_add)
|
24
|
-
end
|
25
|
-
|
26
|
-
def url(path, req_params={})
|
27
|
-
req_params = apply_auth(req_params)
|
28
|
-
"#{complete_url(path)}?#{to_query_string(req_params)}"
|
29
|
-
end
|
30
|
-
|
31
|
-
def curl_command(method, path, req_params={})
|
32
|
-
req_params = apply_auth(req_params)
|
33
|
-
Fanforce::Utils.curl_command(method, complete_url(path), req_params)
|
34
|
-
end
|
35
|
-
|
36
|
-
def get(path, req_params={})
|
37
|
-
req_params = apply_auth(req_params)
|
38
|
-
RestClient.get(url(path, req_params), :accept => :json) do |response, request, result, &block|
|
39
|
-
Fanforce::API::Response.process(response, request, complete_url(path), req_params)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def post(path, req_params={}, retries=0)
|
44
|
-
url = complete_url(path)
|
45
|
-
req_params = apply_auth(req_params) unless retries > 0
|
46
|
-
RestClient.post(url, MultiJson.dump(req_params), :content_type => :json, :accept => :json) do |response, request, result, &block|
|
47
|
-
if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2
|
48
|
-
puts "retried #{path} #{retries+1} times"
|
49
|
-
post(path, req_params, retries+1)
|
50
|
-
else
|
51
|
-
Fanforce::API::Response.process(response, request, url, req_params)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def put(path, req_params={}, retries=0)
|
57
|
-
url = complete_url(path)
|
58
|
-
req_params = apply_auth(req_params) unless retries > 0
|
59
|
-
RestClient.put(url, MultiJson.dump(req_params), :content_type => :json, :accept => :json) do |response, request, result, &block|
|
60
|
-
if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2
|
61
|
-
puts "retried #{path} #{retries+1} times"
|
62
|
-
post(path, req_params, retries+1)
|
63
|
-
else
|
64
|
-
Fanforce::API::Response.process(response, request, url, req_params)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def delete(path, req_params={})
|
70
|
-
url = complete_url(path)
|
71
|
-
req_params = apply_auth(req_params)
|
72
|
-
RestClient.delete(url(path, req_params), :accept => :json) do |response, request, result, &block|
|
73
|
-
Fanforce::API::Response.process(response, request, url, req_params)
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
def identify(req_params)
|
78
|
-
post '/bie/identify', req_params
|
79
|
-
end
|
80
|
-
|
81
|
-
def track(com_behavior_id, req_params)
|
82
|
-
post '/bie/track', req_params.merge(com_behavior_id: com_behavior_id)
|
83
|
-
end
|
84
|
-
|
85
|
-
def recommend(com_behavior_id, unique_key, req_params={})
|
86
|
-
req_params.update(unique_key: "#{com_behavior_id}:#{unique_key}")
|
87
|
-
post '/bie/recommend', req_params
|
88
|
-
end
|
89
|
-
|
90
|
-
def auth(auth_data=nil)
|
91
|
-
@auth_hash ||= {}
|
92
|
-
return @auth_hash if is_blank?(auth_data)
|
93
|
-
auth_data = auth_data.is_a?(Hash) ? symbolize_keys(auth_data) : {api_key: auth_data.to_s}
|
94
|
-
@auth_hash.merge! remove_nil_values(api_key: auth_data[:api_key], organization_id: auth_data[:organization_id], organization_slug: auth_data[:organization_slug])
|
95
|
-
params.merge!(@auth_hash)
|
96
|
-
valid_auth?
|
97
|
-
end
|
98
|
-
|
99
|
-
def valid_auth?
|
100
|
-
is_present?(@auth_hash) and is_present?(@auth_hash[:api_key]) and is_present?(@auth_hash[:organization_id])
|
101
|
-
end
|
102
|
-
|
103
|
-
def apply_auth(req_params)
|
104
|
-
req_params.merge(@auth_hash || {})
|
105
|
-
end
|
106
|
-
|
107
|
-
def complete_url(path)
|
108
|
-
'http://' + Fanforce.api_domain + path
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
data/lib/fanforce/api/error.rb
DELETED
@@ -1,71 +0,0 @@
|
|
1
|
-
require_relative 'utils'
|
2
|
-
|
3
|
-
class Fanforce::API::Error < StandardError
|
4
|
-
attr_accessor :response, :request, :requested_url, :requested_params, :errors
|
5
|
-
|
6
|
-
def initialize(message, response, request, requested_url, requested_params)
|
7
|
-
@response = response
|
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
|
-
end
|
24
|
-
|
25
|
-
class Fanforce::API::ServerError < Fanforce::API::Error
|
26
|
-
|
27
|
-
def initialize(response, request, requested_url, requested_params)
|
28
|
-
@response_body = response.to_s
|
29
|
-
|
30
|
-
if Fanforce::Utils.is_present?(response.body)
|
31
|
-
begin
|
32
|
-
@response_hash = Fanforce::Utils.decode_json(response)
|
33
|
-
@errors = @response_hash[:errors]
|
34
|
-
rescue Exception => e
|
35
|
-
raise Fanforce::API::DecodingError.new(e, response, request, requested_url, requested_params)
|
36
|
-
end
|
37
|
-
else
|
38
|
-
@errors = [{message: "(Response body from #{response.code} error was empty: #{response.body})"}]
|
39
|
-
end
|
40
|
-
|
41
|
-
super(response.to_s, response, request, requested_url, requested_params)
|
42
|
-
end
|
43
|
-
|
44
|
-
def to_hash
|
45
|
-
@response_hash
|
46
|
-
end
|
47
|
-
|
48
|
-
def curl_command
|
49
|
-
method = begin @request.method rescue nil end
|
50
|
-
Fanforce::Utils.curl_command(method, @requested_url, @requested_params)
|
51
|
-
end
|
52
|
-
|
53
|
-
def code; @response.code end
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
class Fanforce::API::DecodingError < Fanforce::API::Error
|
58
|
-
|
59
|
-
def initialize(e, response, request, requested_url, requested_params)
|
60
|
-
@message = "There was a Fanforce gem error while decoding JSON response (#{e.is_a?(String) ? e : e.message}): #{response.body}"
|
61
|
-
@errors = [{message: @message}]
|
62
|
-
super(@message, response, request, requested_url, requested_params)
|
63
|
-
end
|
64
|
-
|
65
|
-
def to_hash
|
66
|
-
{errors: @errors}
|
67
|
-
end
|
68
|
-
|
69
|
-
def code; 500 end
|
70
|
-
|
71
|
-
end
|
data/test/fanforce_test.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'minitest/autorun'
|
3
|
-
|
4
|
-
$: << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
-
require 'fanforce'
|
6
|
-
|
7
|
-
describe Fanforce do
|
8
|
-
|
9
|
-
it 'should test if var is blank' do
|
10
|
-
assert Fanforce.is_blank?(nil)
|
11
|
-
assert !Fanforce.is_blank?("value")
|
12
|
-
|
13
|
-
ff = Fanforce.new
|
14
|
-
assert ff.is_blank?(nil)
|
15
|
-
assert !ff.is_blank?("value")
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'should create new instance of fanforce with api_key' do
|
19
|
-
Fanforce.new('13d90b01-6df7-4618-881c-c79320a0dc21')
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|