Sha256: 837fa63f26f541926398e6bfcdf24edf6e98bc7daac0ebc6587d7ec41c3ec878

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

require 'dry/struct'
require 'addressable/template'

module Payable
  class Resource < Dry::Struct
    constructor_type :schema

    attribute :id, Types::Strict::Int
    attribute :created_at, Types::Form::DateTime
    attribute :updated_at, Types::Form::DateTime

    def self.api_url
      Addressable::URI.parse Payable.config.api_url
    end

    def self.url_template
      Addressable::Template.new api_url.join("#{collection}/{id}")
    end

    def self.retrieve(id, client: Payable.client, params: {})
      response = client.get(url_template.expand(id: id), params)

      new(response.body)
    end

    def self.collection
      raise NotImplementedError if self == Payable::Resource

      @collection ||= begin
        collection = name.split('::').last
        collection = collection.split(/(?=[A-Z])/).map(&:downcase)

        "#{ collection.join('_') }s"
      end
    end

    def self.list(options = {})
      ResourceList.new(self, options)
    end

    def url
      self.class.url_template.expand(id: id)
    end
  end
end

require 'payable/writeable'
require 'payable/worker'
require 'payable/work_type'
require 'payable/work'
require 'payable/payment'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
payable-api-0.1.2 lib/payable/resource.rb
payable-api-0.1.1 lib/payable/resource.rb
payable-api-0.1.0 lib/payable/resource.rb