Class: DevinApi::Resources::Base

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

Overview

Base class for all API resources

Direct Known Subclasses

Attachment, Enterprise, Knowledge, Secret, Session

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Base.



9
10
11
12
# File 'lib/devin_api/resources/base.rb', line 9

def initialize(client, attributes = {})
  @client = client
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *_args) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/devin_api/resources/base.rb', line 34

def method_missing(method, *_args)
  if @attributes.key?(method.to_s)
    @attributes[method.to_s]
  elsif @attributes.key?(method.to_sym)
    @attributes[method.to_sym]
  end
end

Instance Attribute Details

#attributes ⇒ Object (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/devin_api/resources/base.rb', line 7

def attributes
  @attributes
end

#client ⇒ Object (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/devin_api/resources/base.rb', line 7

def client
  @client
end

Class Method Details

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



55
56
57
58
# File 'lib/devin_api/resources/base.rb', line 55

def create(client, attributes = {})
  response = client.post(resource_path, attributes)
  new(client, response)
end

.pagination_supported? ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/devin_api/resources/base.rb', line 60

def pagination_supported?
  false
end

.resource_name ⇒ Object



47
48
49
# File 'lib/devin_api/resources/base.rb', line 47

def resource_name
  @resource_name ||= name.split('::').last.downcase
end

.resource_path ⇒ Object



51
52
53
# File 'lib/devin_api/resources/base.rb', line 51

def resource_path
  "/v1/#{resource_name.gsub('_', '-')}s"
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
# File 'lib/devin_api/resources/base.rb', line 14

def [](key)
  @attributes[key.to_s] || @attributes[key.to_sym]
end

#[]=(key, value) ⇒ Object



18
19
20
# File 'lib/devin_api/resources/base.rb', line 18

def []=(key, value)
  @attributes[key.to_sym] = value
end

#id ⇒ Object



22
23
24
# File 'lib/devin_api/resources/base.rb', line 22

def id
  self[:id]
end

#path ⇒ Object



26
27
28
# File 'lib/devin_api/resources/base.rb', line 26

def path
  "#{self.class.resource_path}/#{id}"
end

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

Returns:

  • (Boolean)


42
43
44
# File 'lib/devin_api/resources/base.rb', line 42

def respond_to_missing?(method, include_private = false)
  @attributes.key?(method.to_s) || @attributes.key?(method.to_sym) || super
end

#to_hash ⇒ Object



30
31
32
# File 'lib/devin_api/resources/base.rb', line 30

def to_hash
  @attributes.dup
end