Class: Springcm::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/springcm-sdk/resource.rb

Overview

A Resource is a SpringCM object that has an auto-assigned GUID.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Object

#initialize, #method_missing, #raw

Constructor Details

This class inherits a constructor from Springcm::Object

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Springcm::Object

Class Method Details

.resource_paramsObject

Some resources have query parameters that must be passed when retrieving it, e.g. expand=attributegroups when retrieving a document.



94
95
96
# File 'lib/springcm-sdk/resource.rb', line 94

def self.resource_params
  {}
end

Instance Method Details

#deleteObject

Send a DELETE request for this resource.



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/springcm-sdk/resource.rb', line 73

def delete
  conn = @client.authorized_connection(url: @client.object_api_url)
  res = conn.delete do |req|
    req.url resource_uri
  end
  if res.success?
    data = JSON.parse(res.body)
    reload
  else
    nil
  end
end

#getObject

Send a GET request for this resource.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/springcm-sdk/resource.rb', line 24

def get
  conn = @client.authorized_connection(url: @client.object_api_url)
  res = conn.get do |req|
    req.url resource_uri
    resource_params.each { |key, value|
      req.params[key] = value
    }
  end
  if res.success?
    data = JSON.parse(res.body)
    self.class.new(data, @client)
  else
    nil
  end
end

#patchObject

Send a PATCH request for this resource.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/springcm-sdk/resource.rb', line 41

def patch
  conn = @client.authorized_connection(url: @client.object_api_url)
  res = conn.patch do |req|
    req.headers['Content-Type'] = "application/json"
    req.url resource_uri
    req.body = raw.to_json
  end
  if res.success?
    data = JSON.parse(res.body)
    self.class.new(data, @client)
  else
    nil
  end
end

#putObject

Send a PUT request for this resource.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/springcm-sdk/resource.rb', line 57

def put
  conn = @client.authorized_connection(url: @client.object_api_url)
  res = conn.put do |req|
    req.headers['Content-Type'] = "application/json"
    req.url resource_uri
    req.body = raw.to_json
  end
  if res.success?
    data = JSON.parse(res.body)
    self.class.new(data, @client)
  else
    nil
  end
end

#reloadObject

Resend a request to the API for this resource and return a new instance.



12
13
14
# File 'lib/springcm-sdk/resource.rb', line 12

def reload
  get
end

#reload!Object

Resend a request to the API for this resource and modify the data for the current object in-place.



18
19
20
21
# File 'lib/springcm-sdk/resource.rb', line 18

def reload!
  @data = reload.raw
  self
end

#resource_nameObject

Pluralized resource name, e.g. documents or folders. Used to construct request URLs.



104
105
106
# File 'lib/springcm-sdk/resource.rb', line 104

def resource_name
  "#{self.class.to_s.split("::").last.downcase}s"
end

#resource_paramsObject



98
99
100
# File 'lib/springcm-sdk/resource.rb', line 98

def resource_params
  self.class.resource_params
end

#resource_uriObject

Retrieve the URI for this resource (relative to the base object API URL).



88
89
90
# File 'lib/springcm-sdk/resource.rb', line 88

def resource_uri
  "#{resource_name}/#{uid}"
end

#uidString

Returns The object’s unique identifier (UID).

Returns:

  • (String)

    The object’s unique identifier (UID)



7
8
9
# File 'lib/springcm-sdk/resource.rb', line 7

def uid
  href[-36..-1]
end