Sha256: e8ffdba4fe808430f1e8791a24352228a3a915398eb688b6146088412062ed72

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

require "omise/config"
require "omise/attributes"
require "omise/resource"

module Omise
  class OmiseObject
    include Attributes

    class << self
      attr_accessor :endpoint

      def location(id = nil)
        [endpoint, id].compact.join("/")
      end

      def resource(path, attributes = {})
        key = attributes.delete(:key) { resource_key }
        preprocess_attributes!(attributes)
        Omise.resource.new(resource_url, path, key)
      end

      private

      def collection
        self
      end

      def singleton!
        require "omise/singleton_resource"
        include SingletonResource
      end

      def preprocess_attributes!(attributes)
        if attributes[:card].is_a?(Hash)
          require "omise/token"
          card_attributes = attributes.delete(:card)
          attributes[:card] = Token.create(card: card_attributes).id
        end
      end

      def resource_url
        Omise.api_url
      end

      def resource_key
        Omise.api_key
      end
    end

    private

    def collection
      self.class
    end

    def resource(*args)
      collection.resource(location, *args)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
omise-0.1.5 lib/omise/object.rb
omise-0.1.4 lib/omise/object.rb
omise-0.1.1 lib/omise/object.rb
omise-0.1.0 lib/omise/object.rb