Class: Frame::APIResource Abstract

Inherits:
FrameObject show all
Includes:
Frame::APIOperations::Request
Defined in:
lib/frame/api_resource.rb

Overview

This class is abstract.

Subclass and implement object_name to create a new resource type.

Base class for all Frame Payments API resources.

All API resources inherit from APIResource, which provides common functionality like retrieving resources by ID and building resource URLs.

Instance Attribute Summary

Attributes inherited from FrameObject

#id, #original_values

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Frame::APIOperations::Request

included

Methods inherited from FrameObject

#[], #[]=, construct_from, #each, #initialize, #initialize_from, #inspect, #keys, #serialize_params, #to_hash, #to_s, #update_attributes, #values

Constructor Details

This class inherits a constructor from Frame::FrameObject

Class Method Details

.class_nameObject



13
14
15
# File 'lib/frame/api_resource.rb', line 13

def self.class_name
  name.split("::")[-1]
end

.resource_urlString

Builds the base URL for this resource type

Returns:

  • (String)

    the resource URL (e.g., “/v1/customers”)



19
20
21
22
23
24
25
26
27
# File 'lib/frame/api_resource.rb', line 19

def self.resource_url
  if self == APIResource
    raise NotImplementedError,
      "APIResource is an abstract class. You should perform actions " \
      "on its subclasses (Customer, etc.)"
  end

  "/v1/#{object_name.downcase}s"
end

.retrieve(id, opts = {}) ⇒ APIResource

Retrieves a resource by its ID

Examples:

customer = Frame::Customer.retrieve('cus_123456789')

Parameters:

  • id (String)

    the resource ID

  • opts (Hash) (defaults to: {})

    additional options

Returns:



35
36
37
38
39
40
# File 'lib/frame/api_resource.rb', line 35

def self.retrieve(id, opts = {})
  id = Util.normalize_id(id)
  instance = new(id, opts)
  instance.refresh(opts)
  instance
end

Instance Method Details

#refresh(opts = {}) ⇒ APIResource

Refreshes the resource data from the API

Examples:

customer.refresh

Parameters:

  • opts (Hash) (defaults to: {})

    additional options

Returns:



61
62
63
64
65
# File 'lib/frame/api_resource.rb', line 61

def refresh(opts = {})
  response = request(:get, resource_url, {}, opts)
  initialize_from(response, opts)
  self
end

#resource_urlString

Builds the URL for this specific resource instance

Returns:

  • (String)

    the resource instance URL

Raises:



45
46
47
48
49
50
51
52
53
54
# File 'lib/frame/api_resource.rb', line 45

def resource_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.resource_url}/#{CGI.escape(id)}"
end