tray-checkout 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,26 @@
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
+ misc
19
+ TODO
20
+
21
+ # For MacOS:
22
+ .DS_Store
23
+
24
+ # For vim:
25
+ *.swp
26
+ *.swo
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,2 @@
1
+ == Version 0.0.0
2
+ - First version, unstable yet.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "http://rubygems.org"
2
+ gemspec
data/README.rdoc ADDED
@@ -0,0 +1,65 @@
1
+ = tray-checkout
2
+
3
+ Tray Checkout API
4
+
5
+ == Installing
6
+
7
+ === Gemfile
8
+ gem 'tray-checkout'
9
+
10
+ === Direct installation
11
+ $ gem install tray-checkout
12
+
13
+
14
+ == Using
15
+
16
+ require 'tray-checkout'
17
+
18
+ t = Tray::Checkout::Transaction.new
19
+
20
+
21
+ == Author
22
+ - {Fernando Hamasaki (prodis)}[http://prodis.blog.br]
23
+
24
+
25
+ == Contributing to tray-checkout
26
+
27
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
28
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
29
+ * Fork the project.
30
+ * Start a feature/bugfix branch.
31
+ * Commit and push until you are happy with your contribution.
32
+ * Don't forget to rebase with branch master in main project before submit the pull request.
33
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
34
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
35
+
36
+
37
+ == Copyright
38
+
39
+ (The MIT License)
40
+
41
+ {Prodis a.k.a. Fernando Hamasaki}[http://prodis.blog.br]
42
+
43
+ http://prodis.net.br/images/prodis_150.gif
44
+
45
+ Copyright (c) 2012 Prodis
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining
48
+ a copy of this software and associated documentation files (the
49
+ "Software"), to deal in the Software without restriction, including
50
+ without limitation the rights to use, copy, modify, merge, publish,
51
+ distribute, sublicense, and/or sell copies of the Software, and to
52
+ permit persons to whom the Software is furnished to do so, subject to
53
+ the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be
56
+ included in all copies or substantial portions of the Software.
57
+
58
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
59
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
60
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
61
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
62
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
63
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
64
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
65
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+ require 'active_support/core_ext'
3
+
4
+ module Tray
5
+ module Checkout
6
+ class Parser
7
+ def response(xml)
8
+ puts xml
9
+ hash = convert_to_hash(xml)
10
+ success?(hash) ? data(hash) : errors(hash)
11
+ end
12
+
13
+ private
14
+
15
+ def convert_to_hash(xml)
16
+ symbolize_all_keys!(Hash.from_xml(xml))[:transaction]
17
+ end
18
+
19
+ def success?(hash)
20
+ hash[:message_response][:message] == "success"
21
+ end
22
+
23
+ def data(hash)
24
+ hash[:data_response]
25
+ end
26
+
27
+ def errors(hash)
28
+ hash[:error_response]
29
+ end
30
+
31
+ def symbolize_all_keys!(hash)
32
+ #TODO: Move to Hash class.
33
+ case
34
+ when hash.is_a?(Array)
35
+ puts "### Array #{hash}"
36
+ puts
37
+ hash.each { |value| symbolize_all_keys!(value) }
38
+ when hash.is_a?(Hash)
39
+ puts "$$$ Hash: #{hash}"
40
+ puts
41
+ hash.symbolize_keys!
42
+
43
+ hash.each_value do |value|
44
+ value.symbolize_keys! if value.is_a?(Hash)
45
+ symbolize_all_keys!(value)
46
+ end
47
+ end
48
+
49
+ hash
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ module Tray
3
+ module Checkout
4
+ class Transaction
5
+ URL = "http://api.sandbox.checkout.tray.com.br/api/transactions"
6
+
7
+ def get(token)
8
+ response = web_service.request!("#{URL}/get_by_token", { token: token })
9
+ result = parser.response(response)
10
+ result
11
+ end
12
+
13
+ private
14
+
15
+ def web_service
16
+ @web_service ||= Tray::Checkout::WebService.new
17
+ end
18
+
19
+ def parser
20
+ @parser ||= Tray::Checkout::Parser.new
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: UTF-8
2
+ module Tray
3
+ module Checkout
4
+ VERSION = "0.0.0"
5
+ end
6
+ end
7
+
@@ -0,0 +1,32 @@
1
+ # encoding: UTF-8
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module Tray
6
+ module Checkout
7
+ class WebService
8
+ def request!(url, params)
9
+ uri = URI.parse(url)
10
+ http = build_http(uri)
11
+ request = build_request(uri, params)
12
+
13
+ response = http.request(request)
14
+ response.body
15
+ end
16
+
17
+ private
18
+
19
+ def build_http(uri)
20
+ http = Net::HTTP.new(uri.host, uri.port)
21
+ http.open_timeout = Tray::Checkout.request_timeout
22
+ http
23
+ end
24
+
25
+ def build_request(uri, params)
26
+ request = Net::HTTP::Post.new(uri.path)
27
+ request.set_form_data(params)
28
+ request
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+ require 'log-me'
3
+
4
+ module Tray
5
+ module Checkout
6
+ extend LogMe
7
+
8
+ module Timeout
9
+ DEFAULT_REQUEST_TIMEOUT = 5 #seconds
10
+ attr_writer :request_timeout
11
+
12
+ def request_timeout
13
+ (@request_timeout ||= DEFAULT_REQUEST_TIMEOUT).to_i
14
+ end
15
+ end
16
+
17
+ extend Timeout
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ require 'tray/checkout'
4
+ require 'tray/checkout/parser'
5
+ require 'tray/checkout/transaction'
6
+ require 'tray/checkout/web_service'
@@ -0,0 +1,13 @@
1
+ require 'tray-checkout'
2
+ require 'webmock/rspec'
3
+
4
+ # Requires supporting files with custom matchers and macros, etc,
5
+ # in ./support/ and its subdirectories.
6
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
7
+
8
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
+ RSpec.configure do |config|
10
+ config.order = "random"
11
+ end
12
+
13
+ WebMock.disable_net_connect!
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Tray::Checkout do
5
+ describe "#request_timeout" do
6
+ it "default is 5" do
7
+ Tray::Checkout.request_timeout.should eql 5
8
+ end
9
+
10
+ context "when set timeout" do
11
+ it "returns timeout" do
12
+ Tray::Checkout.configure { |config| config.request_timeout = 3 }
13
+ Tray::Checkout.request_timeout.should eql 3
14
+ end
15
+
16
+ it "returns timeout in seconds (integer)" do
17
+ Tray::Checkout.configure { |config| config.request_timeout = 2.123 }
18
+ Tray::Checkout.request_timeout.should eql 2
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'tray/checkout/version'
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "tray-checkout"
7
+ gem.version = Tray::Checkout::VERSION
8
+ gem.authors = ["Prodis a.k.a. Fernando Hamasaki"]
9
+ gem.email = ["prodis@gmail.com"]
10
+ gem.summary = "Tray Checkout API"
11
+ gem.description = "Tray Checkout API"
12
+ gem.homepage = "https://github.com/prodis/tray-checkout"
13
+ gem.licenses = ["MIT"]
14
+
15
+ gem.files = `git ls-files`.split($\)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.platform = Gem::Platform::RUBY
21
+ gem.required_ruby_version = Gem::Requirement.new(">= 1.9.2")
22
+
23
+ gem.add_dependency "activesupport", "~> 3.1"
24
+ gem.add_dependency "log-me", "~> 0.0.4"
25
+
26
+ gem.add_development_dependency "rake"
27
+ gem.add_development_dependency "rspec", "~> 2.12"
28
+ gem.add_development_dependency "webmock", "~> 1.9"
29
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tray-checkout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Prodis a.k.a. Fernando Hamasaki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &14773020 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *14773020
25
+ - !ruby/object:Gem::Dependency
26
+ name: log-me
27
+ requirement: &14772790 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.0.4
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *14772790
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &14772600 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *14772600
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &14772330 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.12'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *14772330
58
+ - !ruby/object:Gem::Dependency
59
+ name: webmock
60
+ requirement: &14772080 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '1.9'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *14772080
69
+ description: Tray Checkout API
70
+ email:
71
+ - prodis@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - CHANGELOG.rdoc
79
+ - Gemfile
80
+ - README.rdoc
81
+ - Rakefile
82
+ - lib/tray-checkout.rb
83
+ - lib/tray/checkout.rb
84
+ - lib/tray/checkout/parser.rb
85
+ - lib/tray/checkout/transaction.rb
86
+ - lib/tray/checkout/version.rb
87
+ - lib/tray/checkout/web_service.rb
88
+ - spec/spec_helper.rb
89
+ - spec/tray/checkout_spec.rb
90
+ - tray-checkout.gemspec
91
+ homepage: https://github.com/prodis/tray-checkout
92
+ licenses:
93
+ - MIT
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: 1.9.2
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ segments:
111
+ - 0
112
+ hash: 255298765
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.16
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Tray Checkout API
119
+ test_files:
120
+ - spec/spec_helper.rb
121
+ - spec/tray/checkout_spec.rb