Class: ResourceSpace::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/resourcespace/resource.rb

Overview

Resource management interface for ResourceSpace API

Examples:

resources = client.resources

# Create a new resource
resource = resources.create_resource(name: "My Image")

# Upload a file
uploaded = resources.upload_file(File.open("image.jpg"))

# Get resource data
data = resources.get_resource_data(123)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Resource

Initialize the resource interface

Parameters:

  • client (Client)

    ResourceSpace client instance



24
25
26
# File 'lib/resourcespace/resource.rb', line 24

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientClient (readonly)

Returns the ResourceSpace client.

Returns:

  • (Client)

    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

Parameters:

  • resource_id (Integer)

    resource ID

  • file (File, String)

    file object or file path

  • name (String) (defaults to: nil)

    alternative file name

  • description (String) (defaults to: nil)

    alternative file description

Returns:

  • (Hash)

    response



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

Parameters:

  • resource_id (Integer)

    source resource ID

  • resource_type (Integer) (defaults to: nil)

    destination resource type (optional)

Returns:

  • (Hash)

    new resource data



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

Parameters:

  • name (String) (defaults to: nil)

    resource name

  • resource_type (Integer) (defaults to: 1)

    resource type ID (default: 1)

  • collection (Integer) (defaults to: nil)

    collection ID to add resource to

  • metadata (Hash) (defaults to: {})

    metadata fields to set

Returns:

  • (Hash)

    created resource data



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

Parameters:

  • resource_id (Integer)

    resource ID

  • alt_file_id (Integer)

    alternative file ID

Returns:

  • (Hash)

    response



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

Parameters:

  • resource_id (Integer)

    resource ID

Returns:

  • (Hash)

    response



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

Parameters:

  • resource_id (Integer)

    resource ID

  • file_path (String)

    local file path to save to

  • size (String) (defaults to: "")

    image size

Returns:

  • (Boolean)

    true if successful



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

Parameters:

  • resource_id (Integer)

    resource ID

Returns:

  • (Boolean)

    true if user has edit access



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

Parameters:

  • resource_id (Integer)

    resource ID

Returns:

  • (Array)

    alternative files data



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

Parameters:

  • resource_id (Integer)

    resource ID

Returns:

  • (Integer)

    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

Parameters:

  • resource_id (Integer)

    resource ID

Returns:

  • (Hash)

    image sizes data



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

Parameters:

  • resource_id (Integer)

    resource ID

Returns:

  • (Hash)

    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

Parameters:

  • resource_id (Integer)

    resource ID

  • field_id (Integer) (defaults to: nil)

    field ID (optional)

Returns:

  • (Hash)

    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

Parameters:

  • resource_id (Integer)

    resource ID

  • entries (Integer) (defaults to: 50)

    number of entries to return

Returns:

  • (Array)

    log entries



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

Parameters:

  • resource_id (Integer)

    resource ID

  • size (String) (defaults to: "")

    image size ('', 'pre', 'scr', 'thm', 'col')

  • page (Integer) (defaults to: 1)

    page number for multi-page documents

  • ext (String) (defaults to: "")

    file extension override

Returns:

  • (String)

    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_typesArray

Get resource types

Returns:

  • (Array)

    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

Parameters:

  • resource_id (Integer)

    resource ID

  • file (File, String)

    new file

  • no_exif (Boolean) (defaults to: false)

    skip EXIF extraction

Returns:

  • (Hash)

    response



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

Parameters:

  • resource_id (Integer)

    resource ID

  • field_id (Integer)

    field ID

  • value (String)

    field value

  • node_values (Boolean) (defaults to: false)

    whether value contains node IDs

Returns:

  • (Hash)

    response



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

Parameters:

  • resource_id (Integer)

    resource ID

  • resource_type (Integer)

    new resource type ID

Returns:

  • (Hash)

    response



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

Parameters:

  • file (File, String)

    file object or file path

  • caption (String) (defaults to: nil)

    file caption

  • no_exif (Boolean) (defaults to: false)

    whether to skip EXIF data extraction

Returns:

  • (Hash)

    uploaded file data



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

Parameters:

  • resource_id (Integer)

    resource ID

  • url (String)

    file URL

  • save_as (String) (defaults to: nil)

    filename to save as

  • no_exif (Boolean) (defaults to: false)

    skip EXIF extraction

Returns:

  • (Hash)

    response



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