class Pixaven::Client
Main Client
class, responsible for entire interaction with the API
Public Class Methods
# File lib/pixaven/client.rb, line 6 def initialize(api_key) @api_key = api_key end
Public Instance Methods
Prepares Fetch request @param url [String] web location of a file we wan't to transform via api
# File lib/pixaven/client.rb, line 14 def fetch(url) validate_request :fetch # initiating the request @request = Pixaven::Request::Fetch.new(@api_key, @proxy) # and preparing data for the request @request.data[:url] = url self end
Sets request proxy
# File lib/pixaven/client.rb, line 72 def proxy(url = nil) return self if url.nil? validate_url(url) # adding proxy if request instance already exists @request ? @request.proxy = url : @proxy = url self end
Returns response containing JSON metadata and binary file buffer
# File lib/pixaven/client.rb, line 64 def to_buffer validate_binary_operations @request.perform(binary: true) end
Returns response containing JSON metadata and saves received binary file @param path [String] file destination
# File lib/pixaven/client.rb, line 51 def to_file(path) if path.nil? raise Pixaven::Errors::NoPathProvided, "No save path provided" end validate_binary_operations @request.perform(binary: true, save_path: path) end
Returns response containing only JSON metadata
# File lib/pixaven/client.rb, line 42 def to_json @request.perform end
Prepares Upload request @param path [String] local file path
# File lib/pixaven/client.rb, line 30 def upload(path) validate_request :upload # initiating the request @request = Pixaven::Request::Upload.new(@api_key, path, @proxy) self end
Private Instance Methods
Validates if particular operations are valid for binary responses.
# File lib/pixaven/client.rb, line 113 def validate_binary_operations if @request.data[:webhook] raise Pixaven::Errors::OperationNotSupported, "Binary responses are not supported when using Webhooks" end if @request.data[:store] raise Pixaven::Errors::OperationNotSupported, "Binary responses are not supported when using External Storage" end end
Validates if we can perform request operation, i.e. if fetch was called upload should throw an exception and the other way around. So we don't chain two types of requests
@param type [Symbol] request type which we want to perform
# File lib/pixaven/client.rb, line 104 def validate_request(type) return if @request.nil? || @request.type == type raise Pixaven::Errors::RequestTypeConflict.new(@request.type, type) end
Validates proxy url
# File lib/pixaven/client.rb, line 127 def validate_url(url) return if url =~ /\A#{URI::regexp(['http', 'https'])}\z/ raise Pixaven::Errors::InvalidProxyUrl, "Proxy url is not valid" end