зеркало из
https://github.com/glebtv/yookassa.git
synced 2026-09-07 19:35:51 +03:00
Merge pull request #9 from PaymentInstruments/Replace-initializers-with-dry-structs
Replace All initializers with dry-structs
Этот коммит содержится в:
27
Gemfile.lock
27
Gemfile.lock
@@ -2,7 +2,7 @@ PATH
|
|||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
yookassa (0.1.0)
|
yookassa (0.1.0)
|
||||||
dry-initializer
|
dry-struct
|
||||||
http (~> 5.0.1)
|
http (~> 5.0.1)
|
||||||
|
|
||||||
GEM
|
GEM
|
||||||
@@ -13,13 +13,35 @@ GEM
|
|||||||
ast (2.4.2)
|
ast (2.4.2)
|
||||||
byebug (11.1.3)
|
byebug (11.1.3)
|
||||||
coderay (1.1.3)
|
coderay (1.1.3)
|
||||||
|
concurrent-ruby (1.1.9)
|
||||||
crack (0.4.5)
|
crack (0.4.5)
|
||||||
rexml
|
rexml
|
||||||
diff-lcs (1.4.4)
|
diff-lcs (1.4.4)
|
||||||
docile (1.4.0)
|
docile (1.4.0)
|
||||||
domain_name (0.5.20190701)
|
domain_name (0.5.20190701)
|
||||||
unf (>= 0.0.5, < 1.0.0)
|
unf (>= 0.0.5, < 1.0.0)
|
||||||
dry-initializer (3.0.4)
|
dry-configurable (0.13.0)
|
||||||
|
concurrent-ruby (~> 1.0)
|
||||||
|
dry-core (~> 0.6)
|
||||||
|
dry-container (0.9.0)
|
||||||
|
concurrent-ruby (~> 1.0)
|
||||||
|
dry-configurable (~> 0.13, >= 0.13.0)
|
||||||
|
dry-core (0.7.1)
|
||||||
|
concurrent-ruby (~> 1.0)
|
||||||
|
dry-inflector (0.2.1)
|
||||||
|
dry-logic (1.2.0)
|
||||||
|
concurrent-ruby (~> 1.0)
|
||||||
|
dry-core (~> 0.5, >= 0.5)
|
||||||
|
dry-struct (1.4.0)
|
||||||
|
dry-core (~> 0.5, >= 0.5)
|
||||||
|
dry-types (~> 1.5)
|
||||||
|
ice_nine (~> 0.11)
|
||||||
|
dry-types (1.5.1)
|
||||||
|
concurrent-ruby (~> 1.0)
|
||||||
|
dry-container (~> 0.3)
|
||||||
|
dry-core (~> 0.5, >= 0.5)
|
||||||
|
dry-inflector (~> 0.1, >= 0.1.2)
|
||||||
|
dry-logic (~> 1.0, >= 1.0.2)
|
||||||
ffi (1.15.4)
|
ffi (1.15.4)
|
||||||
ffi-compiler (1.0.1)
|
ffi-compiler (1.0.1)
|
||||||
ffi (>= 1.0.0)
|
ffi (>= 1.0.0)
|
||||||
@@ -33,6 +55,7 @@ GEM
|
|||||||
http-cookie (1.0.4)
|
http-cookie (1.0.4)
|
||||||
domain_name (~> 0.5)
|
domain_name (~> 0.5)
|
||||||
http-form_data (2.3.0)
|
http-form_data (2.3.0)
|
||||||
|
ice_nine (0.11.2)
|
||||||
llhttp-ffi (0.4.0)
|
llhttp-ffi (0.4.0)
|
||||||
ffi-compiler (~> 1.0)
|
ffi-compiler (~> 1.0)
|
||||||
rake (~> 13.0)
|
rake (~> 13.0)
|
||||||
|
|||||||
@@ -1,16 +1,13 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "http"
|
require "http"
|
||||||
require "dry-initializer"
|
require "dry-struct"
|
||||||
require "yookassa/version"
|
require "yookassa/version"
|
||||||
require "yookassa/payment"
|
require "yookassa/payment"
|
||||||
require "yookassa/refund"
|
require "yookassa/refund"
|
||||||
require "yookassa/response"
|
|
||||||
require "yookassa/callable"
|
|
||||||
require "yookassa/optional"
|
|
||||||
require "yookassa/entity/payment"
|
require "yookassa/entity/payment"
|
||||||
require "yookassa/entity/refund"
|
require "yookassa/entity/refund"
|
||||||
require "yookassa/error"
|
require "yookassa/entity/error"
|
||||||
require "yookassa/config"
|
require "yookassa/config"
|
||||||
require "yookassa/http_helpers"
|
require "yookassa/http_helpers"
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Yookassa
|
|
||||||
module Callable
|
|
||||||
def call(*args)
|
|
||||||
new(*args)
|
|
||||||
end
|
|
||||||
alias [] call
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,14 +1,12 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./types"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
module Entity
|
module Entity
|
||||||
class Amount
|
class Amount < Dry::Struct
|
||||||
extend Dry::Initializer
|
attribute :value, Types::Coercible::Float
|
||||||
extend Yookassa::Callable
|
attribute :currency, Types::String
|
||||||
include Yookassa::Optional
|
|
||||||
|
|
||||||
option :value, proc(&:to_f), optional: true
|
|
||||||
option :currency, proc(&:to_s), optional: true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,18 +1,16 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./types"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
module Entity
|
module Entity
|
||||||
class Card
|
class Card < Dry::Struct
|
||||||
extend Dry::Initializer
|
attribute :first6, Types::Integer
|
||||||
extend Yookassa::Callable
|
attribute :last4, Types::Integer
|
||||||
include Yookassa::Optional
|
attribute :expiry_month, Types::Integer
|
||||||
|
attribute :expiry_year, Types::Integer
|
||||||
option :first6
|
attribute :card_type, Types::String
|
||||||
option :last4
|
attribute :source, Types::String
|
||||||
option :expiry_month
|
|
||||||
option :expiry_year
|
|
||||||
option :card_type, proc(&:to_s)
|
|
||||||
option :source, proc(&:to_s), optional: true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,16 +1,14 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./types"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
module Entity
|
module Entity
|
||||||
class Confirmation
|
class Confirmation < Dry::Struct
|
||||||
extend Dry::Initializer
|
attribute :type, Types::String
|
||||||
extend Yookassa::Callable
|
attribute :confirmation_url, Types::String
|
||||||
include Yookassa::Optional
|
attribute? :enforce, Types::Bool
|
||||||
|
attribute? :return_url, Types::String
|
||||||
option :type, proc(&:to_s), optional: true
|
|
||||||
option :confirmation_url, proc(&:to_s), optional: true
|
|
||||||
option :enforce, optional: true
|
|
||||||
option :return_url, proc(&:to_s), optional: true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
17
lib/yookassa/entity/error.rb
Обычный файл
17
lib/yookassa/entity/error.rb
Обычный файл
@@ -0,0 +1,17 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Yookassa
|
||||||
|
module Entity
|
||||||
|
class Error < Dry::Struct
|
||||||
|
attribute :type, Types::String
|
||||||
|
attribute? :id, Types::String
|
||||||
|
attribute? :code, Types::String
|
||||||
|
attribute? :description, Types::String
|
||||||
|
attribute? :parameter, Types::String
|
||||||
|
|
||||||
|
def error?
|
||||||
|
type == "error"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,22 +1,26 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./types"
|
||||||
require_relative "./amount"
|
require_relative "./amount"
|
||||||
require_relative "./payment_method"
|
require_relative "./payment_method"
|
||||||
require_relative "./confirmation"
|
require_relative "./confirmation"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
module Entity
|
module Entity
|
||||||
class Payment < Yookassa::Response
|
class Payment < Dry::Struct
|
||||||
option :paid
|
attribute :id, Types::String
|
||||||
option :amount, Entity::Amount
|
attribute :status, Types::String.enum("pending", "waiting_for_capture", "succeeded", "canceled")
|
||||||
option :created_at
|
attribute :paid, Types::Bool
|
||||||
option :captured_at, proc(&:to_s), optional: true
|
attribute :amount, Entity::Amount
|
||||||
option :expires_at, optional: true
|
attribute? :income_amount, Entity::Amount
|
||||||
option :description, proc(&:to_s), optional: true
|
attribute :created_at, Types::String
|
||||||
option :metadata, optional: true
|
attribute? :captured_at, Types::String
|
||||||
option :payment_method, Entity::PaymentMethod, optional: true
|
attribute? :expires_at, Types::String
|
||||||
option :confirmation, Entity::Confirmation, optional: true
|
attribute? :description, Types::String
|
||||||
option :test
|
attribute :metadata, Types::Hash
|
||||||
|
attribute :payment_method, Entity::PaymentMethod
|
||||||
|
attribute :confirmation, Entity::Confirmation
|
||||||
|
attribute :test, Types::Bool
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,19 +1,16 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./types"
|
||||||
require_relative "./card"
|
require_relative "./card"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
module Entity
|
module Entity
|
||||||
class PaymentMethod
|
class PaymentMethod < Dry::Struct
|
||||||
extend Dry::Initializer
|
attribute :type, Types::String
|
||||||
extend Yookassa::Callable
|
attribute :id, Types::String
|
||||||
include Yookassa::Optional
|
attribute :saved, Types::Bool
|
||||||
|
attribute? :card, Entity::Card
|
||||||
option :type, proc(&:to_s)
|
attribute? :title, Types::String
|
||||||
option :id, proc(&:to_s)
|
|
||||||
option :saved
|
|
||||||
option :card, Entity::Card, optional: true
|
|
||||||
option :title, proc(&:to_s), optional: true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./types"
|
||||||
require_relative "./amount"
|
require_relative "./amount"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
module Entity
|
module Entity
|
||||||
class Refund < Yookassa::Response
|
class Refund < Dry::Struct
|
||||||
option :payment_id
|
attribute :id, Types::String
|
||||||
option :created_at, proc(&:to_s)
|
attribute :status, Types::String
|
||||||
option :amount, Entity::Amount
|
attribute :payment_id, Types::String
|
||||||
option :receipt_registration, proc(&:to_s), optional: true
|
attribute :created_at, Types::String
|
||||||
option :description, proc(&:to_s), optional: true
|
attribute :amount, Entity::Amount
|
||||||
|
attribute? :receipt_registration, Types::String
|
||||||
|
attribute :description, Types::String
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
9
lib/yookassa/entity/types.rb
Обычный файл
9
lib/yookassa/entity/types.rb
Обычный файл
@@ -0,0 +1,9 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Yookassa
|
||||||
|
module Entity
|
||||||
|
module Types
|
||||||
|
include Dry.Types()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Yookassa
|
|
||||||
class Error
|
|
||||||
extend Dry::Initializer
|
|
||||||
extend Yookassa::Callable
|
|
||||||
include Yookassa::Optional
|
|
||||||
|
|
||||||
option :type, proc(&:to_s)
|
|
||||||
option :id, proc(&:to_s), optional: true
|
|
||||||
option :code, proc(&:to_s), optional: true
|
|
||||||
option :description, proc(&:to_s), optional: true
|
|
||||||
option :parameter, proc(&:to_s), optional: true
|
|
||||||
|
|
||||||
def error?
|
|
||||||
type == "error"
|
|
||||||
end
|
|
||||||
|
|
||||||
class << self
|
|
||||||
def build(*res)
|
|
||||||
body = res.last
|
|
||||||
new JSON.parse(body.first)
|
|
||||||
end
|
|
||||||
|
|
||||||
def new(opts)
|
|
||||||
super opts.transform_keys(&:to_sym)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -8,7 +8,7 @@ module Yookassa
|
|||||||
response = client.get("#{API_URL}#{endpoint}", params: query)
|
response = client.get("#{API_URL}#{endpoint}", params: query)
|
||||||
body = JSON.parse(response.body.to_s, symbolize_names: true)
|
body = JSON.parse(response.body.to_s, symbolize_names: true)
|
||||||
|
|
||||||
return Error.new(body) if response.status.client_error?
|
return Entity::Error.new(**body) if response.status.client_error?
|
||||||
|
|
||||||
yield(body) if block_given?
|
yield(body) if block_given?
|
||||||
end
|
end
|
||||||
@@ -17,7 +17,7 @@ module Yookassa
|
|||||||
response = client.headers("Idempotence-Key" => idempotency_key).post("#{API_URL}#{endpoint}", json: payload)
|
response = client.headers("Idempotence-Key" => idempotency_key).post("#{API_URL}#{endpoint}", json: payload)
|
||||||
body = JSON.parse(response.body.to_s, symbolize_names: true)
|
body = JSON.parse(response.body.to_s, symbolize_names: true)
|
||||||
|
|
||||||
return Error.new(body) if response.status.client_error?
|
return Entity::Error.new(**body) if response.status.client_error?
|
||||||
|
|
||||||
yield(body) if block_given?
|
yield(body) if block_given?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,23 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Yookassa
|
|
||||||
module Optional
|
|
||||||
private
|
|
||||||
|
|
||||||
def initialize(opts)
|
|
||||||
super opts.transform_keys(&:to_sym)
|
|
||||||
end
|
|
||||||
|
|
||||||
def __options__
|
|
||||||
@__options__ ||= self.class.dry_initializer.attributes(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def respond_to_missing?(name, *)
|
|
||||||
__options__.respond_to? name
|
|
||||||
end
|
|
||||||
|
|
||||||
def method_missing(*args, &block)
|
|
||||||
respond_to_missing?(*args) ? __options__.send(*args, &block) : super
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Yookassa
|
|
||||||
class Response
|
|
||||||
extend Dry::Initializer
|
|
||||||
option :id, proc(&:to_s)
|
|
||||||
option :status, proc(&:to_s), default: proc {}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@@ -10,7 +10,7 @@ RSpec.describe Yookassa::Payment do
|
|||||||
|
|
||||||
shared_examples "returns_payment_object" do
|
shared_examples "returns_payment_object" do
|
||||||
it "returns success" do
|
it "returns success" do
|
||||||
expect(subject).to be_kind_of Yookassa::Response
|
expect(subject).to be_a Yookassa::Entity::Payment
|
||||||
expect(subject.id).to eq "2490ded1-000f-5000-8000-1f64111bc63e"
|
expect(subject.id).to eq "2490ded1-000f-5000-8000-1f64111bc63e"
|
||||||
expect(subject.test).to eq true
|
expect(subject.test).to eq true
|
||||||
expect(subject.paid).to eq false
|
expect(subject.paid).to eq false
|
||||||
@@ -21,17 +21,17 @@ RSpec.describe Yookassa::Payment do
|
|||||||
expect(subject.expires_at).to eq nil
|
expect(subject.expires_at).to eq nil
|
||||||
expect(subject.metadata).to eq({})
|
expect(subject.metadata).to eq({})
|
||||||
|
|
||||||
expect(subject.amount).to be_kind_of Yookassa::Entity::Amount
|
expect(subject.amount).to be_a Yookassa::Entity::Amount
|
||||||
expect(subject.amount.currency).to eq "RUB"
|
expect(subject.amount.currency).to eq "RUB"
|
||||||
expect(subject.amount.value).to eq 10.0
|
expect(subject.amount.value).to eq 10.0
|
||||||
|
|
||||||
expect(subject.confirmation).to be_kind_of Yookassa::Entity::Confirmation
|
expect(subject.confirmation).to be_a Yookassa::Entity::Confirmation
|
||||||
expect(subject.confirmation.confirmation_url).to eq "https://money.yookassa.ru/payments/external/confirmation?orderId=2490ded1-000f-5000-8000-1f64111bc63e"
|
expect(subject.confirmation.confirmation_url).to eq "https://money.yookassa.ru/payments/external/confirmation?orderId=2490ded1-000f-5000-8000-1f64111bc63e"
|
||||||
expect(subject.confirmation.type).to eq "redirect"
|
expect(subject.confirmation.type).to eq "redirect"
|
||||||
expect(subject.confirmation.return_url).to eq "https://url.test"
|
expect(subject.confirmation.return_url).to eq "https://url.test"
|
||||||
expect(subject.confirmation.enforce).to eq nil
|
expect(subject.confirmation.enforce).to eq nil
|
||||||
|
|
||||||
expect(subject.payment_method).to be_kind_of Yookassa::Entity::PaymentMethod
|
expect(subject.payment_method).to be_a Yookassa::Entity::PaymentMethod
|
||||||
expect(subject.payment_method.card).to eq nil
|
expect(subject.payment_method.card).to eq nil
|
||||||
expect(subject.payment_method.id).to eq "2490ded1-000f-5000-8000-1f64111bc63e"
|
expect(subject.payment_method.id).to eq "2490ded1-000f-5000-8000-1f64111bc63e"
|
||||||
expect(subject.payment_method.saved).to eq false
|
expect(subject.payment_method.saved).to eq false
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ RSpec.describe Yookassa::Refund do
|
|||||||
|
|
||||||
shared_examples "returns_refund_object" do
|
shared_examples "returns_refund_object" do
|
||||||
it "returns success" do
|
it "returns success" do
|
||||||
expect(subject).to be_kind_of Yookassa::Response
|
expect(subject).to be_a Yookassa::Entity::Refund
|
||||||
expect(subject.id).to eq "2491ab0c-0015-5000-9000-1640c7f1a6f0"
|
expect(subject.id).to eq "2491ab0c-0015-5000-9000-1640c7f1a6f0"
|
||||||
expect(subject.payment_id).to eq "2491a6e2-000f-5000-9000-1480e820ae17"
|
expect(subject.payment_id).to eq "2491a6e2-000f-5000-9000-1480e820ae17"
|
||||||
expect(subject.status).to eq "succeeded"
|
expect(subject.status).to eq "succeeded"
|
||||||
|
|||||||
@@ -22,6 +22,6 @@ Gem::Specification.new do |spec|
|
|||||||
|
|
||||||
spec.required_ruby_version = ">= 2.6"
|
spec.required_ruby_version = ">= 2.6"
|
||||||
|
|
||||||
spec.add_runtime_dependency "dry-initializer"
|
spec.add_runtime_dependency "dry-struct"
|
||||||
spec.add_runtime_dependency "http", "~> 5.0.1"
|
spec.add_runtime_dependency "http", "~> 5.0.1"
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user