apruve 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,42 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ InstalledFiles
7
+ _yardoc
8
+ coverage
9
+ doc/
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
17
+
18
+ # Mac finder artifacts
19
+ .DS_Store
20
+
21
+ # Eclipse-based IDE project files
22
+ .project
23
+
24
+ # Idea-based IDE project files
25
+ .idea
26
+ .idea/*
27
+
28
+ # Textmate project files
29
+ /*.tmproj
30
+
31
+ # vim artifacts
32
+ **.swp
33
+
34
+ # rbenv ruby version for local directory
35
+ .ruby-version
36
+ .rbenv-version
37
+
38
+ #ctags file for vim.
39
+ tags
40
+
41
+ # aptana studio 3
42
+ .settings/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,29 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in apruve.gemspec
4
+ gemspec
5
+
6
+ gem 'faraday'
7
+ gem 'faraday_middleware'
8
+ gem 'addressable'
9
+ gem 'json'
10
+
11
+ group :development do
12
+ gem 'yard'
13
+ gem 'guard', '~> 1.6.2'
14
+ gem 'listen', '~> 1.3.1' # 2.x requires celluloid, not 1.8.7 friendly
15
+ gem 'guard-rspec', '~> 2.4.1'
16
+ end
17
+
18
+ group :test do
19
+
20
+ gem 'ruby-debug-base19x'
21
+ gem 'ruby-debug-ide'
22
+
23
+ gem 'faker'
24
+ gem 'net-http-persistent'
25
+ gem 'rspec', '~> 2.14.1'
26
+ gem 'rake', '~> 10.0.3'
27
+ gem 'vcr', '~> 2.4.0'
28
+ gem 'webmock'
29
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,98 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ apruve (0.0.1)
5
+ addressable (~> 2.3.5)
6
+ faraday (>= 0.8.6, <= 0.9.0)
7
+ faraday_middleware (~> 0.9.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.3.6)
13
+ coderay (1.1.0)
14
+ crack (0.4.2)
15
+ safe_yaml (~> 1.0.0)
16
+ debugger-ruby_core_source (1.3.2)
17
+ diff-lcs (1.2.5)
18
+ faker (1.3.0)
19
+ i18n (~> 0.5)
20
+ faraday (0.9.0)
21
+ multipart-post (>= 1.2, < 3)
22
+ faraday_middleware (0.9.1)
23
+ faraday (>= 0.7.4, < 0.10)
24
+ ffi (1.9.3)
25
+ guard (1.6.2)
26
+ listen (>= 0.6.0)
27
+ lumberjack (>= 1.0.2)
28
+ pry (>= 0.9.10)
29
+ terminal-table (>= 1.4.3)
30
+ thor (>= 0.14.6)
31
+ guard-rspec (2.4.1)
32
+ guard (>= 1.1)
33
+ rspec (~> 2.11)
34
+ i18n (0.6.9)
35
+ json (1.8.1)
36
+ listen (1.3.1)
37
+ rb-fsevent (>= 0.9.3)
38
+ rb-inotify (>= 0.9)
39
+ rb-kqueue (>= 0.2)
40
+ lumberjack (1.0.5)
41
+ method_source (0.8.2)
42
+ multipart-post (2.0.0)
43
+ net-http-persistent (2.9.4)
44
+ pry (0.9.12.6)
45
+ coderay (~> 1.0)
46
+ method_source (~> 0.8)
47
+ slop (~> 3.4)
48
+ rake (10.0.4)
49
+ rb-fsevent (0.9.4)
50
+ rb-inotify (0.9.4)
51
+ ffi (>= 0.5.0)
52
+ rb-kqueue (0.2.2)
53
+ ffi (>= 0.5.0)
54
+ rspec (2.14.1)
55
+ rspec-core (~> 2.14.0)
56
+ rspec-expectations (~> 2.14.0)
57
+ rspec-mocks (~> 2.14.0)
58
+ rspec-core (2.14.8)
59
+ rspec-expectations (2.14.5)
60
+ diff-lcs (>= 1.1.3, < 2.0)
61
+ rspec-mocks (2.14.6)
62
+ ruby-debug-base19x (0.11.30.pre15)
63
+ debugger-ruby_core_source (> 0)
64
+ rake (>= 0.8.1)
65
+ ruby-debug-ide (0.4.22)
66
+ rake (>= 0.8.1)
67
+ safe_yaml (1.0.3)
68
+ slop (3.5.0)
69
+ terminal-table (1.4.5)
70
+ thor (0.19.1)
71
+ vcr (2.4.0)
72
+ webmock (1.17.4)
73
+ addressable (>= 2.2.7)
74
+ crack (>= 0.3.2)
75
+ yard (0.8.7.4)
76
+
77
+ PLATFORMS
78
+ ruby
79
+
80
+ DEPENDENCIES
81
+ addressable
82
+ apruve!
83
+ bundler (~> 1.5)
84
+ faker
85
+ faraday
86
+ faraday_middleware
87
+ guard (~> 1.6.2)
88
+ guard-rspec (~> 2.4.1)
89
+ json
90
+ listen (~> 1.3.1)
91
+ net-http-persistent
92
+ rake (~> 10.0.3)
93
+ rspec (~> 2.14.1)
94
+ ruby-debug-base19x
95
+ ruby-debug-ide
96
+ vcr (~> 2.4.0)
97
+ webmock
98
+ yard
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Neal Tovsen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Apruve
2
+
3
+ Apruve helps B2B merchants by making it easier for customers to buy what they need for
4
+ their jobs. The apruve gem makes it easier for merchants on Ruby-based platforms to
5
+ integrate Apruve!
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'apruve'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install apruve
20
+
21
+ ## Usage
22
+
23
+ ### Create a store on Apruve. Use test.apruve.com for test accounts.
24
+
25
+ ### Create a PaymentRequest
26
+ @pr = Apruve::PaymentRequest.new
27
+ @pr.merchant_id =
28
+
29
+ ### On your web page, declare apruve.js
30
+
31
+
32
+ 3. Load the PaymentRequest and it's hash into apruve.js
33
+ 4. Decide where to put the Apruve button
34
+ 5. Register a call-back function to get the transaction ID back to your server
35
+ 6. Tell Apruve to create a payment against the transaction ID
36
+ 7. Process the webhook from Apruve when payment is received
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it ( http://github.com/apruve/apruve_gem/fork )
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec'
5
+
6
+ desc 'Run spec suite'
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ file_list = FileList['spec/**/*_spec.rb']
9
+ task.pattern = file_list
10
+ end
11
+
12
+ desc 'Default the task to run the spec'
13
+ task :default => [:spec]
data/apruve.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'apruve/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'apruve'
8
+ gem.version = Apruve::VERSION
9
+ gem.authors = ['Apruve, Inc.', 'Neal Tovsen']
10
+ gem.email = ['support@apruve.com']
11
+ gem.summary = 'Helper library for integrating Apruve into a ruby app.'
12
+ gem.description = 'Easily integrate the Apruve B2B payment network into your ruby-based application.'
13
+ gem.homepage = 'https://www.apruve.com'
14
+ gem.license = 'MIT'
15
+
16
+ gem.files = `git ls-files -z`.split("\x0")
17
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ['lib']
20
+
21
+ gem.add_development_dependency 'bundler', '~> 1.5'
22
+ gem.add_development_dependency 'rake'
23
+
24
+ gem.add_dependency('faraday', ['>= 0.8.6', '<= 0.9.0'])
25
+ gem.add_dependency('faraday_middleware', '~> 0.9.0')
26
+ gem.add_dependency('addressable', '~> 2.3.5')
27
+ end
data/lib/apruve.rb ADDED
@@ -0,0 +1,101 @@
1
+ # $:.unshift File.join(File.dirname(__FILE__), 'apruve', 'resources')
2
+ # $:.unshift File.join(File.dirname(__FILE__), 'apruve', 'response')
3
+
4
+ require_relative 'apruve/client'
5
+ require_relative 'apruve/version'
6
+ require_relative 'apruve/error'
7
+ require_relative 'apruve/faraday_error_handler'
8
+ require_relative 'apruve/utils'
9
+
10
+ module Apruve
11
+
12
+ @client = nil
13
+ @config = {
14
+ :scheme => 'http',
15
+ :host => 'localhost',
16
+ :port => 3000,
17
+ :version => 'v3',
18
+ }
19
+
20
+ class << self
21
+
22
+ attr_accessor :client
23
+ attr_accessor :config
24
+
25
+ PROD = 'prod'
26
+ TEST = 'test'
27
+ LOCAL = 'local'
28
+
29
+ def configure(api_key=nil, environment=LOCAL, options={})
30
+ configure_environment environment
31
+ @config = @config.merge(options)
32
+ @client = Apruve::Client.new(api_key, @config)
33
+ end
34
+
35
+ def js(display=nil)
36
+ display_param = display.nil? ? '' : "?display=#{display}"
37
+ "<script type=\"text/javascript\" src=\"#{js_url}#{display_param}\"></script>"
38
+ end
39
+
40
+ def button
41
+ '<div id="apruveDiv"></div>'
42
+ end
43
+
44
+ def default_currency
45
+ 'USD'
46
+ end
47
+
48
+ def get(*args, &block)
49
+ self.client.get *args
50
+ end
51
+
52
+ def post(*args, &block)
53
+ self.client.post *args
54
+ end
55
+
56
+ def put(*args, &block)
57
+ self.client.put *args
58
+ end
59
+
60
+ def unstore(*args, &block)
61
+ self.client.unstore *args
62
+ end
63
+
64
+ alias_method :delete, :unstore
65
+
66
+ # run configure on import so we have a default configuration
67
+ # that will run without an api-key
68
+
69
+ private
70
+
71
+ def configure_environment(env)
72
+ if env == PROD
73
+ @config[:scheme] = 'https'
74
+ @config[:host] = 'www.apruve.com'
75
+ @config[:port] = 443
76
+ elsif env == TEST
77
+ @config[:scheme] = 'https'
78
+ @config[:host] = 'test.apruve.com'
79
+ @config[:port] = 443
80
+ elsif env == LOCAL
81
+ @config[:scheme] = 'http'
82
+ @config[:host] = 'localhost'
83
+ @config[:port] = 3000
84
+ else
85
+ raise 'unknown environment'
86
+ end
87
+ end
88
+
89
+ def js_url
90
+ port_param = [443, 80].include?(@config[:port]) ? '' : ":#{@config[:port]}"
91
+ "#{@config[:scheme]}://#{@config[:host]}#{port_param}/js/apruve.js"
92
+ end
93
+ end
94
+
95
+ configure
96
+ end
97
+
98
+
99
+ # require all the resources! this is needed at the end because
100
+ # the module needs to be defined first, as it contains the registry
101
+ require_relative 'apruve/resources'
@@ -0,0 +1,102 @@
1
+ require 'logger'
2
+ require 'uri'
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require_relative 'response/apruve_exception_middleware'
6
+ require_relative 'response/apruve_parse_json'
7
+
8
+ module Apruve
9
+ class Client
10
+ DEFAULTS = {
11
+ :scheme => 'http',
12
+ :host => 'localhost',
13
+ :port => 3000,
14
+ :version => 'v3',
15
+ :logging_level => 'WARN',
16
+ :connection_timeout => 60,
17
+ :read_timeout => 60,
18
+ :logger => nil,
19
+ :ssl_verify => true,
20
+ :faraday_adapter => Faraday.default_adapter,
21
+ :accept_type => 'application/json'
22
+ }
23
+
24
+ # attr_reader :conn
25
+ attr_accessor :api_key, :config, :conn
26
+
27
+ def initialize(api_key, options={})
28
+ @api_key = api_key.nil? ? api_key : api_key.strip
29
+ @config = DEFAULTS.merge options
30
+ build_conn
31
+ end
32
+
33
+
34
+ def build_conn
35
+ if config[:logger]
36
+ logger = config[:logger]
37
+ else
38
+ logger = Logger.new(STDOUT)
39
+ logger.level = Logger.const_get(config[:logging_level].to_s)
40
+ config[:logger] = logger
41
+ end
42
+
43
+ Faraday::Response.register_middleware :handle_apruve_errors => lambda { Faraday::Response::RaiseApruveError }
44
+ Faraday::Response.register_middleware :apruve_json_parser => lambda { FaradayMiddleware::ApruveParseJson }
45
+
46
+ options = {
47
+ :request => {
48
+ :open_timeout => config[:connection_timeout],
49
+ :timeout => config[:read_timeout]
50
+ },
51
+ :ssl => {
52
+ :verify => @config[:ssl_verify] # Only set this to false for testing
53
+ }
54
+ }
55
+ @conn = Faraday.new(url, options) do |builder|
56
+ # Order is kinda important here...
57
+ builder.response :raise_error # raise exceptions on 40x, 50x responses
58
+ builder.use Apruve::FaradayErrorHandler
59
+ builder.request :json
60
+ builder.response :logger, logger
61
+ builder.response :handle_apruve_errors
62
+ builder.response :apruve_json_parser
63
+ builder.adapter config[:faraday_adapter]
64
+ end
65
+ conn.path_prefix = "/api/#{@config[:version]}"
66
+ conn.headers['User-Agent'] = "apruve-ruby/#{Apruve::VERSION}"
67
+ conn.headers['Content-Type'] = 'application/json'
68
+ conn.headers['Accept'] = "#{@config[:accept_type]};revision=#{@config[:version]}"
69
+ end
70
+
71
+ def url
72
+ builder = (config[:scheme] == 'http') ? URI::HTTP : URI::HTTPS
73
+
74
+ builder.build({:host => config[:host],
75
+ :port => config[:port],
76
+ :scheme => config[:scheme]})
77
+ end
78
+
79
+ def method_missing(method, *args, &block)
80
+ if is_http_method? method
81
+ conn.headers['Apruve-Api-Key'] = @api_key unless @api_key.nil?
82
+ conn.send method, *args
83
+ else
84
+ super method, *args, &block
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def is_http_method? method
91
+ [:get, :post, :put, :delete].include? method
92
+ end
93
+
94
+ def respond_to?(method, include_private = false)
95
+ if is_http_method? method
96
+ true
97
+ else
98
+ super method, include_private
99
+ end
100
+ end
101
+ end
102
+ end