Class: Pixaven::Client
- Inherits:
-
Object
- Object
- Pixaven::Client
- Defined in:
- lib/pixaven/client.rb
Overview
Main Client class, responsible for entire interaction with the API
Instance Method Summary collapse
-
#fetch(url) ⇒ Object
Prepares Fetch request.
-
#initialize(api_key) ⇒ Client
constructor
A new instance of Client.
-
#method ⇒ Object
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.
-
#proxy(url = nil) ⇒ Object
Sets request proxy.
-
#to_buffer ⇒ Object
Returns response containing JSON metadata and binary file buffer.
-
#to_file(path) ⇒ Object
Returns response containing JSON metadata and saves received binary file.
-
#to_json ⇒ Object
Returns response containing only JSON metadata.
-
#upload(path) ⇒ Object
Prepares Upload request.
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
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 |
#method ⇒ Object
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_buffer ⇒ Object
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
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_json ⇒ Object
Returns response containing only JSON metadata
42 43 44 |
# File 'lib/pixaven/client.rb', line 42 def to_json @request.perform end |