Class: Anvil::Resources::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/anvil/resources/base.rb

Direct Known Subclasses

PDF, Signature, SignatureSigner, Webform, Webhook, Workflow

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, client: nil) ⇒ Base

Returns a new instance of Base.



8
9
10
11
# File 'lib/anvil/resources/base.rb', line 8

def initialize(attributes = {}, client: nil)
  @attributes = symbolize_keys(attributes)
  @client = client || default_client
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

ActiveRecord-like attribute accessors



14
15
16
17
18
19
20
21
22
23
# File 'lib/anvil/resources/base.rb', line 14

def method_missing(method_name, *args)
  if method_name.to_s.end_with?('=')
    attribute = method_name.to_s.chomp('=').to_sym
    attributes[attribute] = args.first
  elsif attributes.key?(method_name)
    attributes[method_name]
  else
    super
  end
end

Class Attribute Details

.clientObject



61
62
63
# File 'lib/anvil/resources/base.rb', line 61

def client
  @client ||= Client.new
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/anvil/resources/base.rb', line 6

def attributes
  @attributes
end

#clientObject (readonly)

Returns the value of attribute client.



6
7
8
# File 'lib/anvil/resources/base.rb', line 6

def client
  @client
end

Class Method Details

.build_from_response(response) ⇒ Object

Helper for building resource instances from API responses



77
78
79
80
81
82
83
# File 'lib/anvil/resources/base.rb', line 77

def build_from_response(response)
  if response.data.is_a?(Array)
    response.data.map { |item| new(item) }
  else
    new(response.data)
  end
end

.create(attributes = {}, client: nil) ⇒ Object

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/anvil/resources/base.rb', line 90

def create(attributes = {}, client: nil)
  raise NotImplementedError, "#{self.class.name}#create must be implemented by subclass"
end

.find(id, client: nil) ⇒ Object

Common API operations

Raises:

  • (NotImplementedError)


86
87
88
# File 'lib/anvil/resources/base.rb', line 86

def find(id, client: nil)
  raise NotImplementedError, "#{self.class.name}#find must be implemented by subclass"
end

.list(params = {}, client: nil) ⇒ Object

Raises:

  • (NotImplementedError)


94
95
96
# File 'lib/anvil/resources/base.rb', line 94

def list(params = {}, client: nil)
  raise NotImplementedError, "#{self.class.name}#list must be implemented by subclass"
end

.with_client(api_key: nil) ⇒ Object

Override in subclasses to provide resource-specific client



68
69
70
71
72
73
74
# File 'lib/anvil/resources/base.rb', line 68

def with_client(api_key: nil)
  original_client = @client
  @client = Client.new(api_key: api_key)
  yield
ensure
  @client = original_client
end

Instance Method Details

#==(other) ⇒ Object



41
42
43
# File 'lib/anvil/resources/base.rb', line 41

def ==(other)
  other.is_a?(self.class) && attributes == other.attributes
end

#inspectObject



37
38
39
# File 'lib/anvil/resources/base.rb', line 37

def inspect
  "#<#{self.class.name} #{attributes.inspect}>"
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/anvil/resources/base.rb', line 25

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('=') || attributes.key?(method_name) || super
end

#to_hObject



29
30
31
# File 'lib/anvil/resources/base.rb', line 29

def to_h
  attributes
end

#to_json(*args) ⇒ Object



33
34
35
# File 'lib/anvil/resources/base.rb', line 33

def to_json(*args)
  attributes.to_json(*args)
end