./lib/animoto/client.rb in animoto-1.0.0 vs ./lib/animoto/client.rb in animoto-1.1.0

- old
+ new

@@ -16,10 +16,12 @@ require 'animoto/resources/video' require 'animoto/resources/jobs/base' require 'animoto/resources/jobs/directing_and_rendering' require 'animoto/resources/jobs/directing' require 'animoto/resources/jobs/rendering' +require 'animoto/resources/jobs/storyboard_bundling' +require 'animoto/resources/jobs/storyboard_unbundling' require 'animoto/assets/base' require 'animoto/assets/footage' require 'animoto/assets/image' require 'animoto/assets/song' @@ -27,19 +29,21 @@ require 'animoto/manifests/base' require 'animoto/manifests/directing' require 'animoto/manifests/directing_and_rendering' require 'animoto/manifests/rendering' +require 'animoto/manifests/storyboard_bundling' +require 'animoto/manifests/storyboard_unbundling' require 'animoto/http_engines/base' require 'animoto/response_parsers/base' module Animoto class Client # The default endpoint where requests go. - API_ENDPOINT = "https://api2-sandbox.animoto.com/" + API_ENDPOINT = "https://platform-sandbox.animoto.com/" # The version of the Animoto API this client targets. API_VERSION = 1 # The common prefix all vendor-specific Animoto content types share. @@ -183,20 +187,47 @@ # @return [Jobs::DirectingAndRendering] a job to monitor the status of the directing and rendering def direct_and_render! manifest, options = {} Resources::Jobs::DirectingAndRendering.load(send_manifest(manifest, Resources::Jobs::DirectingAndRendering.endpoint, options)) end + # Sends a request to bundle a storyboard. + # + # @param [Manifests::StoryboardBundling] manifest the manifest to bundle + # @param [Hash{Symbol=>Object}] options + # @return [Jobs::StoryboardBundling] a job to monitor the status of the storyboard bundling + def bundle! manifest, options = {} + Resources::Jobs::StoryboardBundling.load(send_manifest(manifest, Resources::Jobs::StoryboardBundling.endpoint, options)) + end + + # Sends a request to unbundle a storyboard. + # + # @param [Manifests::StoryboardUnbundling] manifest the manifest to unbundle + # @param [Hash{Symbol=>Object}] options + # @return [Jobs::StoryboardUnbundling] a job to monitor the status of the storyboard unbundling + def unbundle! manifest, options = {} + Resources::Jobs::StoryboardUnbundling.load(send_manifest(manifest, Resources::Jobs::StoryboardUnbundling.endpoint, options)) + end + # Update a resource with the latest attributes. Useful to update the state of a Job to # see if it's ready if you are not using HTTP callbacks. # # @param [Resources::Base] resource the resource to update # @param [Hash{Symbol=>Object}] options # @return [Resources::Base] the given resource with the latest attributes def reload! resource, options = {} resource.load(find_request(resource.class, resource.url, options)) end + # Delete a resource. May not supported for all types of resources. + # + # @param [Resources::Base] resource to delete + # @param [Hash{Symbol=>Object}] options + # @return [Boolean] true if deletion was successful + def delete! resource, options = {} + request(:delete, resource.url, nil) + end + private # Sets the API credentials from an .animotorc file. First looks for one in the current # directory, then checks ~/.animotorc, then finally /etc/.animotorc. # @@ -206,11 +237,10 @@ current_path = Dir.pwd + '/.animotorc' home_path = File.expand_path('~/.animotorc') config = if File.exist?(current_path) YAML.load(File.read(current_path)) elsif File.exist?(home_path) - home_path = File.expand_path '~/.animotorc' YAML.load(File.read(home_path)) elsif File.exist?('/etc/.animotorc') YAML.load(File.read('/etc/.animotorc')) end if config @@ -242,11 +272,11 @@ u.path = endpoint request( :post, u.to_s, response_parser.unparse(manifest.to_hash), - { "Accept" => "application/#{response_parser.format}", "Content-Type" => content_type_of(manifest) }, + { "Accept" => content_type_of(manifest.associated_job_class), "Content-Type" => content_type_of(manifest) }, options ) end # Makes a request and parses the response. @@ -255,36 +285,28 @@ # @param [String] url the URL of the request # @param [String,nil] body the request body # @param [Hash{String=>String}] headers the request headers (will be sent as-is, which means you should # specify "Content-Type" => "..." instead of, say, :content_type => "...") # @param [Hash{Symbol=>Object}] options - # @return [Hash{String=>Object}] deserialized response body - # @raise [Error] + # @return [Hash{String=>Object},Boolean] deserialized response body, or boolean indicating success or failure + # @raise [HTTPError] if something goes wrong with the request or response def request method, url, body, headers = {}, options = {} - code, body = catch(:fail) do - options = { :username => @key, :password => @secret }.merge(options) - @logger.info "Sending request to #{url.inspect} with body #{body}" - response = http_engine.request(method, url, body, headers, options) - @logger.info "Received response #{response}" - return response_parser.parse(response) - end - if code - if body.empty? - @logger.error "HTTP error (#{code})" - raise Animoto::Error.new("HTTP error (#{code})") + options = { :username => @key, :password => @secret }.merge(options) + @logger.info "Sending request to #{url.inspect} with body #{body}" + code, response_body = http_engine.request(method, url, body, headers, options) + if method == :head + (200..299) === code || raise(Animoto::HTTPError.new(url, code)) + else + case code + when 204, 205 + true + when (200..299) + response_parser.parse(response_body) else - errors = response_parser.parse(body)['response']['status']['errors'] - err_string = errors.collect { |e| e['message'] }.join(', ') - @logger.error "Error response from server: #{err_string}" - raise Animoto::Error.new(err_string) + parsed_response = begin response_body ? response_parser.parse(response_body) : nil rescue nil end + raise Animoto::HTTPError.new(url, code, parsed_response) end - else - @logger.error "Error sending request to #{url.inspect}" - raise Animoto::Error end - rescue NoMethodError => e - @logger.error e.inspect - raise Animoto::Error.new("Invalid response (#{e.inspect})") end # Creates the full content type string given a Resource class or instance # @param [Class,Support::ContentType] klass_or_instance the class or instance to build the # content type for \ No newline at end of file