lib/cdnconnect_api.rb in cdnconnect-api-0.3.3 vs lib/cdnconnect_api.rb in cdnconnect-api-0.4.0
- old
+ new
@@ -23,11 +23,11 @@
##
# Used to easily interact with CDN Connect API.
class APIClient
@@application_name = 'cdnconnect-api-ruby'
- @@application_version = '0.3.3'
+ @@application_version = '0.4.0'
@@user_agent = @@application_name + ' v' + @@application_version
@@api_host = 'https://api.cdnconnect.com'
@@api_version = 'v1'
##
@@ -170,11 +170,15 @@
# - <code>:webhook_format</code> -
# When a `webhook_url` is provided, you can have the data formatted as either `json`
# or `xml`. The defautl format is `json`.
#
# @return [APIResponse] A response object with helper methods to read the response.
- def upload(options={})
+ def upload(options)
+ if options[:app_host] == nil
+ options[:app_host] = @app_host
+ end
+
# Make sure we've got good source data before starting the upload
prepare_upload(options)
# Place all of the source files in an upload queue for each destination folder.
# Up to 25 files can be sent in one POST request. As uploads are successful
@@ -198,11 +202,11 @@
if upload_url == nil
# We do not already have an upload url created. The first upload request
# will need to make a request for an upload url. After the first upload
# each upload response will also include a new upload url which can be used
# for the next upload when uploading to the same folder.
- upload_url_response = self.get_upload_url(destination_path)
+ upload_url_response = self.get_upload_url(options, destination_path)
if upload_url_response.is_error
return upload_url_response
end
upload_url = upload_url_response.get_result('upload_url')
upload_id = upload_url_response.get_result('upload_id')
@@ -528,11 +532,11 @@
##
# This method should not be called directly, but is used by the upload method
# to get the options all ready to go and validated before uploading a file(s).
# @!visibility private
- def prepare_upload(options={})
+ def prepare_upload(options)
# Check if we've got valid source files
if options[:source_folder_path] != nil
# Check that the source folder exists
if not File.directory?(options[:source_folder_path])
@@ -598,22 +602,22 @@
##
# An upload url must be optained first before uploading a file. After the first
# upload url is received, all upload responses contain another upload which can be
# used to eliminate the need to do seperate requests for an upload url.
# @!visibility private
- def get_upload_url(destination_path)
+ def get_upload_url(options, destination_path)
if destination_path == "/"
destination_path = ""
end
upload_id = ((9_999_999 - 1_000_000) * rand + 1_000_000).to_i
- api_path = "#{destination_path}/upload-#{upload_id}.json"
+ path = "/#{options[:app_host]}#{destination_path}/upload-#{upload_id}.json"
- @logger.debug("get_upload_url: #{api_path}")
+ @logger.debug("get_upload_url: #{path}")
i = 1
begin
- response = get(api_path)
+ response = get(path)
if not response.is_server_error
return response
elsif i > 2
@logger.error("Too many get_upload_url attempts")
return response
@@ -635,20 +639,23 @@
# its sub-files or not. Default is false.
# - <code>:folders</code> -
# True or false value indicating if a folder's response should contain
# its sub-folders or not. Default is false.
# @return [APIResponse] A response object with helper methods to read the response.
- def get_object(options={})
- api_path = options[:path] + '.json'
+ def get_object(options)
+ if options[:app_host] == nil
+ options[:app_host] = @app_host
+ end
+ path = "/#{options[:app_host]}#{options[:path]}.json"
data = {}
if options[:files] == true
data[:files] = true
end
if options[:folders] == true
data[:folders] = true
end
- get(api_path, data)
+ get(path, data)
end
##
# Rename object, which can be either a file or folder.
@@ -657,99 +664,129 @@
# - <code>:path</code> -
# The path to the CDN Connect object to get. (required)
# - <code>:new_name</code> -
# The new filename or folder name for the object. (required)
# @return [APIResponse] A response object with helper methods to read the response.
- def rename_object(options={})
- api_path = options[:path] + '/rename.json'
+ def rename_object(options)
+ if options[:app_host] == nil
+ options[:app_host] = @app_host
+ end
+ path = "/#{options[:app_host]}#{options[:path]}/rename.json"
data = { :new_name => options[:new_name] }
- put(api_path, data)
+ put(path, data)
end
##
# Delete object info, which can be either a file or folder.
#
# @param [Hash] options
# - <code>:path</code> -
# The path to the CDN Connect object to delete. (required)
# @return [APIResponse] A response object with helper methods to read the response.
- def delete_object(options={})
- api_path = options[:path] + '.json'
- delete(api_path)
+ def delete_object(options)
+ if options[:app_host] == nil
+ options[:app_host] = @app_host
+ end
+ path = "/#{options[:app_host]}#{options[:path]}.json"
+ delete(path)
end
##
# Create a folder path. If any of the folders within the given path do not
# already exist they will be created.
#
# @return [APIResponse] A response object with helper methods to read the response.
- def create_path(options={})
- api_path = options[:path] + '/create-path.json'
- get(api_path)
+ def create_path(options)
+ if options[:app_host] == nil
+ options[:app_host] = @app_host
+ end
+ path = "/#{options[:app_host]}#{options[:path]}/create-path.json"
+ get(path)
end
+
+ ##
+ # Create a new CDN Connect app. The creator of the app will be the
+ # same user as the creator of the API Key for the request.
+ #
+ # @param [Hash] options
+ # The configuration parameters for the client.
+ # - <code>:subdomain</code> -
+ # This new app's subdomain to the "cdnconnect.com" domain.
+ # - <code>:label</code> -
+ # The label for this new app.
+ #
+ # @return [APIResponse] A response object with helper methods to read the response.
+ def create_app(options)
+ path = '/apps.json'
+ data = {}
+ data[:subdomain] = options[:subdomain]
+ data[:label] = options[:label]
+ post(path, data)
+ end
+
##
# Executes a GET request to an API URL and returns a response object.
# GET requests are used when reading data.
#
- # @param api_path [String] The API path to send the GET request to.
+ # @param path [String] The API path to send the GET request to.
# @param data [Hash] Data which will be placed in the GET request's querystring. (Optional)
# @return [APIResponse] A response object with helper methods to read the response.
- def get(api_path, data={})
- fetch(:api_path => api_path, :method => 'GET', :data => data)
+ def get(path, data={})
+ fetch(:path => path, :method => 'GET', :data => data)
end
##
# Executes a POST request to an API URL and returns a response object.
# POST requests are used when creating data.
#
- # @param api_path [String] The API path to send the POST request to.
+ # @param path [String] The API path to send the POST request to.
# @param data [Hash] Data which will be sent in the POST request.
# @return [APIResponse] A response object with helper methods to read the response.
- def post(api_path, data)
- fetch(:api_path => api_path, :method => 'POST', :data => data)
+ def post(path, data)
+ fetch(:path => path, :method => 'POST', :data => data)
end
##
# Executes a PUT request to an API URL and returns a response object.
# PUT requests are used when updating data.
#
- # @param api_path [String] The API path to send the PUT request to.
+ # @param path [String] The API path to send the PUT request to.
# @param data [Hash] Data which will be sent in the PUT request.
# @return [APIResponse] A response object with helper methods to read the response.
- def put(api_path, data)
- fetch(:api_path => api_path, :method => 'PUT', :data => data)
+ def put(path, data)
+ fetch(:path => path, :method => 'PUT', :data => data)
end
##
# Executes a DELETE request to an API URL and returns a response object.
# DELETE requests are used when (you guessed it) deleting data.
#
- # @param api_path [String] The API path to send the DELETE request to.
+ # @param path [String] The API path to send the DELETE request to.
# @return [APIResponse] A response object with helper methods to read the response.
- def delete(api_path)
- fetch(:api_path => api_path, :method => 'DELETE')
+ def delete(path)
+ fetch(:path => path, :method => 'DELETE')
end
-
+
##
# This method should not be called directly, but is used to validate data
# and make it all pretty before firing off the request to the API.
# @!visibility private
- def prepare(options={})
- if options[:api_path] == nil
- raise ArgumentError, 'missing api path'
+ def prepare(options)
+ if options[:path] == nil
+ raise ArgumentError, 'missing path'
end
options[:headers] = { 'User-Agent' => @@user_agent }
- options[:uri] = "#{@@api_host}/#{@@api_version}/#{@app_host}#{options[:api_path]}"
+ options[:uri] = "#{@@api_host}/#{@@api_version}#{options[:path]}"
options[:method] = options[:method] || 'GET'
if options[:method] == 'GET' and options[:data] != nil and options[:data].length > 0
require "addressable/uri"
@@ -766,10 +803,10 @@
##
# Guts of an authorized request. Do not call this directly.
# @!visibility private
- def fetch(options={})
+ def fetch(options)
# Prepare the data to be shipped in the request
options = prepare(options)
@logger.debug(options[:method] + ': ' + options[:uri])