Sha256: e1ea67ac0a5176b9ea444b8e29936a4c6e2bcdb53dd76d30f826bff4e42870e2

Contents?: true

Size: 967 Bytes

Versions: 3

Compression:

Stored size: 967 Bytes

Contents

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

    def self.api_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 api_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.api_url}/#{CGI.escape(id.to_s)}"
    end

    def refresh
      response, api_key = Paid.request(:get, api_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

3 entries across 3 versions & 1 rubygems

Version Path
paid-0.1.0 lib/paid/api_resource.rb
paid-0.0.8 lib/paid/api_resource.rb
paid-0.0.7 lib/paid/api_resource.rb