Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 3002ea7

Browse files
committed
added yaml template generator
added option to use yml or initializer added javascript adapter to initializer template added environment to pagseguro yaml template added config to working with yaml normalize test to working with new configs normalize request to working with configs added adapter_javascript_url to direct access added new comments better markdown refactored config and configure methods Normalize all the line endings
1 parent 9aef10b commit 3002ea7

File tree

13 files changed

+181
-52
lines changed

13 files changed

+181
-52
lines changed

README.md

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,52 @@ gem "pagseguro-transparente", "~> 0.2.3"
2525
~~~
2626
* Ainda não publicada, é preciso baixar direto do Github.
2727

28-
Criar um initializer em config/initializer/pagseguro.rb
28+
###**PagSeguro** suporta dois métodos de configurações.
29+
30+
####Rails Initializer
31+
~~~.ruby
32+
rails g pagseguro:install
33+
~~~
34+
35+
O generator vai criar o Rails initializer em config/initializers/pagseguro.rb.
2936
~~~.ruby
3037
PagSeguro.configure do |config|
31-
config.email = "exemplo@pagseguro.com.br"
32-
config.token = "token válido"
38+
config.environment = :sandbox
39+
config.adapter_javascript_url = "https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js"
40+
config.email = "exemplo@pagseguro.com.br"
41+
config.token = "token válido"
3342
end
3443
~~~
3544

45+
####Ou por YAML
46+
~~~.ruby
47+
rails g pagseguro:install --use-yml
48+
~~~
49+
50+
O generator vai criar o YAML em config/pagseguro.yml.
51+
~~~.yaml
52+
development: &development
53+
adapter_javascript_url: "https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js"
54+
email: "exemplo@pagseguro.com.br"
55+
token: "token válido"
56+
environment: "sandbox"
57+
58+
test:
59+
<<: *development
60+
61+
production:
62+
adapter_javascript_url: "https://stc.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js"
63+
email: "exemplo@pagseguro.com.br"
64+
token: "token válido"
65+
environment: "production"
66+
~~~
67+
68+
69+
####Adicione ao seu application layout
70+
~~~
71+
<%= javascript_include_tag PagSeguro.adapter_javascript_url %>
72+
~~~
73+
3674
##Criando uma nova sessão
3775
~~~.ruby
3876
pagseguro_session = PagSeguro::Session.new

lib/config.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module PagSeguro
2+
class Config
3+
# Primary e-mail associated with the primary account.
4+
attr_accessor :email
5+
6+
# The API token associated with primary account.
7+
attr_accessor :token
8+
9+
# Sencondary e-mail associated with secondary account.
10+
attr_accessor :alt_email
11+
12+
# The API token associated with secondary account.
13+
attr_accessor :alt_token
14+
15+
# The PagSeguro environment.
16+
# Only +production+ for now.
17+
attr_accessor :environment
18+
19+
# Timeout value in seconds for requests.
20+
attr_accessor :timeout
21+
22+
# Javascript adapter
23+
attr_accessor :adapter_javascript_url
24+
25+
def initialize
26+
load_yml! if defined?(Rails) && yml_exists?
27+
end
28+
29+
def yml_exists?
30+
defined?(Rails.root) ? File.exist?(self.yml_path) : false
31+
end
32+
33+
def yml
34+
begin
35+
@yml ||= HashWithIndifferentAccess.new(YAML.load_file(yml_path)[Rails.env]) rescue nil || {}
36+
rescue Psych::SyntaxError
37+
@yml = {}
38+
end
39+
end
40+
41+
def yml_path
42+
Rails.root.join("config/pagseguro.yml")
43+
end
44+
45+
def load_yml!
46+
self.email = yml[:email]
47+
self.token = yml[:token]
48+
self.alt_email = yml[:alt_email]
49+
self.alt_token = yml[:alt_token]
50+
self.timeout = yml[:timeout]
51+
self.adapter_javascript_url = yml[:adapter_javascript_url]
52+
self.environment = yml[:environment].to_sym
53+
end
54+
end
55+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module PagSeguro
2+
module Generators
3+
class InstallGenerator < Rails::Generators::Base
4+
namespace "pagseguro:install"
5+
source_root File.expand_path("../../templates", __FILE__)
6+
7+
class_option :use_yml, type: :boolean, default: false, desc: "Use YML file instead of Rails Initializer"
8+
9+
def generate_config
10+
if options[:use_yml]
11+
template "pagseguro.yml", "config/pagseguro.yml"
12+
end
13+
end
14+
15+
def generate_initializer
16+
unless options[:use_yml]
17+
template "pagseguro.rb", "config/initializers/pagseguro.rb"
18+
end
19+
end
20+
end
21+
end
22+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PagSeguro.configure do |config|
2+
config.environment = :sandbox
3+
config.adapter_javascript_url = "https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js"
4+
config.email = "exemplo@pagseguro.com.br"
5+
config.token = "token válido"
6+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
development: &development
2+
adapter_javascript_url: "https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js"
3+
email: "exemplo@pagseguro.com.br"
4+
token: "token válido"
5+
environment: "sandbox"
6+
7+
test:
8+
<<: *development
9+
10+
production:
11+
adapter_javascript_url: "https://stc.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js"
12+
email: "exemplo@pagseguro.com.br"
13+
token: "token válido"
14+
environment: "production"

lib/pagseguro.rb

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "active_model"
2+
require "config"
23
require "pagseguro/version"
34
require "pagseguro/request"
45
require "pagseguro/session"
@@ -34,27 +35,24 @@
3435

3536
module PagSeguro
3637
class << self
37-
# Primary e-mail associated with the primary account.
38-
attr_accessor :email
39-
40-
# The API token associated with primary account.
41-
attr_accessor :token
42-
43-
# Sencondary e-mail associated with secondary account.
44-
attr_accessor :alt_email
38+
def config=(data)
39+
@config = data
40+
end
4541

46-
# The API token associated with secondary account.
47-
attr_accessor :alt_token
42+
def config
43+
@config ||= Config.new
44+
end
4845

49-
# The PagSeguro environment.
50-
# Only +production+ for now.
51-
attr_accessor :environment
46+
def configure(&block)
47+
yield config
48+
end
5249

53-
# Timeout value in seconds for requests.
54-
attr_accessor :timeout
50+
def adapter_javascript_url
51+
config.adapter_javascript_url
52+
end
5553

5654
def api_url(version)
57-
uris.fetch(environment) + version
55+
uris.fetch(config.environment) + version
5856
end
5957

6058
private
@@ -66,7 +64,7 @@ def uris
6664
end
6765
end
6866

69-
self.environment = :production
67+
self.config.environment = :production
7068

7169
# Set the global configuration.
7270
#
@@ -75,8 +73,4 @@ def uris
7573
# config.token = "abc"
7674
# config.environment = :sandbox
7775
# end
78-
#
79-
def self.configure(&block)
80-
yield self
81-
end
8276
end

lib/pagseguro/request.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ def post(path, version, account = "default", params = {})
1616
self.class.base_uri PagSeguro.api_url(version)
1717
options = { body: add_credencials(account) }
1818
options[:body].merge!(params)
19-
options[:timeout] = PagSeguro.timeout unless PagSeguro.timeout.blank?
19+
options[:timeout] = PagSeguro.config.timeout unless PagSeguro.config.timeout.blank?
2020
self.class.post(path, options)
2121
end
2222

2323
private
2424
def add_credencials(account)
2525
if account == "alternative"
26-
{ email: PagSeguro.alt_email, token: PagSeguro.alt_token }
26+
{ email: PagSeguro.config.alt_email, token: PagSeguro.config.alt_token }
2727
else
28-
{ email: PagSeguro.email, token: PagSeguro.token }
28+
{ email: PagSeguro.config.email, token: PagSeguro.config.token }
2929
end
3030
end
3131
end

spec/pagseguro/notification_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
it { should respond_to(:transaction) }
1111

1212
before do
13-
PagSeguro.email = 'mail'
14-
PagSeguro.token = 'token'
15-
PagSeguro.alt_email = 'alt_mail'
16-
PagSeguro.alt_token = 'alt_token'
13+
PagSeguro.config.email = 'mail'
14+
PagSeguro.config.token = 'token'
15+
PagSeguro.config.alt_email = 'alt_mail'
16+
PagSeguro.config.alt_token = 'alt_token'
1717
end
1818

1919
describe "#transaction" do

spec/pagseguro/pagseguro_spec.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
describe PagSeguro do
44
before do
5-
PagSeguro.email = "EMAIL"
6-
PagSeguro.token = "TOKEN"
7-
PagSeguro.timeout = 10
5+
PagSeguro.config.email = "EMAIL"
6+
PagSeguro.config.token = "TOKEN"
7+
PagSeguro.config.timeout = 10
88
end
99

10-
it { expect(PagSeguro.email).to eql("EMAIL") }
11-
it { expect(PagSeguro.token).to eql("TOKEN") }
12-
it { expect(PagSeguro.timeout).to eql(10) }
10+
it { expect(PagSeguro.config.email).to eql("EMAIL") }
11+
it { expect(PagSeguro.config.token).to eql("TOKEN") }
12+
it { expect(PagSeguro.config.timeout).to eql(10) }
1313

1414
context "configuring library" do
1515
it "yields PagSeguro" do
@@ -20,7 +20,7 @@
2020
end
2121

2222
context "default settings" do
23-
it { expect(PagSeguro.environment).to eql(:production) }
23+
it { expect(PagSeguro.config.environment).to eql(:production) }
2424
end
2525

2626
describe ".api_url" do
@@ -30,7 +30,7 @@
3030
end
3131

3232
context "sandbox" do
33-
before { PagSeguro.environment = :sandbox }
33+
before { PagSeguro.config.environment = :sandbox }
3434
it { expect(PagSeguro.api_url(PagSeguro::Request::API_V2)).to eq('https://ws.sandbox.pagseguro.uol.com.br/v2') }
3535
end
3636
end
@@ -41,7 +41,7 @@
4141
end
4242

4343
context "sandbox" do
44-
before { PagSeguro.environment = :sandbox }
44+
before { PagSeguro.config.environment = :sandbox }
4545
it { expect(PagSeguro.api_url(PagSeguro::Request::API_V3)).to eq('https://ws.sandbox.pagseguro.uol.com.br/v3') }
4646
end
4747
end

spec/pagseguro/payment/serializer_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@
7575
end
7676

7777
before do
78-
PagSeguro.email = 'pagseguro@eventick.com.br'
79-
PagSeguro.token = 'my_token'
78+
PagSeguro.config.email = 'pagseguro@eventick.com.br'
79+
PagSeguro.config.token = 'my_token'
8080
end
8181

8282
let(:bank) { PagSeguro::Bank.new('bancodobrasil') }

0 commit comments

Comments
 (0)