Class: ResourceSpace::Resource
- Inherits:
-
Object
- Object
- ResourceSpace::Resource
- Defined in:
- lib/resourcespace/resource.rb
Overview
Resource management interface for ResourceSpace API
Instance Attribute Summary collapse
-
#client ⇒ Client
readonly
The ResourceSpace client.
Instance Method Summary collapse
-
#add_alternative_file(resource_id, file, name: nil, description: nil) ⇒ Hash
Add an alternative file to a resource.
-
#copy_resource(resource_id, resource_type: nil) ⇒ Hash
Copy a resource.
-
#create_resource(name: nil, resource_type: 1, collection: nil, metadata: {}) ⇒ Hash
Create a new resource.
-
#delete_alternative_file(resource_id, alt_file_id) ⇒ Hash
Delete an alternative file.
-
#delete_resource(resource_id) ⇒ Hash
Delete a resource.
-
#download_resource(resource_id, file_path, size: "") ⇒ Boolean
Download a resource file.
-
#edit_access?(resource_id) ⇒ Boolean
Check if user has edit access to resource.
-
#get_alternative_files(resource_id) ⇒ Array
Get alternative files for a resource.
-
#get_resource_access(resource_id) ⇒ Integer
Get resource access level.
-
#get_resource_all_image_sizes(resource_id) ⇒ Hash
Get all image sizes for a resource.
-
#get_resource_data(resource_id) ⇒ Hash
Get resource data.
-
#get_resource_field_data(resource_id, field_id = nil) ⇒ Hash
Get resource field data.
-
#get_resource_log(resource_id, entries: 50) ⇒ Array
Get resource log.
-
#get_resource_path(resource_id, size: "", page: 1, ext: "") ⇒ String
Get resource download path.
-
#get_resource_types ⇒ Array
Get resource types.
-
#initialize(client) ⇒ Resource
constructor
Initialize the resource interface.
-
#replace_resource_file(resource_id, file, no_exif: false) ⇒ Hash
Replace resource file.
-
#update_field(resource_id, field_id, value, node_values: false) ⇒ Hash
Update a resource field.
-
#update_resource_type(resource_id, resource_type) ⇒ Hash
Update resource type.
-
#upload_file(file, caption: nil, no_exif: false) ⇒ Hash
Upload a file to ResourceSpace.
-
#upload_file_by_url(resource_id, url, save_as: nil, no_exif: false) ⇒ Hash
Upload file by URL.
Constructor Details
#initialize(client) ⇒ Resource
Initialize the resource interface
24 25 26 |
# File 'lib/resourcespace/resource.rb', line 24 def initialize(client) @client = client end |
Instance Attribute Details
#client ⇒ Client (readonly)
Returns the ResourceSpace client.
19 20 21 |
# File 'lib/resourcespace/resource.rb', line 19 def client @client end |
Instance Method Details
#add_alternative_file(resource_id, file, name: nil, description: nil) ⇒ Hash
Add an alternative file to a resource
164 165 166 167 168 169 170 |
# File 'lib/resourcespace/resource.rb', line 164 def add_alternative_file(resource_id, file, name: nil, description: nil) params = { param1: resource_id.to_s } params[:param2] = name if name params[:param3] = description if description client.upload_file(file, params.merge(function: "add_alternative_file")) end |
#copy_resource(resource_id, resource_type: nil) ⇒ Hash
Copy a resource
123 124 125 126 127 128 |
# File 'lib/resourcespace/resource.rb', line 123 def copy_resource(resource_id, resource_type: nil) params = { param1: resource_id.to_s } params[:param2] = resource_type.to_s if resource_type client.post("copy_resource", params) end |
#create_resource(name: nil, resource_type: 1, collection: nil, metadata: {}) ⇒ Hash
Create a new resource
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/resourcespace/resource.rb', line 35 def create_resource(name: nil, resource_type: 1, collection: nil, metadata: {}) params = { param1: resource_type.to_s } params[:param2] = collection.to_s if collection response = client.post("create_resource", params) resource_id = response.is_a?(Hash) ? response["ref"] || response["id"] : response # Set resource name if provided if name && resource_id update_field(resource_id, 8, name) # Field 8 is typically the title/name field end # Set additional metadata if provided .each do |field, value| update_field(resource_id, field, value) end if .any? get_resource_data(resource_id) end |
#delete_alternative_file(resource_id, alt_file_id) ⇒ Hash
Delete an alternative file
177 178 179 180 181 182 |
# File 'lib/resourcespace/resource.rb', line 177 def delete_alternative_file(resource_id, alt_file_id) client.post("delete_alternative_file", { param1: resource_id.to_s, param2: alt_file_id.to_s }) end |
#delete_resource(resource_id) ⇒ Hash
Delete a resource
114 115 116 |
# File 'lib/resourcespace/resource.rb', line 114 def delete_resource(resource_id) client.post("delete_resource", { param1: resource_id.to_s }) end |
#download_resource(resource_id, file_path, size: "") ⇒ Boolean
Download a resource file
278 279 280 281 282 283 |
# File 'lib/resourcespace/resource.rb', line 278 def download_resource(resource_id, file_path, size: "") download_path = get_resource_path(resource_id, size: size) download_url = "#{client.config.url.gsub('/api/', '')}#{download_path}" client.download_file(download_url, file_path) end |
#edit_access?(resource_id) ⇒ Boolean
Check if user has edit access to resource
258 259 260 261 |
# File 'lib/resourcespace/resource.rb', line 258 def edit_access?(resource_id) response = client.get("get_edit_access", { param1: resource_id.to_s }) response == true || response == "true" || response == 1 || response == "1" end |
#get_alternative_files(resource_id) ⇒ Array
Get alternative files for a resource
153 154 155 |
# File 'lib/resourcespace/resource.rb', line 153 def get_alternative_files(resource_id) client.get("get_alternative_files", { param1: resource_id.to_s }) end |
#get_resource_access(resource_id) ⇒ Integer
Get resource access level
267 268 269 270 |
# File 'lib/resourcespace/resource.rb', line 267 def get_resource_access(resource_id) response = client.get("get_resource_access", { param1: resource_id.to_s }) response.to_i end |
#get_resource_all_image_sizes(resource_id) ⇒ Hash
Get all image sizes for a resource
219 220 221 |
# File 'lib/resourcespace/resource.rb', line 219 def get_resource_all_image_sizes(resource_id) client.get("get_resource_all_image_sizes", { param1: resource_id.to_s }) end |
#get_resource_data(resource_id) ⇒ Hash
Get resource data
76 77 78 |
# File 'lib/resourcespace/resource.rb', line 76 def get_resource_data(resource_id) client.get("get_resource_data", { param1: resource_id.to_s }) end |
#get_resource_field_data(resource_id, field_id = nil) ⇒ Hash
Get resource field data
85 86 87 88 89 90 |
# File 'lib/resourcespace/resource.rb', line 85 def get_resource_field_data(resource_id, field_id = nil) params = { param1: resource_id.to_s } params[:param2] = field_id.to_s if field_id client.get("get_resource_field_data", params) end |
#get_resource_log(resource_id, entries: 50) ⇒ Array
Get resource log
208 209 210 211 212 213 |
# File 'lib/resourcespace/resource.rb', line 208 def get_resource_log(resource_id, entries: 50) client.get("get_resource_log", { param1: resource_id.to_s, param2: entries.to_s }) end |
#get_resource_path(resource_id, size: "", page: 1, ext: "") ⇒ String
Get resource download path
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/resourcespace/resource.rb', line 137 def get_resource_path(resource_id, size: "", page: 1, ext: "") params = { param1: resource_id.to_s, param2: size.to_s, param3: page.to_s } params[:param4] = ext if ext && !ext.empty? response = client.get("get_resource_path", params) response.is_a?(String) ? response : response["path"] end |
#get_resource_types ⇒ Array
Get resource types
187 188 189 |
# File 'lib/resourcespace/resource.rb', line 187 def get_resource_types client.get("get_resource_types") end |
#replace_resource_file(resource_id, file, no_exif: false) ⇒ Hash
Replace resource file
229 230 231 232 233 234 |
# File 'lib/resourcespace/resource.rb', line 229 def replace_resource_file(resource_id, file, no_exif: false) params = { param1: resource_id.to_s } params[:param2] = "1" if no_exif client.upload_file(file, params.merge(function: "replace_resource_file")) end |
#update_field(resource_id, field_id, value, node_values: false) ⇒ Hash
Update a resource field
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/resourcespace/resource.rb', line 99 def update_field(resource_id, field_id, value, node_values: false) params = { param1: resource_id.to_s, param2: field_id.to_s, param3: value.to_s } params[:param4] = node_values.to_s if node_values client.post("update_field", params) end |
#update_resource_type(resource_id, resource_type) ⇒ Hash
Update resource type
196 197 198 199 200 201 |
# File 'lib/resourcespace/resource.rb', line 196 def update_resource_type(resource_id, resource_type) client.post("update_resource_type", { param1: resource_id.to_s, param2: resource_type.to_s }) end |
#upload_file(file, caption: nil, no_exif: false) ⇒ Hash
Upload a file to ResourceSpace
64 65 66 67 68 69 70 |
# File 'lib/resourcespace/resource.rb', line 64 def upload_file(file, caption: nil, no_exif: false) params = {} params[:param1] = caption if caption params[:param2] = "1" if no_exif client.upload_file(file, params) end |
#upload_file_by_url(resource_id, url, save_as: nil, no_exif: false) ⇒ Hash
Upload file by URL
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/resourcespace/resource.rb', line 243 def upload_file_by_url(resource_id, url, save_as: nil, no_exif: false) params = { param1: resource_id.to_s, param2: url } params[:param3] = save_as if save_as params[:param4] = "1" if no_exif client.post("upload_file_by_url", params) end |