Sha256: 188e9b0b6a2b81d1cdb69921c2bdee3334f233204a0d5aa53a38ea0719c09120

Contents?: true

Size: 946 Bytes

Versions: 2

Compression:

Stored size: 946 Bytes

Contents

module Paid
  class APIResource < PaidObject
    def self.class_name
      self.name.split('::')[-1]
    end

    def self.url
      if self == APIResource
        raise NotImplementedError.new('APIResource is an abstract class.  You should perform actions on its subclasses (Transaction, Customer, etc.)')
      end
      require 'active_support/inflector'
      "/v0/#{CGI.escape(class_name.downcase).pluralize}"
    end

    def url
      unless id = self['id']
        raise InvalidRequestError.new("Could not determine which URL to request: #{self.class} instance has invalid ID: #{id.inspect}", 'id')
      end
      "#{self.class.url}/#{CGI.escape(id)}"
    end

    def refresh
      response, api_key = Paid.request(:get, url, @api_key, @retrieve_options)
      refresh_from(response, api_key)
    end

    def self.retrieve(id, api_key=nil)
      instance = self.new(id, api_key)
      instance.refresh
      instance
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paid-0.0.3 lib/paid/api_resource.rb
paid-0.0.2 lib/paid/api_resource.rb