class Ferto::Callback

Attributes

download_url[R]

@return [String] The URL from which the resulting downloaded file can be

fetched.
error[R]

@return [String, NilClass] The error message of the download or nil if there was

no error.
extra[R]

@return [Hash] Any extra information sent by the client.

job_id[R]

@return [String] The job id generated by the Downloader.

resource_url[R]

@return [String] The original resource URL of the job.

response_code[R]

@return [Integer] The status code returned by the upstream server when the

download was attempted.

Public Class Methods

new(params) click to toggle source

@param [Hash{String => Boolean, String, Integer}] params the options to

create the Callback with

@option params [Boolean] :success Whether the download job was successful @option params [String] :error The error message of the download @option params [String] :download_url The URL from which the resulting

downloaded file can be fetched

@option params [String] :resource_url The original resource URL of the job @option params [String] :job_id The job id generated by the Downloader @option params [Integer] :response_code The status code returned by the

upstream server when the download was attempted

@option params [String] :extra Any extra information sent by the client

# File lib/ferto/callback.rb, line 35
def initialize(params)
  params = parse(params)

  @success = params[:success]
  @error = params[:error]
  @extra = params[:extra]
  @download_url = params[:download_url]
  @resource_url = params[:resource_url]
  @job_id = params[:job_id]
  @response_code = params[:response_code]
  @mime_error = params[:mime_error]
end

Public Instance Methods

download_successful?() click to toggle source

@return [Boolean] Whether the download job was successful.

# File lib/ferto/callback.rb, line 68
def download_successful?
  @success
end
matching_url?(url) click to toggle source

Determines whether the URL of the download is the same with the one the downloader used for the download.

@example

self.resource_url # => "https://img-morhipo.mncdn.com/%5Bimg%5D%5B4%5D%5B1%5D.jpg"
self.url?("https://img-morhipo.mncdn.com/[img][4][1].jpg") # => true

@param url [String]

@return [Boolean]

# File lib/ferto/callback.rb, line 58
def matching_url?(url)
  resource_url == url
end
mime_error?() click to toggle source

@return [Boolean] Return true if there is a Mime type mismatch.

# File lib/ferto/callback.rb, line 63
def mime_error?
  @mime_error
end

Private Instance Methods

parse(params) click to toggle source

@raise [Ferto::Callback::ParseError] if extra is not valid JSON

object
# File lib/ferto/callback.rb, line 76
def parse(params)
  extra = params['extra']
  if extra.empty?
    extra = {}
  else
    begin
      extra = JSON.parse(extra)
    rescue JSON::ParserError => e
      raise Ferto::Callback::ParserError.new(params, e.message)
    end
  end

  error = if params['error'].is_a?(String) && !!params['error'].empty?
            params['error']
          else
            nil
          end

  {
    success: params['success'],
    error: error,
    extra: extra,
    download_url: params['download_url'],
    resource_url: params['resource_url'],
    job_id: params['job_id'],
    response_code: params['response_code'],
    mime_error: params['error'].include?('mime-type'),
  }
end