slidepay 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea9149688f04b38ba2d7253f8805ebe167a518b9
4
+ data.tar.gz: 925a135adacc97d7f5c25d653a10e4b08da01996
5
+ SHA512:
6
+ metadata.gz: 4c50c362f7d73afcd7da67a06a423affc13b1a4701041057af683a4b3c159e097aeaa91b3642fffb1871addd978b838fe7502845188fe6cd206fa94972f8d61f
7
+ data.tar.gz: 37ea01471930e0a3691a3904c8b17a21dc8584e0e2f0025841088f4c80e1d3a5fdbfa78a3634351d91d32173eb8d1c195d9d6fd3468c5cfd11478d300a5164a3
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ environment.rb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ slidepay-ruby-sdk
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p247
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ bundler_args: --without development
3
+ rvm:
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - 2.0.0
7
+ script: "bundle exec rake spec"
8
+ addons:
9
+ code_climate:
10
+ repo_token: 9960cb8262e8d38cddff9d66fa7f2e64d1fd8227e78a8829b0eab43647a01903
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slidepay-ruby-sdk.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "rspec", "~> 2.6"
8
+ gem "rake"
9
+ gem "codeclimate-test-reporter"
10
+ end
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ notification :growl
2
+
3
+ ignore %r{^doc}
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/.+\.rb$})
7
+ watch(%r{^lib/slidepay/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
8
+ watch(%r{^lib/slidepay/resources/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 SlidePay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,135 @@
1
+
2
+
3
+ # Slidepay Ruby Sdk
4
+
5
+ [![Build Status](https://travis-ci.org/SlidePay/slidepay-ruby-sdk.png)](https://travis-ci.org/SlidePay/slidepay-ruby-sdk) [![Code Climate](https://codeclimate.com/repos/5236cb7013d6371e46004010/badges/f963bebb1565c5419c78/gpa.png)](https://codeclimate.com/repos/5236cb7013d6371e46004010/feed)
6
+
7
+ First version of the SlidePay Ruby SDK
8
+
9
+ ## Dependencies
10
+
11
+ We depend on these fantastic libraries:
12
+
13
+ - [rest-client](https://github.com/rest-client/rest-client)
14
+ - [multi_json](https://github.com/intridea/multi_json)
15
+ - [json](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html)
16
+
17
+ ## Building the Gem
18
+
19
+ From the root of the repository, run the ```gem build``` command:
20
+
21
+ ```bash
22
+ $ gem build slidepay.gemspec
23
+ ```
24
+
25
+ ## How Does SlidePay's API Work?
26
+
27
+ Check out our [main documentation](https://getcube.atlassian.net/wiki/display/CDP/Getting+Started) for detailed information about how the API works, as well as information about functionality not yet wrapped by this library.
28
+
29
+ ## The Ruby SDK Basics
30
+
31
+ Requests can be made three ways:
32
+
33
+ 1. Using the SlidePay request methods directly with the desired API url and relevant request JSON
34
+ 2. [Coming Soon] Using an instance of the SlidePay::Client class to save an ApiResource, such as a payment, order, or item
35
+ 3. Using an instance of a SlidePay::ApiResource class, such as SlidePay::ApiKey or SlidePay::Payment
36
+
37
+ ## Authentication
38
+
39
+ A request to SlidePay is considered authenticated if either an api_key or token are present in the request. Information about how those are sent, and how they are obtained, can be found on [the authentication page of the api documentation](https://getcube.atlassian.net/wiki/display/CDP/Making+your+first+API+call%3A+authentication).
40
+
41
+ Both the api_key and token can be set in a global, instance, and request context. The order of precedence goes from most to least specific, and tokens are of greater significance than api_keys. This means that an api_key provided to an instance of an ApiResource would be used over a global token, but would be ignored in the face of a token set on that same object, and would yield to either provided to a single request method.
42
+
43
+ Either of these fields can be set globally by assigning ```SlidePay.token``` or ```SlidePay.api_key```:
44
+
45
+ ```ruby
46
+ SlidePay.token = "MY_TOKEN"
47
+ ```
48
+
49
+ ```ruby
50
+ SlidePay.api_key = "MY_API_KEY"
51
+ ```
52
+
53
+ The global token can also be set using the global SlidePay authentication method:
54
+
55
+ ```ruby
56
+ SlidePay.authenticate(email, password)
57
+ ```
58
+
59
+ You can retrieve a token for use in any context with the retrieve_token method, which is similar to SlidePay.authenticate except that it returns the token string and does not set a global token value.
60
+
61
+ ```ruby
62
+ SlidePay.authenticate(email, password)
63
+ ```
64
+
65
+ These fields can also be used on an instance or per-request level by either passing them into the SlidePay module request methods as a field in a hash, or by assigning instance variables on a SlidePay::Client.
66
+
67
+ ```ruby
68
+ SlidePay.get(path: "payment/#{payment_id}", token: "MY_TOKEN")
69
+ ```
70
+ or
71
+ ```ruby
72
+ SlidePay.get(path: "payment/#{payment_id}", api_key: "MY_API_KEY")
73
+ ```
74
+
75
+ ## Resources
76
+
77
+ SlidePay::ApiResources are classes that encapsulate RESTful API resources. API interaction relative to these resources is handled by the following descriptive methods:
78
+
79
+ - ```save ```
80
+ - ```destroy ```
81
+ - ```retrieve ```
82
+
83
+ The ```save``` method can handle both creation, via POST, and updating, via PUT, and determines which verb is appropriate by the ```ApiResource.is_new?``` method, which checks for the presence of an id in the resource.
84
+
85
+ ## Payments
86
+
87
+ Payments are much like resources, except they cannot be updated or destroyed once created. Two more relevant instance methods for interacting with payments are provided.
88
+
89
+ To process a payment, supply a JSON representation of a valid [simple_payment](https://getcube.atlassian.net/wiki/display/CDP/Processing+a+Simple+Payment) object on instantiation, and then call that instance's ```process``` method:
90
+
91
+ ```ruby
92
+ p = SlidePay::Payment.new(simple_payment_json)
93
+ p.process()
94
+ ```
95
+
96
+ To refund a payment, call an existing payment's ```refund``` method, or POST to the ```payment/refund/:payment_id``` API path:
97
+
98
+ ```ruby
99
+ # Refund from an object
100
+ p.refund()
101
+
102
+ # Refund using SlidePay.post
103
+ SlidePay.post(path: 'payment/refund/#{payment_id}')
104
+ ```
105
+
106
+ ## Testing
107
+
108
+ Test are in the spec directory, and can be run from the root of the repository:
109
+
110
+ ```bash
111
+ $ rspec spec
112
+ ```
113
+
114
+ Some tests will require that you have an active SlidePay account on the development server, and that you supply those credentials in an ```environment.rb``` file in the root of this repository.
115
+
116
+ Your populated environment.rb should look something like this:
117
+
118
+ ```ruby
119
+ # Environment variables for testing. This Should NOT be included in VCS
120
+ ENV["email"] = "email@example.com"
121
+ ENV["password"] = "really_secure_password"
122
+ ENV["api_key"] = "super-secret-api-key-that-you-never-share"
123
+ ```
124
+
125
+ ## Notes
126
+
127
+ The SlidePay::Client class is largely untested and likely non-functional.
128
+
129
+ Though this document rarely mentions authentication in most examples following the section above dedicated to it, an api_key or token, and an endpoint, may be supplied to any object or module that can perform an API request. This flexibility allows for interacting with a single instance of a single ApiResource class without having to use the SlidePay module methods directly, or having to use an instance of the SlidePay::Client class. It also accommodates those developers who may wish to authenticate many SlidePay accounts within a single thread, such as inside a request context of a Rails application, or in a scheduled task, without having to repeatedly set the SlidePay.token or SlidePay.api_key global values.
130
+
131
+ ## License
132
+
133
+ The MIT License (MIT)
134
+
135
+ Copyright (c) 2013 SlidePay
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "rake"
2
+ require "rspec/core/rake_task"
3
+ require "bundler/gem_tasks"
4
+ require 'rspec'
5
+
6
+ desc "Run the spec suite"
7
+ RSpec::Core::RakeTask.new(:spec) { |t| }
8
+
9
+ desc "Default task to run the spec suite"
10
+ task :default => [:spec]
data/lib/slidepay.rb ADDED
@@ -0,0 +1,145 @@
1
+ # Dependencies
2
+ require "rest-client"
3
+ require "multi_json"
4
+ require "json"
5
+
6
+ # SlidePay Modules and
7
+ require "slidepay/version"
8
+ require "slidepay/config"
9
+ require "slidepay/response"
10
+
11
+ # Instance-Level API Interaction
12
+ require "slidepay/client"
13
+
14
+ # CRUD Capable Resources
15
+ require "slidepay/resources/api_resource"
16
+ require "slidepay/resources/api_key"
17
+ require "slidepay/resources/payment"
18
+ require "slidepay/resources/bank_account"
19
+
20
+ module SlidePay
21
+ class << self
22
+ attr_accessor :token, :api_key, :endpoint
23
+
24
+ def get_endpoint_option(options)
25
+ if options[:endpoint]
26
+ options[:endpoint]
27
+ else
28
+ @endpoint
29
+ end
30
+ end
31
+
32
+ def get_auth_option(request_options_hash)
33
+ options = {}
34
+ if request_options_hash[:token]
35
+ options["x-cube-token"] = request_options_hash[:token]
36
+ elsif request_options_hash[:api_key]
37
+ options["x-cube-api-key"] = request_options_hash[:api_key]
38
+ elsif @token
39
+ options["x-cube-token"] = @token
40
+ elsif @api_key
41
+ options["x-cube-api-key"] = @api_key
42
+ end
43
+
44
+ options
45
+ end
46
+
47
+ def authenticate(email, password)
48
+ response = retrieve_token(email, password)
49
+ if response.was_successful?
50
+ @token = response.data
51
+ true
52
+ else
53
+ false
54
+ end
55
+ end
56
+
57
+ def retrieve_token(email, password)
58
+ get(path: "login", :email => email, :password => password)
59
+ end
60
+
61
+ def retrieve_endpoint(email)
62
+ get(path: "endpoint", :email => email)
63
+ end
64
+
65
+ def request(type, request_options_hash)
66
+ options = { "Content-Type" => "application/json" }
67
+ options.merge! get_auth_option request_options_hash
68
+
69
+ if request_options_hash[:email]
70
+ options["x-cube-email"] = request_options_hash[:email]
71
+ end
72
+
73
+ if request_options_hash[:password]
74
+ options["x-cube-password"] = request_options_hash[:password]
75
+ end
76
+
77
+ url = "#{get_endpoint_option(request_options_hash)}#{request_options_hash[:path]}"
78
+ data = request_options_hash[:data]
79
+
80
+ begin
81
+ case type
82
+ when "GET"
83
+ response = RestClient.get url, options
84
+ when "PUT"
85
+ response = RestClient.put url, data, options
86
+ when "POST"
87
+ response = RestClient.post url, data, options
88
+ when "DELETE"
89
+ response = RestClient.delete url, options
90
+ else
91
+ raise Exception.new("Invalid request type specified")
92
+ end
93
+ rescue => e
94
+ # raise Exception.new("Request to #{url} failed with status code: #{e}")
95
+ response = e.response
96
+ end
97
+
98
+ Response.new(response)
99
+ end
100
+
101
+ def get(request_options_hash)
102
+ if request_options_hash.is_a? String
103
+ request_options_hash = { :path => request_options_hash }
104
+ end
105
+
106
+ request("GET", request_options_hash)
107
+ end
108
+
109
+ def post(request_options_hash)
110
+ request("POST", request_options_hash)
111
+ end
112
+
113
+ def put(request_options_hash)
114
+ request("PUT", request_options_hash)
115
+ end
116
+
117
+ def delete(request_options_hash)
118
+ if request_options_hash.is_a? String
119
+ request_options_hash = { :path => request_options_hash }
120
+ end
121
+
122
+ request("DELETE", request_options_hash)
123
+ end
124
+
125
+ def configure(options_hash={})
126
+ if options_hash[:api_key]
127
+ @api_key = options_hash[:api_key]
128
+ end
129
+
130
+ if options_hash[:token]
131
+ @token = options_hash[:token]
132
+ end
133
+
134
+ if options_hash[:endpoint]
135
+ @endpoint = options_hash[:endpoint]
136
+ elsif options_hash[:development] == true
137
+ @endpoint = DEV_API_URL
138
+ else
139
+ @endpoint = SUPERVISOR_URL
140
+ end
141
+ end
142
+ end
143
+
144
+ self.configure
145
+ end
@@ -0,0 +1,99 @@
1
+ module SlidePay
2
+ class Client
3
+ attr_accessor :endpoint, :email, :password, :token, :api_key
4
+
5
+ def initialize(options={})
6
+ if options[:endpoint]
7
+ @endpoint = options[:endpoint]
8
+ end
9
+ if options[:email]
10
+ @email = options[:email]
11
+ end
12
+ if options[:password]
13
+ @password = options[:password]
14
+ end
15
+ if options[:token]
16
+ @token = options[:token]
17
+ end
18
+ if options[:api_key]
19
+ @api_key = options[:api_key]
20
+ end
21
+ end
22
+
23
+ def is_authenticated?
24
+ @token != nil || @api_key != nil
25
+ end
26
+
27
+ def authenticate(email=nil, password=nil)
28
+ email = email || @email
29
+ password = password || @password
30
+
31
+ response = SlidePay.retrieve_token(email, password)
32
+
33
+ if response.was_successful?
34
+ @token = response.data
35
+ true
36
+ else
37
+ false
38
+ end
39
+ end
40
+
41
+ # Base Request Methods
42
+ def get(request_params)
43
+ if request_params.is_a? String
44
+ SlidePay.get(path: request_params, api_key: @api_key, token: @token, endpoint: @endpoint)
45
+ else
46
+ request_params.merge! api_key: @api_key, token: @token, endpoint: @endpoint
47
+ SlidePay.get(request_params)
48
+ end
49
+ end
50
+
51
+ def put(request_params)
52
+ request_params.merge! api_key: @api_key, token: @token, endpoint: @endpoint
53
+ SlidePay.put(request_params)
54
+ end
55
+
56
+ def post(request_params)
57
+ request_params.merge! api_key: @api_key, token: @token, endpoint: @endpoint
58
+ SlidePay.post(request_params)
59
+ end
60
+
61
+ def delete(request_params)
62
+ if request_params.is_a? String
63
+ SlidePay.delete(path: request_params, api_key: @api_key, token: @token, endpoint: @endpoint)
64
+ else
65
+ request_params.merge! api_key: @api_key, token: @token, endpoint: @endpoint
66
+ SlidePay.delete(request_params)
67
+ end
68
+ end
69
+
70
+
71
+ # Resource Methods
72
+ def list(resource)
73
+ response = SlidePay.get(path: resource.url_root, api_key: @api_key, token: @token, endpoint: @endpoint)
74
+ if response.was_successful?
75
+ resources = []
76
+ response.data.each do |resource_instance|
77
+ resources.push resource.class.new(resource_instance)
78
+ end
79
+ else
80
+ resources = []
81
+ end
82
+
83
+ resources
84
+ end
85
+
86
+ def retrieve(resource)
87
+ resource.retrieve(api_key: @api_key, token: @token, endpoint: @endpoint)
88
+ end
89
+
90
+ def save(resource)
91
+ resource.save(api_key: @api_key, token: @token, endpoint: @endpoint)
92
+ end
93
+
94
+ def destroy(resource)
95
+ resource.destroy(api_key: @api_key, token: @token, endpoint: @endpoint)
96
+ end
97
+
98
+ end
99
+ end