fanforce 0.2.0 → 0.2.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.
data/README.md CHANGED
@@ -14,4 +14,70 @@ $ cd fanforce-ruby
14
14
  $ git checkout master
15
15
  $ rake gem
16
16
  $ gem install pkg/fanforce-{version}
17
- ```
17
+ ```
18
+
19
+ ## Getting Started
20
+
21
+ ### Set It Up
22
+
23
+ ``` ruby
24
+ require 'rubygems' # not necessary with ruby 1.9 but included for completeness
25
+ require 'fanforce'
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.new api_key
32
+ ```
33
+
34
+ ### Making Your First Call
35
+
36
+ ``` ruby
37
+ # get your api access info
38
+ @ff.get('/access_info', {})
39
+ ```
40
+
41
+ ## Full REST Access
42
+
43
+ ### GET
44
+
45
+ ``` ruby
46
+ ff.get('/', {})
47
+ ```
48
+
49
+ ### POST
50
+
51
+ ``` ruby
52
+ ff.post('/', {})
53
+ ```
54
+
55
+ ### PUT
56
+
57
+ ``` ruby
58
+ ff.put('/', {})
59
+ ```
60
+
61
+ ### DELETE
62
+
63
+ ``` ruby
64
+ ff.delete('/')
65
+ ```
66
+
67
+ ## Error Handling
68
+
69
+ begin
70
+
71
+ rescue Fanforce::Error => e
72
+
73
+ end
74
+
75
+ e.curl_command
76
+ e.response_code
77
+ e.response_body
78
+ e.request_url
79
+ e.request_params
80
+
81
+ ## More Information
82
+
83
+ Visit the Fanforce Developers site to explore the full API access methods available.
data/lib/fanforce/main.rb CHANGED
@@ -6,82 +6,89 @@ class Fanforce
6
6
  include Fanforce::Utils
7
7
 
8
8
  ########################################################################
9
- def initialize(auth_key_or_hash=nil)
10
- auth(auth_key_or_hash) if present?(auth_key_or_hash)
9
+ def initialize(auth_data=nil)
10
+ auth(auth_data) if is_present?(auth_data)
11
11
  end
12
12
 
13
- def get(path, requestParams={})
14
- path = complete_path(path)
15
- query = apply_auth(requestParams)
16
- RestClient.get(path, {:params => query, :accept => :json}) do |response, request, result, &block|
17
- handle_response(response, request, path, query)
13
+ def get(path, params={})
14
+ url = complete_url(path)
15
+ params = apply_auth(params)
16
+ RestClient.get(url, {:params => params, :accept => :json}) do |response, request, result, &block|
17
+ handle_response(response, request, url, params)
18
18
  end
19
19
  end
20
20
 
21
- def post(path, requestParams={})
22
- path = complete_path(path)
23
- query = apply_auth(requestParams)
24
- RestClient.post(path, query, {:accept => :json}) do |response, request, result, &block|
25
- handle_response(response, request, path, query)
21
+ def get_url(path, params={})
22
+ url = complete_url(path)
23
+ params = apply_auth(params)
24
+ "#{url}?#{to_query_string(params)}"
25
+ end
26
+
27
+ def post(path, params={})
28
+ url = complete_url(path)
29
+ params = apply_auth(params)
30
+ RestClient.post(url, params, {:accept => :json}) do |response, request, result, &block|
31
+ handle_response(response, request, url, params)
26
32
  end
27
33
  end
28
34
 
29
- def put(path, requestParams={})
30
- path = complete_path(path)
31
- query = apply_auth(requestParams)
32
- RestClient.put(path, query, {:accept => :json}) do |response, request, result, &block|
33
- handle_response(response, request, path, query)
35
+ def put(path, params={})
36
+ url = complete_url(path)
37
+ params = apply_auth(params)
38
+ RestClient.put(url, params, {:accept => :json}) do |response, request, result, &block|
39
+ handle_response(response, request, url, params)
34
40
  end
35
41
  end
36
42
 
37
- def delete(path, requestParams={})
38
- path = complete_path(path)
39
- query = apply_auth(requestParams)
40
- RestClient.delete(path, {:query => query, :accept => :json}) do |response, request, result, &block|
41
- handle_response(response, request, path, query)
43
+ def delete(path, params={})
44
+ url = complete_url(path)
45
+ params = apply_auth(url)
46
+ RestClient.delete(url, {:query => params, :accept => :json}) do |response, request, result, &block|
47
+ handle_response(response, request, url, params)
42
48
  end
43
49
  end
44
50
 
45
- def handle_response(response, request, path, query)
51
+ def handle_response(response, request, url, params)
46
52
  case response.code
47
53
  when 200, 201
48
54
  begin
49
55
  response = decode_json(response)
50
56
  rescue
51
- raise UnknownError.new(response, request, path, query)
57
+ raise UnknownError.new(response, request, url, params)
52
58
  end
53
59
  when 400
54
- raise BadRequestError.new(response, request, path, query)
60
+ raise BadRequestError.new(response, request, url, params)
55
61
  when 403
56
- raise ForbiddenError.new(response, request, path, query)
62
+ raise ForbiddenError.new(response, request, url, params)
57
63
  when 404
58
- raise NotFoundError.new(response, request, path, query)
64
+ raise NotFoundError.new(response, request, url, params)
59
65
  when 422
60
- raise UnprocessableEntityError.new(response, request, path, query)
66
+ raise UnprocessableEntityError.new(response, request, url, params)
61
67
  else
62
- raise UnknownError.new(response, request, path, query)
68
+ raise UnknownError.new(response, request, url, params)
63
69
  end
64
70
  response
65
71
  end
66
72
 
67
- def auth(auth_hash=nil)
68
- if present?(auth_hash)
69
- auth_hash = auth_hash.is_a?(Hash) ? auth_hash.symbolize_keys : {api_key: auth_hash}
70
- @auth_hash[:api_key] if present?(auth_hash[:api_key])
71
- @auth_hash[:fanforce_id] if present?(auth_hash[:fanforce_id])
73
+ def auth(auth_data=nil)
74
+ if is_present?(auth_data)
75
+ auth_data = auth_data.is_a?(Hash) ? auth_data.symbolize_keys : {api_key: auth_data.to_s}
76
+ @auth_hash ||= {}
77
+ @auth_hash[:api_key] = auth_data[:api_key] if is_present?(auth_data[:api_key])
78
+ @auth_hash[:fanforce_id] = auth_data[:fanforce_id] if is_present?(auth_data[:fanforce_id])
72
79
  end
73
80
  @auth_hash
74
81
  end
75
82
 
76
83
  def valid_auth?
77
- present?(@auth_hash) and present?(@auth_hash[:api_key])
84
+ is_present?(@auth_hash) and is_present?(@auth_hash[:api_key])
78
85
  end
79
86
 
80
87
  def apply_auth(params)
81
88
  params.merge(@auth_hash || {})
82
89
  end
83
90
 
84
- def complete_path(path)
91
+ def complete_url(path)
85
92
  'http://' + $API_DOMAIN + path
86
93
  end
87
94
 
@@ -4,48 +4,48 @@ require 'query_string_parser'
4
4
  module Fanforce::Utils
5
5
  def self.included(base) base.extend(self) end
6
6
 
7
- def blank?(obj)
7
+ def is_blank?(obj)
8
8
  obj.respond_to?(:empty?) ? obj.empty? : !obj
9
9
  end
10
10
 
11
- def present?(obj)
12
- !blank?(obj)
11
+ def is_present?(obj)
12
+ !is_blank?(obj)
13
13
  end
14
14
 
15
15
  def valid_request?(params)
16
16
  return false if !params.is_a?(Hash)
17
- return false if blank?(params[:fanforce_id])
18
- return false if blank?(params[:app_id]) and blank?(params[:behavior_id]) and blank?(params[:module_id]) and blank?(params[:widget_id])
17
+ return false if is_blank?(params[:fanforce_id])
18
+ return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id])
19
19
  return true
20
20
  end
21
21
 
22
22
  def valid_install_request?(params)
23
23
  return false if !params.is_a?(Hash)
24
- return false if blank?(params[:fanforce_id])
25
- return false if blank?(params[:app_id]) and blank?(params[:behavior_id]) and blank?(params[:module_id]) and blank?(params[:widget_id])
26
- return false if blank?(params[:api_key])
24
+ return false if is_blank?(params[:fanforce_id])
25
+ return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id])
26
+ return false if is_blank?(params[:api_key])
27
27
  return true
28
28
  end
29
29
 
30
30
  def valid_uninstall_request?(params)
31
31
  return false if !params.is_a?(Hash)
32
- return false if blank?(params[:fanforce_id])
33
- return false if blank?(params[:app_id]) and blank?(params[:behavior_id]) and blank?(params[:module_id]) and blank?(params[:widget_id])
34
- return false if blank?(params[:api_key])
32
+ return false if is_blank?(params[:fanforce_id])
33
+ return false if is_blank?(params[:app_id]) and is_blank?(params[:behavior_id]) and is_blank?(params[:module_id]) and is_blank?(params[:widget_id])
34
+ return false if is_blank?(params[:api_key])
35
35
  return true
36
36
  end
37
37
 
38
38
  def parse_params(params)
39
- if present?(params[:app_id])
39
+ if is_present?(params[:app_id])
40
40
  plugin_type = :app
41
41
  plugin_id = params[:app_id]
42
- elsif present?(params[:behavior_id])
42
+ elsif is_present?(params[:behavior_id])
43
43
  plugin_type = :behavior
44
44
  plugin_id = params[:behavior_id]
45
- elsif present?(params[:module_id])
45
+ elsif is_present?(params[:module_id])
46
46
  plugin_type = :module
47
47
  plugin_id = params[:module_id]
48
- elsif present?(params[:widget_id])
48
+ elsif is_present?(params[:widget_id])
49
49
  plugin_type = :widget
50
50
  plugin_id = params[:widget_id]
51
51
  end
@@ -53,34 +53,47 @@ module Fanforce::Utils
53
53
  end
54
54
 
55
55
  def parse_url(raw_url)
56
- return if blank?(raw_url)
56
+ return if is_blank?(raw_url)
57
57
  url = URI::parse(raw_url)
58
58
  query_params = QueryStringParser.qs_parse(url.query).inject({}) do |result, (k,v)|
59
59
  result[k] = v if k !~ /^ff_.+/
60
60
  result
61
61
  end
62
- query_string = to_query_string(Hash[query_params.sort]) if present?(query_params)
62
+ query_string = to_query_string(Hash[query_params.sort]) if is_present?(query_params)
63
63
  _external_id = url.host + url.path
64
- clean_url = "#{url.scheme}://#{url.host}#{(if ![80,443].include?(url.port) then ":#{url.port}" end)}#{url.path}#{(if present?(query_string) then "?#{query_string}" end)}"
64
+ 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)}"
65
65
 
66
66
  { _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 }
67
67
  end
68
68
 
69
- def to_query_string(params, namespace = nil)
70
- if params.is_a?(String)
71
- params
72
- elsif params.is_a?(Array)
73
- params.collect { |value| to_query_chunk(value, "#{namespace}[]") }.join '&'
74
- elsif params.is_a?(Hash)
75
- params.collect { |key, value| to_query_chunk(value, namespace ? "#{namespace}[#{key}]" : key) }.sort * '&'
69
+ def to_query_string(obj, namespace=nil)
70
+ return '' if is_blank?(obj)
71
+ if obj.is_a?(Array)
72
+ obj.collect { |value| to_query_string(value, "#{namespace}[]") }.join '&'
73
+ elsif obj.is_a?(Hash)
74
+ obj.collect { |key, value| to_query_string(value, namespace ? "#{namespace}[#{key}]" : key) }.sort * '&'
75
+ elsif obj.is_a?(Object)
76
+ require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
77
+ "#{CGI.escape(to_param(namespace))}=#{CGI.escape(to_param(obj).to_s)}"
76
78
  else
77
- raise "params must be an Object, Hash, or Array"
79
+ raise "Argument must be an object, hash, or array; instead it was a #{obj.class}"
78
80
  end
79
81
  end
80
82
 
81
- def to_query_chunk(value,key)
82
- require 'cgi' unless defined?(CGI) && defined?(CGI::escape)
83
- "#{CGI.escape(to_query_string(key))}=#{CGI.escape(to_query_string(value).to_s)}"
83
+ def to_param(obj, namespace=nil)
84
+ if obj.is_a?(Object)
85
+ obj.to_s
86
+ elsif obj.is_a?(Array)
87
+ collect { |e| e.to_param }.join '/'
88
+ elsif obj.is_a?(Hash)
89
+ to_query_string(obj, namespace)
90
+ elsif obj.is_a?(NilClass)
91
+ obj
92
+ elsif obj.is_a?(TrueClass)
93
+ obj
94
+ elsif obj.is_a?(FalseClass)
95
+ obj
96
+ end
84
97
  end
85
98
 
86
99
  def decode_json(str, symbolize_keys=true)
@@ -1,3 +1,3 @@
1
1
  class Fanforce
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.2"
3
3
  end
@@ -7,12 +7,16 @@ require 'fanforce'
7
7
  describe Fanforce do
8
8
 
9
9
  it "should test if var is blank" do
10
- assert Fanforce.blank?(nil)
11
- assert !Fanforce.blank?("value")
10
+ assert Fanforce.is_blank?(nil)
11
+ assert !Fanforce.is_blank?("value")
12
12
 
13
13
  ff = Fanforce.new
14
- assert ff.blank?(nil)
15
- assert !ff.blank?("value")
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')
16
20
  end
17
21
 
18
22
  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.2.0
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: