paysio 1.0.5 → 1.0.6

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/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- paysio (1.0.4)
4
+ paysio (1.0.5)
5
5
  activemodel
6
6
  activesupport
7
7
  oj (> 2)
data/README.rdoc CHANGED
@@ -30,8 +30,29 @@
30
30
  charge.refund
31
31
 
32
32
 
33
- == Actions: redirect to charge after create (e.g. in Ruby on Rails)
33
+ == Sample: redirect to charge after create (e.g. in Ruby on Rails)
34
34
  def create
35
35
  charge = Paysio::Charge.create(amount: 100, payment_system_id: 't_123', description: 'test charge')
36
36
  redirect_to charge.redirect
37
37
  end
38
+
39
+ == Sample: handle webhook events
40
+ def webhook
41
+ Paysio.api_key = 'HZClZur5OW3BYimWSydQNsArbph2L7IRo0ql8HK'
42
+ event = Paysio::Event.new(params)
43
+ end
44
+
45
+ == Sample: handle webhook events, with getting event from api
46
+ def webhook
47
+ Paysio.api_key = 'HZClZur5OW3BYimWSydQNsArbph2L7IRo0ql8HK'
48
+ event = Paysio::Event.retrieve(params['id'])
49
+ if event.data.object == 'charge':
50
+ if event.type == 'charge.success'
51
+ pass #logic for charge success
52
+ elsif event.type == 'charge.failure'
53
+ pass #logic for charge failure
54
+ elsif event.type == 'charge.refund'
55
+ pass #logic for charge refund
56
+ end
57
+ end
58
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.5
1
+ 1.0.6
data/lib/paysio/client.rb CHANGED
@@ -3,7 +3,7 @@ module Paysio
3
3
  class << self
4
4
  def request(method, path, params = {}, headers = {})
5
5
  unless Paysio.api_key
6
- raise Paysio::Exceptions::NotAuthorized, "Please specify Paysio.api_key"
6
+ raise Paysio::Errors::Unauthorized, "Please specify Paysio.api_key"
7
7
  end
8
8
  headers = {
9
9
  :user_agent => "Pays.io RubyClient/#{Paysio::VERSION}",
@@ -23,18 +23,34 @@ module Paysio
23
23
  else
24
24
  opts[:payload] = stringify_params(params)
25
25
  end
26
- response = self.execute(opts)
26
+ response = execute(opts)
27
27
  body, code = response.body, response.code
28
28
  begin
29
29
  resp = Paysio::JSON.decode(body)
30
30
  rescue Oj::ParseError
31
- raise Paysio::Exceptions::APIError.new("Invalid response object from API: #{body.inspect} (#{code})", code, body)
31
+ raise Paysio::Exceptions::InternalError.new("Invalid response object from API: #{body.inspect} (#{code})", code, body)
32
32
  end
33
33
  Paysio::Resource.build_from(resp, response.headers[:location])
34
34
  end
35
35
 
36
36
  def execute(opts)
37
37
  RestClient::Request.execute(opts)
38
+ puts "test"
39
+ rescue RuntimeError => e
40
+ case e.http_code.to_i
41
+ when 400
42
+ raise Paysio::Errors::BadRequest, e.http_body
43
+ when 401
44
+ raise Paysio::Errors::Unauthorized, e.http_body
45
+ when 403
46
+ raise Paysio::Errors::Forbidden, e.http_body
47
+ when 404
48
+ raise Paysio::Errors::NotFound, e.http_body
49
+ when 500
50
+ raise Paysio::Errors::InternalError, e.http_body
51
+ else
52
+ raise e
53
+ end
38
54
  end
39
55
 
40
56
  def uri_escape(key)
@@ -0,0 +1,9 @@
1
+ module Paysio
2
+ module Errors
3
+ class Unauthorized < Exception; end;
4
+ class InternalError < Exception; end;
5
+ class Forbidden < Exception; end;
6
+ class BadRequest < Exception; end;
7
+ class NotFound < Exception; end;
8
+ end
9
+ end
@@ -5,9 +5,11 @@ module Paysio
5
5
 
6
6
  attr_accessor :redirect
7
7
 
8
- def initialize(id = nil)
8
+ def initialize(values = {})
9
9
  @values = {}
10
- @id = id
10
+ @id = values['id'] if values['id']
11
+ refresh_from(values)
12
+ self.class.define_attribute_methods(attributes.keys)
11
13
  end
12
14
 
13
15
  def refresh_from(values)
@@ -87,9 +89,7 @@ module Paysio
87
89
  else
88
90
  klass = Paysio::Resource
89
91
  end
90
- resource = klass.new(resp['id'])
91
- resource.refresh_from(resp)
92
- resource.class.define_attribute_methods(resource.attributes.keys)
92
+ resource = klass.new(resp)
93
93
  resource.redirect = redirect_url
94
94
  resource
95
95
  else
@@ -1,3 +1,3 @@
1
1
  module Paysio
2
- VERSION = '1.0.5'
2
+ VERSION = '1.0.6'
3
3
  end
data/lib/paysio.rb CHANGED
@@ -9,6 +9,7 @@ require 'oj'
9
9
  require 'paysio/client'
10
10
  require 'paysio/json'
11
11
  require 'paysio/resource'
12
+ require 'paysio/version'
12
13
 
13
14
  # actions
14
15
  require 'paysio/actions/list'
@@ -18,7 +19,7 @@ require 'paysio/actions/destroy'
18
19
  require 'paysio/actions/find'
19
20
 
20
21
  # exceptions
21
- require 'paysio/exceptions'
22
+ require 'paysio/errors'
22
23
 
23
24
  # resources
24
25
  require 'paysio/resources/list'
@@ -40,4 +41,4 @@ module Paysio
40
41
  def self.api_url(path = '')
41
42
  "https://#{api_key}@#{api_base}/#{api_version}#{path}"
42
43
  end
43
- end
44
+ end
@@ -1,4 +1,6 @@
1
1
  require 'spec_helper'
2
+ require 'rest_client'
3
+ require 'ostruct'
2
4
  describe "API Bindings" do
3
5
 
4
6
  before :all do
@@ -7,11 +9,54 @@ describe "API Bindings" do
7
9
 
8
10
  it "should not fetch from network while creating a new Resource" do
9
11
  @client.expects(:get).never
10
- Paysio::Charge.new("someid")
12
+ Paysio::Charge.new(id: "someid")
11
13
  end
12
14
 
13
15
  it "should add redirect url to resource from location" do
14
16
  @client.expects(:get).once.returns(test_response(test_charge, 200, 'http://example.com'))
15
17
  Paysio::Charge.retrieve("someid").redirect == 'http://example.com'
16
18
  end
19
+
20
+ describe "exception handler" do
21
+
22
+ before :all do
23
+ @client = Paysio::Client
24
+ end
25
+
26
+ it "should raise Paysio::Errors::BadRequest on 400" do
27
+ exception = RestClient::Exception.new(OpenStruct.new(code: 400, body: 'message'))
28
+ RestClient::Request.stubs(:execute).raises(exception)
29
+ expect { @client.execute!({}) }.to raise_error(Paysio::Errors::BadRequest)
30
+ end
31
+
32
+ it "should raise Paysio::Errors::Unauthorized on 401" do
33
+ exception = RestClient::Exception.new(OpenStruct.new(code: 401, body: 'message'))
34
+ RestClient::Request.stubs(:execute).raises(exception)
35
+ expect { @client.execute!({}) }.to raise_error(Paysio::Errors::Unauthorized)
36
+ end
37
+
38
+ it "should raise Paysio::Errors::Forbidden on 403" do
39
+ exception = RestClient::Exception.new(OpenStruct.new(code: 403, body: 'message'))
40
+ RestClient::Request.stubs(:execute).raises(exception)
41
+ expect { @client.execute!({}) }.to raise_error(Paysio::Errors::Forbidden)
42
+ end
43
+
44
+ it "should raise Paysio::Errors::NotFound on 404" do
45
+ exception = RestClient::Exception.new(OpenStruct.new(code: 404, body: 'message'))
46
+ RestClient::Request.stubs(:execute).raises(exception)
47
+ expect { @client.execute!({}) }.to raise_error(Paysio::Errors::NotFound)
48
+ end
49
+
50
+ it "should raise Paysio::Errors::InternalError on 500" do
51
+ exception = RestClient::Exception.new(OpenStruct.new(code: 500, body: 'message'))
52
+ RestClient::Request.stubs(:execute).raises(exception)
53
+ expect { @client.execute!({}) }.to raise_error(Paysio::Errors::InternalError)
54
+ end
55
+
56
+ it "should raise Paysio::Errors::Unauthorized if api_key is not provided" do
57
+ Paysio.api_key = nil
58
+ RestClient::Request.stubs(:execute).returns('test')
59
+ expect { @client.request('get', 'test') }.to raise_error(Paysio::Errors::Unauthorized)
60
+ end
61
+ end
17
62
  end
@@ -1,19 +1,21 @@
1
1
  module Paysio
2
2
  class Client
3
- @mock_rest_client = nil
4
-
5
- def self.mock_rest_client(mock_client)
6
- @mock_rest_client = mock_client
7
- end
3
+ class << self
4
+ @mock_rest_client = nil
5
+ def mock_rest_client(mock_client)
6
+ @mock_rest_client = mock_client
7
+ end
8
8
 
9
- def self.execute(opts)
10
- get_params = (opts[:headers] || {})[:params]
11
- post_params = opts[:payload]
12
- case opts[:method]
13
- when :get then @mock_rest_client.get opts[:url], get_params, post_params
14
- when :post then @mock_rest_client.post opts[:url], get_params, post_params
15
- when :put then @mock_rest_client.put opts[:url], get_params, post_params
16
- when :delete then @mock_rest_client.delete opts[:url], get_params, post_params
9
+ alias_method :execute!, :execute
10
+ def execute(opts)
11
+ get_params = (opts[:headers] || {})[:params]
12
+ post_params = opts[:payload]
13
+ case opts[:method]
14
+ when :get then @mock_rest_client.get opts[:url], get_params, post_params
15
+ when :post then @mock_rest_client.post opts[:url], get_params, post_params
16
+ when :put then @mock_rest_client.put opts[:url], get_params, post_params
17
+ when :delete then @mock_rest_client.delete opts[:url], get_params, post_params
18
+ end
17
19
  end
18
20
  end
19
21
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paysio
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-20 00:00:00.000000000 Z
12
+ date: 2013-03-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rest-client
@@ -149,7 +149,7 @@ files:
149
149
  - lib/paysio/actions/list.rb
150
150
  - lib/paysio/actions/update.rb
151
151
  - lib/paysio/client.rb
152
- - lib/paysio/exceptions.rb
152
+ - lib/paysio/errors.rb
153
153
  - lib/paysio/json.rb
154
154
  - lib/paysio/resource.rb
155
155
  - lib/paysio/resources/charge.rb
@@ -1,6 +0,0 @@
1
- module Paysio
2
- module Exceptions
3
- class NotAuthorized < Exception; end;
4
- class APIError < Exception; end;
5
- end
6
- end