зеркало из
https://github.com/glebtv/yookassa.git
synced 2026-09-04 02:05:51 +03:00
Replace All initializers with dry-structs, remove callable and optional modules and hacks around instanciating, ruby 3 support works now
Этот коммит содержится в:
@@ -1,16 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "http"
|
||||
require "dry-initializer"
|
||||
require "dry-struct"
|
||||
require "yookassa/version"
|
||||
require "yookassa/payment"
|
||||
require "yookassa/refund"
|
||||
require "yookassa/response"
|
||||
require "yookassa/callable"
|
||||
require "yookassa/optional"
|
||||
require "yookassa/entity/payment"
|
||||
require "yookassa/entity/refund"
|
||||
require "yookassa/error"
|
||||
require "yookassa/entity/error"
|
||||
require "yookassa/config"
|
||||
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
|
||||
|
||||
require_relative "./types"
|
||||
|
||||
module Yookassa
|
||||
module Entity
|
||||
class Amount
|
||||
extend Dry::Initializer
|
||||
extend Yookassa::Callable
|
||||
include Yookassa::Optional
|
||||
|
||||
option :value, proc(&:to_f), optional: true
|
||||
option :currency, proc(&:to_s), optional: true
|
||||
class Amount < Dry::Struct
|
||||
attribute :value, Types::Coercible::Float
|
||||
attribute :currency, Types::String
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,18 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./types"
|
||||
|
||||
module Yookassa
|
||||
module Entity
|
||||
class Card
|
||||
extend Dry::Initializer
|
||||
extend Yookassa::Callable
|
||||
include Yookassa::Optional
|
||||
|
||||
option :first6
|
||||
option :last4
|
||||
option :expiry_month
|
||||
option :expiry_year
|
||||
option :card_type, proc(&:to_s)
|
||||
option :source, proc(&:to_s), optional: true
|
||||
class Card < Dry::Struct
|
||||
attribute :first6, Types::Integer
|
||||
attribute :last4, Types::Integer
|
||||
attribute :expiry_month, Types::Integer
|
||||
attribute :expiry_year, Types::Integer
|
||||
attribute :card_type, Types::String
|
||||
attribute :source, Types::String
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./types"
|
||||
|
||||
module Yookassa
|
||||
module Entity
|
||||
class Confirmation
|
||||
extend Dry::Initializer
|
||||
extend Yookassa::Callable
|
||||
include Yookassa::Optional
|
||||
|
||||
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
|
||||
class Confirmation < Dry::Struct
|
||||
attribute :type, Types::String
|
||||
attribute :confirmation_url, Types::String
|
||||
attribute? :enforce, Types::Bool
|
||||
attribute? :return_url, Types::String
|
||||
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
|
||||
|
||||
require_relative "./types"
|
||||
require_relative "./amount"
|
||||
require_relative "./payment_method"
|
||||
require_relative "./confirmation"
|
||||
|
||||
module Yookassa
|
||||
module Entity
|
||||
class Payment < Yookassa::Response
|
||||
option :paid
|
||||
option :amount, Entity::Amount
|
||||
option :created_at
|
||||
option :captured_at, proc(&:to_s), optional: true
|
||||
option :expires_at, optional: true
|
||||
option :description, proc(&:to_s), optional: true
|
||||
option :metadata, optional: true
|
||||
option :payment_method, Entity::PaymentMethod, optional: true
|
||||
option :confirmation, Entity::Confirmation, optional: true
|
||||
option :test
|
||||
class Payment < Dry::Struct
|
||||
attribute :id, Types::String
|
||||
attribute :status, Types::String.enum("pending", "waiting_for_capture", "succeeded", "canceled")
|
||||
attribute :paid, Types::Bool
|
||||
attribute :amount, Entity::Amount
|
||||
attribute? :income_amount, Entity::Amount
|
||||
attribute :created_at, Types::String
|
||||
attribute? :captured_at, Types::String
|
||||
attribute? :expires_at, Types::String
|
||||
attribute? :description, Types::String
|
||||
attribute :metadata, Types::Hash
|
||||
attribute :payment_method, Entity::PaymentMethod
|
||||
attribute :confirmation, Entity::Confirmation
|
||||
attribute :test, Types::Bool
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,19 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./types"
|
||||
require_relative "./card"
|
||||
|
||||
module Yookassa
|
||||
module Entity
|
||||
class PaymentMethod
|
||||
extend Dry::Initializer
|
||||
extend Yookassa::Callable
|
||||
include Yookassa::Optional
|
||||
|
||||
option :type, proc(&:to_s)
|
||||
option :id, proc(&:to_s)
|
||||
option :saved
|
||||
option :card, Entity::Card, optional: true
|
||||
option :title, proc(&:to_s), optional: true
|
||||
class PaymentMethod < Dry::Struct
|
||||
attribute :type, Types::String
|
||||
attribute :id, Types::String
|
||||
attribute :saved, Types::Bool
|
||||
attribute? :card, Entity::Card
|
||||
attribute? :title, Types::String
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,15 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./types"
|
||||
require_relative "./amount"
|
||||
|
||||
module Yookassa
|
||||
module Entity
|
||||
class Refund < Yookassa::Response
|
||||
option :payment_id
|
||||
option :created_at, proc(&:to_s)
|
||||
option :amount, Entity::Amount
|
||||
option :receipt_registration, proc(&:to_s), optional: true
|
||||
option :description, proc(&:to_s), optional: true
|
||||
class Refund < Dry::Struct
|
||||
attribute :id, Types::String
|
||||
attribute :status, Types::String
|
||||
attribute :payment_id, Types::String
|
||||
attribute :created_at, Types::String
|
||||
attribute :amount, Entity::Amount
|
||||
attribute? :receipt_registration, Types::String
|
||||
attribute :description, Types::String
|
||||
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)
|
||||
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?
|
||||
end
|
||||
@@ -17,7 +17,7 @@ module Yookassa
|
||||
response = client.headers("Idempotence-Key" => idempotency_key).post("#{API_URL}#{endpoint}", json: payload)
|
||||
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?
|
||||
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
|
||||
Ссылка в новой задаче
Block a user