From 3002ea7865ead5c2f99b6ac6e1590fa4089a7f5f Mon Sep 17 00:00:00 2001 From: Daniel Docki Date: Tue, 24 Mar 2015 19:02:53 -0300 Subject: [PATCH] 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 --- README.md | 44 ++++++++++++++- lib/config.rb | 55 +++++++++++++++++++ lib/generators/pagseguro/install_generator.rb | 22 ++++++++ lib/generators/templates/pagseguro.rb | 6 ++ lib/generators/templates/pagseguro.yml | 14 +++++ lib/pagseguro.rb | 36 +++++------- lib/pagseguro/request.rb | 6 +- spec/pagseguro/notification_spec.rb | 8 +-- spec/pagseguro/pagseguro_spec.rb | 18 +++--- spec/pagseguro/payment/serializer_spec.rb | 4 +- spec/pagseguro/query_spec.rb | 8 +-- spec/pagseguro/refund_spec.rb | 8 +-- spec/pagseguro/session_spec.rb | 4 +- 13 files changed, 181 insertions(+), 52 deletions(-) create mode 100644 lib/config.rb create mode 100644 lib/generators/pagseguro/install_generator.rb create mode 100644 lib/generators/templates/pagseguro.rb create mode 100644 lib/generators/templates/pagseguro.yml diff --git a/README.md b/README.md index 01e35da..22267d5 100644 --- a/README.md +++ b/README.md @@ -25,14 +25,52 @@ gem "pagseguro-transparente", "~> 0.2.3" ~~~ * Ainda não publicada, é preciso baixar direto do Github. -Criar um initializer em config/initializer/pagseguro.rb +###**PagSeguro** suporta dois métodos de configurações. + +####Rails Initializer +~~~.ruby +rails g pagseguro:install +~~~ + +O generator vai criar o Rails initializer em config/initializers/pagseguro.rb. ~~~.ruby PagSeguro.configure do |config| - config.email = "exemplo@pagseguro.com.br" - config.token = "token válido" + config.environment = :sandbox + config.adapter_javascript_url = "https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js" + config.email = "exemplo@pagseguro.com.br" + config.token = "token válido" end ~~~ +####Ou por YAML +~~~.ruby +rails g pagseguro:install --use-yml +~~~ + +O generator vai criar o YAML em config/pagseguro.yml. +~~~.yaml +development: &development + adapter_javascript_url: "https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js" + email: "exemplo@pagseguro.com.br" + token: "token válido" + environment: "sandbox" + +test: + <<: *development + +production: + adapter_javascript_url: "https://stc.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js" + email: "exemplo@pagseguro.com.br" + token: "token válido" + environment: "production" +~~~ + + +####Adicione ao seu application layout +~~~ +<%= javascript_include_tag PagSeguro.adapter_javascript_url %> +~~~ + ##Criando uma nova sessão ~~~.ruby pagseguro_session = PagSeguro::Session.new diff --git a/lib/config.rb b/lib/config.rb new file mode 100644 index 0000000..902cdb7 --- /dev/null +++ b/lib/config.rb @@ -0,0 +1,55 @@ +module PagSeguro + class Config + # Primary e-mail associated with the primary account. + attr_accessor :email + + # The API token associated with primary account. + attr_accessor :token + + # Sencondary e-mail associated with secondary account. + attr_accessor :alt_email + + # The API token associated with secondary account. + attr_accessor :alt_token + + # The PagSeguro environment. + # Only +production+ for now. + attr_accessor :environment + + # Timeout value in seconds for requests. + attr_accessor :timeout + + # Javascript adapter + attr_accessor :adapter_javascript_url + + def initialize + load_yml! if defined?(Rails) && yml_exists? + end + + def yml_exists? + defined?(Rails.root) ? File.exist?(self.yml_path) : false + end + + def yml + begin + @yml ||= HashWithIndifferentAccess.new(YAML.load_file(yml_path)[Rails.env]) rescue nil || {} + rescue Psych::SyntaxError + @yml = {} + end + end + + def yml_path + Rails.root.join("config/pagseguro.yml") + end + + def load_yml! + self.email = yml[:email] + self.token = yml[:token] + self.alt_email = yml[:alt_email] + self.alt_token = yml[:alt_token] + self.timeout = yml[:timeout] + self.adapter_javascript_url = yml[:adapter_javascript_url] + self.environment = yml[:environment].to_sym + end + end +end diff --git a/lib/generators/pagseguro/install_generator.rb b/lib/generators/pagseguro/install_generator.rb new file mode 100644 index 0000000..1520f36 --- /dev/null +++ b/lib/generators/pagseguro/install_generator.rb @@ -0,0 +1,22 @@ +module PagSeguro + module Generators + class InstallGenerator < Rails::Generators::Base + namespace "pagseguro:install" + source_root File.expand_path("../../templates", __FILE__) + + class_option :use_yml, type: :boolean, default: false, desc: "Use YML file instead of Rails Initializer" + + def generate_config + if options[:use_yml] + template "pagseguro.yml", "config/pagseguro.yml" + end + end + + def generate_initializer + unless options[:use_yml] + template "pagseguro.rb", "config/initializers/pagseguro.rb" + end + end + end + end +end diff --git a/lib/generators/templates/pagseguro.rb b/lib/generators/templates/pagseguro.rb new file mode 100644 index 0000000..21fef7c --- /dev/null +++ b/lib/generators/templates/pagseguro.rb @@ -0,0 +1,6 @@ +PagSeguro.configure do |config| + config.environment = :sandbox + config.adapter_javascript_url = "https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js" + config.email = "exemplo@pagseguro.com.br" + config.token = "token válido" +end diff --git a/lib/generators/templates/pagseguro.yml b/lib/generators/templates/pagseguro.yml new file mode 100644 index 0000000..d1a061a --- /dev/null +++ b/lib/generators/templates/pagseguro.yml @@ -0,0 +1,14 @@ +development: &development + adapter_javascript_url: "https://stc.sandbox.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js" + email: "exemplo@pagseguro.com.br" + token: "token válido" + environment: "sandbox" + +test: + <<: *development + +production: + adapter_javascript_url: "https://stc.pagseguro.uol.com.br/pagseguro/api/v2/checkout/pagseguro.directpayment.js" + email: "exemplo@pagseguro.com.br" + token: "token válido" + environment: "production" diff --git a/lib/pagseguro.rb b/lib/pagseguro.rb index de4ac98..b24ebbb 100644 --- a/lib/pagseguro.rb +++ b/lib/pagseguro.rb @@ -1,4 +1,5 @@ require "active_model" +require "config" require "pagseguro/version" require "pagseguro/request" require "pagseguro/session" @@ -34,27 +35,24 @@ module PagSeguro class << self - # Primary e-mail associated with the primary account. - attr_accessor :email - - # The API token associated with primary account. - attr_accessor :token - - # Sencondary e-mail associated with secondary account. - attr_accessor :alt_email + def config=(data) + @config = data + end - # The API token associated with secondary account. - attr_accessor :alt_token + def config + @config ||= Config.new + end - # The PagSeguro environment. - # Only +production+ for now. - attr_accessor :environment + def configure(&block) + yield config + end - # Timeout value in seconds for requests. - attr_accessor :timeout + def adapter_javascript_url + config.adapter_javascript_url + end def api_url(version) - uris.fetch(environment) + version + uris.fetch(config.environment) + version end private @@ -66,7 +64,7 @@ def uris end end - self.environment = :production + self.config.environment = :production # Set the global configuration. # @@ -75,8 +73,4 @@ def uris # config.token = "abc" # config.environment = :sandbox # end - # - def self.configure(&block) - yield self - end end diff --git a/lib/pagseguro/request.rb b/lib/pagseguro/request.rb index c2bc0e1..5b8881c 100644 --- a/lib/pagseguro/request.rb +++ b/lib/pagseguro/request.rb @@ -16,16 +16,16 @@ def post(path, version, account = "default", params = {}) self.class.base_uri PagSeguro.api_url(version) options = { body: add_credencials(account) } options[:body].merge!(params) - options[:timeout] = PagSeguro.timeout unless PagSeguro.timeout.blank? + options[:timeout] = PagSeguro.config.timeout unless PagSeguro.config.timeout.blank? self.class.post(path, options) end private def add_credencials(account) if account == "alternative" - { email: PagSeguro.alt_email, token: PagSeguro.alt_token } + { email: PagSeguro.config.alt_email, token: PagSeguro.config.alt_token } else - { email: PagSeguro.email, token: PagSeguro.token } + { email: PagSeguro.config.email, token: PagSeguro.config.token } end end end diff --git a/spec/pagseguro/notification_spec.rb b/spec/pagseguro/notification_spec.rb index 76cf26f..d673620 100644 --- a/spec/pagseguro/notification_spec.rb +++ b/spec/pagseguro/notification_spec.rb @@ -10,10 +10,10 @@ it { should respond_to(:transaction) } before do - PagSeguro.email = 'mail' - PagSeguro.token = 'token' - PagSeguro.alt_email = 'alt_mail' - PagSeguro.alt_token = 'alt_token' + PagSeguro.config.email = 'mail' + PagSeguro.config.token = 'token' + PagSeguro.config.alt_email = 'alt_mail' + PagSeguro.config.alt_token = 'alt_token' end describe "#transaction" do diff --git a/spec/pagseguro/pagseguro_spec.rb b/spec/pagseguro/pagseguro_spec.rb index 8688d01..720d392 100644 --- a/spec/pagseguro/pagseguro_spec.rb +++ b/spec/pagseguro/pagseguro_spec.rb @@ -2,14 +2,14 @@ describe PagSeguro do before do - PagSeguro.email = "EMAIL" - PagSeguro.token = "TOKEN" - PagSeguro.timeout = 10 + PagSeguro.config.email = "EMAIL" + PagSeguro.config.token = "TOKEN" + PagSeguro.config.timeout = 10 end - it { expect(PagSeguro.email).to eql("EMAIL") } - it { expect(PagSeguro.token).to eql("TOKEN") } - it { expect(PagSeguro.timeout).to eql(10) } + it { expect(PagSeguro.config.email).to eql("EMAIL") } + it { expect(PagSeguro.config.token).to eql("TOKEN") } + it { expect(PagSeguro.config.timeout).to eql(10) } context "configuring library" do it "yields PagSeguro" do @@ -20,7 +20,7 @@ end context "default settings" do - it { expect(PagSeguro.environment).to eql(:production) } + it { expect(PagSeguro.config.environment).to eql(:production) } end describe ".api_url" do @@ -30,7 +30,7 @@ end context "sandbox" do - before { PagSeguro.environment = :sandbox } + before { PagSeguro.config.environment = :sandbox } it { expect(PagSeguro.api_url(PagSeguro::Request::API_V2)).to eq('https://ws.sandbox.pagseguro.uol.com.br/v2') } end end @@ -41,7 +41,7 @@ end context "sandbox" do - before { PagSeguro.environment = :sandbox } + before { PagSeguro.config.environment = :sandbox } it { expect(PagSeguro.api_url(PagSeguro::Request::API_V3)).to eq('https://ws.sandbox.pagseguro.uol.com.br/v3') } end end diff --git a/spec/pagseguro/payment/serializer_spec.rb b/spec/pagseguro/payment/serializer_spec.rb index f0087f1..d658d52 100644 --- a/spec/pagseguro/payment/serializer_spec.rb +++ b/spec/pagseguro/payment/serializer_spec.rb @@ -75,8 +75,8 @@ end before do - PagSeguro.email = 'pagseguro@eventick.com.br' - PagSeguro.token = 'my_token' + PagSeguro.config.email = 'pagseguro@eventick.com.br' + PagSeguro.config.token = 'my_token' end let(:bank) { PagSeguro::Bank.new('bancodobrasil') } diff --git a/spec/pagseguro/query_spec.rb b/spec/pagseguro/query_spec.rb index 4c0e356..dbbbfe5 100644 --- a/spec/pagseguro/query_spec.rb +++ b/spec/pagseguro/query_spec.rb @@ -9,10 +9,10 @@ it { should respond_to(:transaction) } before do - PagSeguro.email = 'mail' - PagSeguro.token = 'token' - PagSeguro.alt_email = 'alt_mail' - PagSeguro.alt_token = 'alt_token' + PagSeguro.config.email = 'mail' + PagSeguro.config.token = 'token' + PagSeguro.config.alt_email = 'alt_mail' + PagSeguro.config.alt_token = 'alt_token' end describe "#transaction" do diff --git a/spec/pagseguro/refund_spec.rb b/spec/pagseguro/refund_spec.rb index 0cdb80d..3a894d8 100644 --- a/spec/pagseguro/refund_spec.rb +++ b/spec/pagseguro/refund_spec.rb @@ -9,10 +9,10 @@ it { should respond_to(:errors) } before do - PagSeguro.email = 'mail' - PagSeguro.token = 'token' - PagSeguro.alt_email = 'alt_mail' - PagSeguro.alt_token = 'alt_token' + PagSeguro.config.email = 'mail' + PagSeguro.config.token = 'token' + PagSeguro.config.alt_email = 'alt_mail' + PagSeguro.config.alt_token = 'alt_token' end describe "#request" do diff --git a/spec/pagseguro/session_spec.rb b/spec/pagseguro/session_spec.rb index aed40ea..a73e715 100644 --- a/spec/pagseguro/session_spec.rb +++ b/spec/pagseguro/session_spec.rb @@ -5,8 +5,8 @@ subject { session } before do - PagSeguro.email = 'mail' - PagSeguro.token = 'token' + PagSeguro.config.email = 'mail' + PagSeguro.config.token = 'token' end it { should respond_to(:create) }