pingpp 2.0.15 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,9 @@
1
1
  module Pingpp
2
2
  module APIOperations
3
3
  module Create
4
- module ClassMethods
5
- def create(params={}, api_key=nil)
6
- response, api_key = Pingpp.request(:post, self.url, api_key, params)
7
- Util.convert_to_pingpp_object(response, api_key)
8
- end
9
- end
10
-
11
- def self.included(base)
12
- base.extend(ClassMethods)
4
+ def create(params={}, opts={})
5
+ response, opts = request(:post, resource_url(opts), params, opts)
6
+ Util.convert_to_pingpp_object(response, opts)
13
7
  end
14
8
  end
15
9
  end
@@ -1,9 +1,10 @@
1
1
  module Pingpp
2
2
  module APIOperations
3
3
  module Delete
4
- def delete(params = {})
5
- response, api_key = Pingpp.request(:delete, url, @api_key, params)
6
- refresh_from(response, api_key)
4
+ def delete(params={}, opts={})
5
+ opts = Util.normalize_opts(opts)
6
+ response, opts = request(:delete, resource_url(opts), params, opts)
7
+ initialize_from(response, opts)
7
8
  end
8
9
  end
9
10
  end
@@ -1,16 +1,14 @@
1
1
  module Pingpp
2
2
  module APIOperations
3
3
  module List
4
- module ClassMethods
5
- def all(filters={}, api_key=nil)
6
- response, api_key = Pingpp.request(:get, url, api_key, filters)
7
- Util.convert_to_pingpp_object(response, api_key)
8
- end
9
- end
4
+ def list(filters={}, opts={})
5
+ opts = Util.normalize_opts(opts)
10
6
 
11
- def self.included(base)
12
- base.extend(ClassMethods)
7
+ response, opts = request(:get, resource_url(opts), filters, opts)
8
+ ListObject.construct_from(response, opts)
13
9
  end
10
+
11
+ alias :all :list
14
12
  end
15
13
  end
16
14
  end
@@ -0,0 +1,46 @@
1
+ module Pingpp
2
+ module APIOperations
3
+ module Request
4
+ module ClassMethods
5
+ OPTS_KEYS_TO_PERSIST = Set[:api_key, :api_base, :app, :user]
6
+
7
+ def request(method, url, params={}, opts={})
8
+ opts = Util.normalize_opts(opts)
9
+
10
+ headers = opts.clone
11
+ api_key = headers.delete(:api_key)
12
+ api_base = headers.delete(:api_base)
13
+ headers.delete(:app)
14
+ headers.delete(:user)
15
+ headers.delete(:parents)
16
+ # Assume all remaining opts must be headers
17
+
18
+ response, opts[:api_key] = Pingpp.request(
19
+ method, url, api_key, params, headers, api_base
20
+ )
21
+
22
+ # Hash#select returns an array before 1.9
23
+ opts_to_persist = {}
24
+ opts.each do |k, v|
25
+ if OPTS_KEYS_TO_PERSIST.include?(k)
26
+ opts_to_persist[k] = v
27
+ end
28
+ end
29
+
30
+ [response, opts_to_persist]
31
+ end
32
+ end
33
+
34
+ def self.included(base)
35
+ base.extend(ClassMethods)
36
+ end
37
+
38
+ protected
39
+
40
+ def request(method, url, params={}, opts={})
41
+ opts = @opts.merge(Util.normalize_opts(opts))
42
+ self.class.request(method, url, params, opts)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -2,67 +2,40 @@ module Pingpp
2
2
  module APIOperations
3
3
  module Update
4
4
  module ClassMethods
5
- def update(id, params={}, api_key=nil)
6
- response, opts = Pingpp.request(:put, "#{self.url}/#{id}", api_key, params)
5
+ def update(id, params={}, opts={})
6
+ response, opts = request(:put, "#{resource_url(opts)}/#{id}", params, opts)
7
7
  Util.convert_to_pingpp_object(response, opts)
8
8
  end
9
9
  end
10
10
 
11
- def save(opts={})
12
- values = serialize_params(self).merge(opts)
11
+ def save(params={}, opts={})
12
+ update_attributes(params)
13
13
 
14
- if @values[:metadata]
15
- values[:metadata] = serialize_metadata
16
- end
14
+ params = params.reject { |k, _| respond_to?(k) }
17
15
 
18
- if values.length > 0
19
- values.delete(:id)
16
+ values = self.serialize_params(self).merge(params)
20
17
 
21
- response, api_key = Pingpp.request(:put, url, @api_key, values)
22
- refresh_from(response, api_key)
23
- end
24
- self
25
- end
18
+ values.delete(:id)
26
19
 
27
- def serialize_metadata
28
- if @unsaved_values.include?(:metadata)
29
- # the metadata object has been reassigned
30
- # i.e. as object.metadata = {key => val}
31
- metadata_update = @values[:metadata] # new hash
32
- new_keys = metadata_update.keys.map(&:to_sym)
33
- # remove keys at the server, but not known locally
34
- keys_to_unset = @previous_metadata.keys - new_keys
35
- keys_to_unset.each {|key| metadata_update[key] = ''}
20
+ response, opts = request(:put, save_url, values, opts)
21
+ initialize_from(response, opts)
36
22
 
37
- metadata_update
38
- else
39
- # metadata is a PingppObject, and can be serialized normally
40
- serialize_params(@values[:metadata])
41
- end
23
+ self
42
24
  end
43
25
 
44
- def serialize_params(obj)
45
- case obj
46
- when nil
47
- ''
48
- when PingppObject
49
- unsaved_keys = obj.instance_variable_get(:@unsaved_values)
50
- obj_values = obj.instance_variable_get(:@values)
51
- update_hash = {}
26
+ def self.included(base)
27
+ base.extend(ClassMethods)
28
+ end
52
29
 
53
- unsaved_keys.each do |k|
54
- update_hash[k] = serialize_params(obj_values[k])
55
- end
30
+ private
56
31
 
57
- update_hash
32
+ def save_url
33
+ if self[:id] == nil && self.class.respond_to?(:create)
34
+ self.class.resource_url
58
35
  else
59
- obj
36
+ resource_url
60
37
  end
61
38
  end
62
-
63
- def self.included(base)
64
- base.extend(ClassMethods)
65
- end
66
39
  end
67
40
  end
68
41
  end
@@ -1,30 +1,50 @@
1
1
  module Pingpp
2
2
  class APIResource < PingppObject
3
+ include Pingpp::APIOperations::Request
4
+
3
5
  def self.class_name
4
6
  self.name.split('::')[-1]
5
7
  end
6
8
 
7
- def self.url
9
+ def self.object_name
10
+ class_name.downcase
11
+ end
12
+
13
+ def self.uri_object_name
14
+ object_name
15
+ end
16
+
17
+ def self.resource_url(opts={})
8
18
  if self == APIResource
9
19
  raise NotImplementedError.new('APIResource is an abstract class. You should perform actions on its subclasses (Charge, etc.)')
10
20
  end
11
- "/v1/#{CGI.escape(class_name.downcase)}s"
21
+
22
+ if opts.has_key?(:parents)
23
+ if opts[:parents].is_a?(Array)
24
+ "/v1/#{opts[:parents].join('/')}/#{CGI.escape(uri_object_name.downcase)}s"
25
+ else
26
+ raise ArgumentError.new("opts[:parents] should be an Array")
27
+ end
28
+ else
29
+ "/v1/#{CGI.escape(uri_object_name.downcase)}s"
30
+ end
12
31
  end
13
32
 
14
- def url
33
+ def resource_url(opts={})
15
34
  unless id = self['id']
16
35
  raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", 'id')
17
36
  end
18
- "#{self.class.url}/#{CGI.escape(id)}"
37
+ "#{self.class.resource_url(opts)}/#{CGI.escape(id)}"
19
38
  end
20
39
 
21
40
  def refresh
22
- response, api_key = Pingpp.request(:get, url, @api_key, @retrieve_options)
23
- refresh_from(response, api_key)
41
+ response, opts = request(:get, resource_url(@opts), @retrieve_params)
42
+ initialize_from(response, opts)
24
43
  end
25
44
 
26
- def self.retrieve(id, api_key=nil)
27
- instance = self.new(id, api_key)
45
+ def self.retrieve(id, opts={})
46
+ opts = Util.normalize_opts(opts)
47
+ instance = self.new(id, opts)
28
48
  instance.refresh
29
49
  instance
30
50
  end
@@ -0,0 +1,14 @@
1
+ module Pingpp
2
+ class BatchRefund < APIResource
3
+ extend Pingpp::APIOperations::Create
4
+ extend Pingpp::APIOperations::List
5
+
6
+ def self.object_name
7
+ 'batch_refund'
8
+ end
9
+
10
+ def self.resource_url(opts={})
11
+ '/v1/batch_refunds'
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module Pingpp
2
+ class BatchTransfer < APIResource
3
+ extend Pingpp::APIOperations::Create
4
+ extend Pingpp::APIOperations::List
5
+
6
+ def self.object_name
7
+ 'batch_transfer'
8
+ end
9
+
10
+ def self.resource_url(opts={})
11
+ '/v1/batch_transfers'
12
+ end
13
+ end
14
+ end
data/lib/pingpp/charge.rb CHANGED
@@ -1,13 +1,12 @@
1
1
  module Pingpp
2
2
  class Charge < APIResource
3
- include Pingpp::APIOperations::List
4
- include Pingpp::APIOperations::Create
5
- include Pingpp::APIOperations::Update
3
+ extend Pingpp::APIOperations::Create
4
+ extend Pingpp::APIOperations::List
6
5
 
7
6
  private
8
7
 
9
8
  def refund_url
10
- url + '/refunds'
9
+ resource_url + '/refunds'
11
10
  end
12
11
  end
13
12
  end
@@ -0,0 +1,10 @@
1
+ module Pingpp
2
+ class Customs < APIResource
3
+ extend Pingpp::APIOperations::Create
4
+ extend Pingpp::APIOperations::List
5
+
6
+ def self.resource_url(opts={})
7
+ '/v1/customs'
8
+ end
9
+ end
10
+ end
@@ -3,8 +3,9 @@ module Pingpp
3
3
  attr_accessor :param
4
4
  attr_accessor :code
5
5
 
6
- def initialize(message, code, param=nil, http_status=nil, http_body=nil, json_body=nil)
7
- super(message, http_status, http_body, json_body)
6
+ def initialize(message, code, param=nil, http_status=nil, http_body=nil,
7
+ json_body=nil, http_headers=nil)
8
+ super(message, http_status, http_body, json_body, http_headers)
8
9
  @code = code
9
10
  @param = param
10
11
  end
@@ -2,8 +2,9 @@ module Pingpp
2
2
  class InvalidRequestError < PingppError
3
3
  attr_accessor :param
4
4
 
5
- def initialize(message, param, http_status=nil, http_body=nil, json_body=nil)
6
- super(message, http_status, http_body, json_body)
5
+ def initialize(message, param, http_status=nil, http_body=nil, json_body=nil,
6
+ http_headers=nil)
7
+ super(message, http_status, http_body, json_body, http_headers)
7
8
  @param = param
8
9
  end
9
10
  end
@@ -4,12 +4,15 @@ module Pingpp
4
4
  attr_reader :http_status
5
5
  attr_reader :http_body
6
6
  attr_reader :json_body
7
+ attr_reader :http_headers
7
8
 
8
- def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil)
9
+ def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil,
10
+ http_headers=nil)
9
11
  @message = message
10
12
  @http_status = http_status
11
13
  @http_body = http_body
12
14
  @json_body = json_body
15
+ @http_headers = http_headers || {}
13
16
  end
14
17
 
15
18
  def to_s
@@ -0,0 +1,4 @@
1
+ module Pingpp
2
+ class RateLimitError < PingppError
3
+ end
4
+ end
data/lib/pingpp/event.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  module Pingpp
2
2
  class Event < APIResource
3
- include Pingpp::APIOperations::List
4
3
 
5
- def self.url
4
+ def self.resource_url(opts={})
6
5
  '/v1/events'
7
6
  end
8
-
9
7
  end
10
8
  end
@@ -1,12 +1,12 @@
1
1
  module Pingpp
2
2
  class Identification < APIResource
3
- include Pingpp::APIOperations::Create
3
+ extend Pingpp::APIOperations::Create
4
4
 
5
- def self.identify(params={}, api_key=nil)
6
- create(params, api_key)
5
+ def self.identify(params={}, opts={})
6
+ create(params, opts)
7
7
  end
8
8
 
9
- def self.url
9
+ def self.resource_url(opts={})
10
10
  '/v1/identification'
11
11
  end
12
12
 
@@ -1,5 +1,8 @@
1
1
  module Pingpp
2
2
  class ListObject < PingppObject
3
+ include Pingpp::APIOperations::List
4
+ include Pingpp::APIOperations::Request
5
+ include Pingpp::APIOperations::Create
3
6
 
4
7
  def [](k)
5
8
  case k
@@ -14,22 +17,19 @@ module Pingpp
14
17
  self.data.each(&blk)
15
18
  end
16
19
 
17
- def retrieve(id, api_key=nil)
18
- api_key ||= @api_key
19
- response, api_key = Pingpp.request(:get,"#{url}/#{CGI.escape(id)}", api_key)
20
- Util.convert_to_pingpp_object(response, api_key)
20
+ def empty?
21
+ self.data.empty?
21
22
  end
22
23
 
23
- def create(params={}, api_key=nil)
24
- api_key ||= @api_key
25
- response, api_key = Pingpp.request(:post, url, api_key, params)
26
- Util.convert_to_pingpp_object(response, api_key)
24
+ def retrieve(id, opts={})
25
+ id, retrieve_params = Util.normalize_id(id)
26
+ response, opts = request(:get, "#{resource_url(opts)}/#{CGI.escape(id)}", retrieve_params, opts)
27
+ Util.convert_to_pingpp_object(response, opts)
27
28
  end
28
29
 
29
- def all(params={}, api_key=nil)
30
- api_key ||= @api_key
31
- response, api_key = Pingpp.request(:get, url, api_key, params)
32
- Util.convert_to_pingpp_object(response, api_key)
30
+ def resource_url(opts={})
31
+ self.url ||
32
+ raise(ArgumentError, "List object does not contain a 'url' field.")
33
33
  end
34
34
  end
35
35
  end