Class: Pixaven::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/pixaven/client.rb

Overview

Main Client class, responsible for entire interaction with the API

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Returns a new instance of Client.



6
7
8
# File 'lib/pixaven/client.rb', line 6

def initialize(api_key)
    @api_key = api_key
end

Instance Method Details

#fetch(url) ⇒ Object

Prepares Fetch request

Parameters:

  • url (String)

    web location of a file we wan’t to transform via api



14
15
16
17
18
19
20
21
22
23
# 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

#methodObject

Meta programmed operation methods - every single one of them does the same thing: adds operation to be performed along with its params to the request data



86
87
88
89
90
91
92
# File 'lib/pixaven/client.rb', line 86

%i(adjust auto border crop mask output padding resize response scale store filter watermark webhook cdn flip).each do |method|
    define_method method do |value = {}|
        return self if value.empty? || !value.respond_to?(:merge)
        @request.data[method] = value
        self
    end
end

#proxy(url = nil) ⇒ Object

Sets request proxy



72
73
74
75
76
77
78
# 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

#to_bufferObject

Returns response containing JSON metadata and binary file buffer



64
65
66
67
# File 'lib/pixaven/client.rb', line 64

def to_buffer
    validate_binary_operations
    @request.perform(binary: true)
end

#to_file(path) ⇒ Object

Returns response containing JSON metadata and saves received binary file

Parameters:

  • path (String)

    file destination



51
52
53
54
55
56
57
58
# 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

#to_jsonObject

Returns response containing only JSON metadata



42
43
44
# File 'lib/pixaven/client.rb', line 42

def to_json
    @request.perform
end

#upload(path) ⇒ Object

Prepares Upload request

Parameters:

  • path (String)

    local file path



30
31
32
33
34
35
36
# 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