Class: ConstantContact::Services::LibraryService

Inherits:
BaseService
  • Object
show all
Defined in:
lib/constantcontact/services/library_service.rb

Instance Attribute Summary

Attributes inherited from BaseService

#api_client

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Constructor Details

This class inherits a constructor from ConstantContact::Services::BaseService

Instance Method Details

#add_library_file(file_name, folder_id, description, source, file_type, contents) ⇒ LibraryFile

Adds a new MyLibrary file using the multipart content-type

Parameters:

  • file_name (String)
    • The name of the file (ie: dinnerplate-special.jpg)

  • folder_id (String)
    • Folder id to add the file to

  • description (String)
    • The description of the file provided by user

  • source (String)
    • indicates the source of the original file;

    image files can be uploaded from the following sources : MyComputer, StockImage, Facebook - MyLibrary Plus customers only, Instagram - MyLibrary Plus customers only, Shutterstock, Mobile

  • file_type (String)
    • Specifies the file type, valid values are: JPEG, JPG, GIF, PDF, PNG

  • contents (String)
    • The content of the file

Returns:

  • (LibraryFile)


202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/constantcontact/services/library_service.rb', line 202

def add_library_file(file_name, folder_id, description, source, file_type, contents)
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.library_files')
  url = build_url(url)

  payload = { :file_name => file_name, :folder_id => folder_id, 
              :description => description, :source => source, :file_type => file_type, 
              :data => contents, :multipart => true }

  response = RestClient.post(url, payload, get_headers())
  location = response.headers[:location] || ''
  location.split('/').last
end

#add_library_folder(folder) ⇒ LibraryFolder

Create a new MyLibrary folder

Parameters:

  • folder (LibraryFolder)
    • MyLibrary folder to be created

Returns:

  • (LibraryFolder)


49
50
51
52
53
54
55
# File 'lib/constantcontact/services/library_service.rb', line 49

def add_library_folder(folder)
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.library_folders')
  url = build_url(url)
  payload = folder.to_json
  response = RestClient.post(url, payload, get_headers())
  Components::LibraryFolder.create(JSON.parse(response.body))
end

#delete_library_file(file_id) ⇒ Boolean

Delete one or more MyLibrary files specified by the fileId path parameter; separate multiple file IDs with a comma. Deleted files are moved from their current folder into the system Trash folder, and its status is set to Deleted.

Parameters:

  • file_id (String)
    • Specifies the MyLibrary file to delete

Returns:

  • (Boolean)


233
234
235
236
237
238
# File 'lib/constantcontact/services/library_service.rb', line 233

def delete_library_file(file_id)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.library_file'), file_id)
  url = build_url(url)
  response = RestClient.delete(url, get_headers())
  response.code == 204
end

#delete_library_folder(folder_id) ⇒ Boolean

Delete a MyLibrary folder

Parameters:

  • folder_id (String)
    • The ID for the MyLibrary folder to delete

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/constantcontact/services/library_service.rb', line 85

def delete_library_folder(folder_id)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.library_folder'), folder_id)
  url = build_url(url)
  response = RestClient.delete(url, get_headers())
  response.code == 204
end

#delete_library_trashBoolean

Permanently deletes all files in the Trash folder

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'lib/constantcontact/services/library_service.rb', line 125

def delete_library_trash()
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.library_folder_trash')
  url = build_url(url)
  response = RestClient.delete(url, get_headers())
  response.code == 204
end

#get_library_file(file_id) ⇒ LibraryFile

Retrieve a MyLibrary file using the file_id path parameter

Parameters:

  • file_id (String)
    • Specifies the MyLibrary file for which to retrieve information

Returns:

  • (LibraryFile)


183
184
185
186
187
188
# File 'lib/constantcontact/services/library_service.rb', line 183

def get_library_file(file_id)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.library_file'), file_id)
  url = build_url(url)
  response = RestClient.get(url, get_headers())
  Components::LibraryFile.create(JSON.parse(response.body))
end

#get_library_files(params = {}) ⇒ ResultSet<LibraryFile>

Retrieve a collection of MyLibrary files in the Constant Contact account

Parameters:

  • params (Hash) (defaults to: {})
    • hash of query parameters and values to append to the request.

    Allowed parameters include: type - Specifies the type of files to retrieve, valid values are : ALL, IMAGES, or DOCUMENTS source - Specifies to retrieve files from a particular source, valid values are :

    ALL - (default) files from all sources
    MyComputer
    StockImage
    Facebook
    Instagram
    Shutterstock
    Mobile

    limit - Specifies the number of results displayed per page of output, from 1 - 1000, default = 50.

Returns:

  • (ResultSet<LibraryFile>)


147
148
149
150
151
152
153
154
155
156
157
# File 'lib/constantcontact/services/library_service.rb', line 147

def get_library_files(params = {})
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.library_files')
  url = build_url(url, params)
  response = RestClient.get(url, get_headers())
  files = []
  body = JSON.parse(response.body)
  body['results'].each do |file|
    files << Components::LibraryFile.create(file)
  end
  Components::ResultSet.new(files, body['meta'])
end

#get_library_files_by_folder(folder_id, params = {}) ⇒ ResultSet<LibraryFile>

Retrieves all files from a MyLibrary folder specified by the folder_id path parameter

Parameters:

  • folder_id (String)
    • Specifies the folder from which to retrieve files

  • params (Hash) (defaults to: {})
    • hash of query parameters and values to append to the request.

    Allowed parameters include: limit - Specifies the number of results displayed per page of output, from 1 - 50, default = 50.

Returns:

  • (ResultSet<LibraryFile>)


166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/constantcontact/services/library_service.rb', line 166

def get_library_files_by_folder(folder_id, params = {})
  url = Util::Config.get('endpoints.base_url') + 
        sprintf(Util::Config.get('endpoints.library_files_by_folder'), folder_id)
  url = build_url(url, params)
  response = RestClient.get(url, get_headers())
  files = []
  body = JSON.parse(response.body)
  body['results'].each do |file|
    files << Components::LibraryFile.create(file)
  end
  Components::ResultSet.new(files, body['meta'])
end

#get_library_files_upload_status(file_id) ⇒ Array<UploadStatus>

Retrieve the upload status for one or more MyLibrary files using the file_id path parameter; separate multiple file IDs with a comma

Parameters:

  • file_id (String)
    • Specifies the files for which to retrieve upload status information

Returns:

  • (Array<UploadStatus>)


245
246
247
248
249
250
251
252
253
254
255
# File 'lib/constantcontact/services/library_service.rb', line 245

def get_library_files_upload_status(file_id)
  url = Util::Config.get('endpoints.base_url') + 
        sprintf(Util::Config.get('endpoints.library_file_upload_status'), file_id)
  url = build_url(url)
  response = RestClient.get(url, get_headers())
  statuses = []
  JSON.parse(response.body).each do |status|
    statuses << Components::UploadStatus.create(status)
  end
  statuses
end

#get_library_folder(folder_id) ⇒ LibraryFolder

Retrieve a specific MyLibrary folder using the folder_id path parameter

Parameters:

  • folder_id (String)
    • The ID for the folder to return

Returns:

  • (LibraryFolder)


61
62
63
64
65
66
# File 'lib/constantcontact/services/library_service.rb', line 61

def get_library_folder(folder_id)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.library_folder'), folder_id)
  url = build_url(url)
  response = RestClient.get(url, get_headers())
  Components::LibraryFolder.create(JSON.parse(response.body))
end

#get_library_folders(params) ⇒ ResultSet<LibraryFolder>

Retrieve a list of MyLibrary folders

Parameters:

  • params (Hash)
    • hash of query parameters and values to append to the request.

    Allowed parameters include: sort_by - The method to sort by, valid values are :

    CREATED_DATE - sorts by date folder was added, ascending (earliest to latest)
    CREATED_DATE_DESC - (default) sorts by date folder was added, descending (latest to earliest)
    MODIFIED_DATE - sorts by date folder was last modified, ascending (earliest to latest)
    MODIFIED_DATE_DESC - sorts by date folder was last modified, descending (latest to earliest)
    NAME - sorts alphabetically by folder name, a to z
    NAME_DESC - sorts alphabetically by folder name, z to a

    limit - Specifies the number of results displayed per page of output, from 1 - 50, default = 50.

Returns:

  • (ResultSet<LibraryFolder>)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/constantcontact/services/library_service.rb', line 33

def get_library_folders(params)
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.library_folders')
  url = build_url(url, params)
  response = RestClient.get(url, get_headers())
  folders = []
  body = JSON.parse(response.body)
  body['results'].each do |folder|
    folders << Components::LibraryFolder.create(folder)
  end
  Components::ResultSet.new(folders, body['meta'])
end

#get_library_infoLibrarySummary

Retrieve MyLibrary usage information

Returns:

  • (LibrarySummary)


13
14
15
16
17
18
# File 'lib/constantcontact/services/library_service.rb', line 13

def get_library_info()
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.library_info')

  response = RestClient.get(url, get_headers())
  Components::LibrarySummary.create(JSON.parse(response.body).first)
end

#get_library_trash(params) ⇒ ResultSet<LibraryFile>

Retrieve all files in the Trash folder

Parameters:

  • params (Hash)
    • hash of query parameters and values to append to the request.

    Allowed parameters include: type - Specifies the type of files to retrieve, valid values are : ALL, IMAGES, or DOCUMENTS sort_by - The method to sort by, valid values are :

    ADDED_DATE - sorts by date folder was added, ascending (earliest to latest)
    ADDED_DATE_DESC - (default) sorts by date folder was added, descending (latest to earliest)
    MODIFIED_DATE - sorts by date folder was last modified, ascending (earliest to latest)
    MODIFIED_DATE_DESC - sorts by date folder was last modified, descending (latest to earliest)
    NAME - sorts alphabetically by file name, a to z
    NAME_DESC - sorts alphabetically by file name, z to a
    SIZE - sorts by file size, smallest to largest
    SIZE_DESC - sorts by file size, largest to smallest
    DIMENSION - sorts by file dimensions (hxw), smallest to largest
    DIMENSION_DESC - sorts by file dimensions (hxw), largest to smallest

    limit - Specifies the number of results displayed per page of output, from 1 - 50, default = 50.

Returns:

  • (ResultSet<LibraryFile>)


110
111
112
113
114
115
116
117
118
119
120
# File 'lib/constantcontact/services/library_service.rb', line 110

def get_library_trash(params)
  url = Util::Config.get('endpoints.base_url') + Util::Config.get('endpoints.library_folder_trash')
  url = build_url(url, params)
  response = RestClient.get(url, get_headers())
  files = []
  body = JSON.parse(response.body)
  body['results'].each do |file|
    files << Components::LibraryFile.create(file)
  end
  Components::ResultSet.new(files, body['meta'])
end

#move_library_files(folder_id, file_id) ⇒ Array<MoveResults>

Move one or more MyLibrary files to a different folder in the user's account specify the destination folder using the folder_id path parameter.

Parameters:

  • folder_id (String)
    • Specifies the destination MyLibrary folder to which the files will be moved

  • file_id (String)
    • Specifies the files to move, in a string of comma separated file ids (e.g. 8,9)

Returns:

  • (Array<MoveResults>)


263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/constantcontact/services/library_service.rb', line 263

def move_library_files(folder_id, file_id)
  url = Util::Config.get('endpoints.base_url') + 
        sprintf(Util::Config.get('endpoints.library_file_move'), folder_id)
  url = build_url(url)

  payload = file_id.split(',').map {|id| id.strip}.to_json
  response = RestClient.put(url, payload, get_headers())
  results = []
  JSON.parse(response.body).each do |result|
    results << Components::MoveResults.create(result)
  end
  results
end

#update_library_file(file) ⇒ LibraryFile

Update information for a specific MyLibrary file

Parameters:

  • file (LibraryFile)
    • Library File to be updated

Returns:

  • (LibraryFile)


219
220
221
222
223
224
225
# File 'lib/constantcontact/services/library_service.rb', line 219

def update_library_file(file)
  url = Util::Config.get('endpoints.base_url') + sprintf(Util::Config.get('endpoints.library_file'), file.id)
  url = build_url(url)
  payload = file.to_json
  response = RestClient.put(url, payload, get_headers())
  Components::LibraryFile.create(JSON.parse(response.body))
end

#update_library_folder(folder) ⇒ LibraryFolder

Update a specific MyLibrary folder

Parameters:

  • folder (LibraryFolder)
    • MyLibrary folder to be updated

Returns:

  • (LibraryFolder)


72
73
74
75
76
77
78
79
# File 'lib/constantcontact/services/library_service.rb', line 72

def update_library_folder(folder)
  url = Util::Config.get('endpoints.base_url') + 
        sprintf(Util::Config.get('endpoints.library_folder'), folder.id)
  url = build_url(url)
  payload = folder.to_json
  response = RestClient.put(url, payload, get_headers())
  Components::LibraryFolder.create(JSON.parse(response.body))
end