Class: Mavenlink::APIResource

Inherits:
Object
  • Object
show all
Includes:
Mavenlink::APIOperations::Request
Defined in:
lib/mavenlink/api_resource.rb

Direct Known Subclasses

CustomFieldValue, Invoice, Workspace, WorkspaceGroup

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mavenlink::APIOperations::Request

included

Constructor Details

#initialize(data) ⇒ APIResource

Returns a new instance of APIResource.



37
38
39
40
41
# File 'lib/mavenlink/api_resource.rb', line 37

def initialize(data)
  @values = data

  update_attributes(@values)
end

Class Method Details

.list(params = {}, options = {}) ⇒ Object



26
27
28
29
# File 'lib/mavenlink/api_resource.rb', line 26

def self.list(params = {}, options = {})
  response = get(resource_url, params)
  List.new(self, response, options)
end

.plural_nameObject



17
18
19
# File 'lib/mavenlink/api_resource.rb', line 17

def self.plural_name
  "#{self::OBJECT_NAME.downcase}s"
end

.resource_urlObject



7
8
9
10
11
12
13
14
15
# File 'lib/mavenlink/api_resource.rb', line 7

def self.resource_url
  if self == APIResource
    raise NotImplementedError,
          "APIResource is an abstract class, you should before actions "\
          "on its subclasses (Workspace, Invoice, etc.)"
  end

  "/#{plural_name}"
end

.retrieve(id) ⇒ Object



21
22
23
24
# File 'lib/mavenlink/api_resource.rb', line 21

def self.retrieve(id)
  instance = new(id: id)
  instance.refresh
end

Instance Method Details

#refreshObject



31
32
33
34
35
# File 'lib/mavenlink/api_resource.rb', line 31

def refresh
  response = get(resource_url)
  data = Util.results(response).first
  self.class.new(data)
end

#resource_urlObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/mavenlink/api_resource.rb', line 43

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