Class: ImageKitRequest
- Inherits:
-
Object
- Object
- ImageKitRequest
- Defined in:
- lib/imagekit/resource.rb
Overview
ImageKitRequest requests and sends data from server
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#private_key ⇒ Object
readonly
Returns the value of attribute private_key.
-
#public_key ⇒ Object
readonly
Returns the value of attribute public_key.
-
#transformation_position ⇒ Object
readonly
Returns the value of attribute transformation_position.
-
#url_endpoint ⇒ Object
readonly
Returns the value of attribute url_endpoint.
Instance Method Summary collapse
- #auth_headers ⇒ Object
-
#create_headers ⇒ Object
creates required headers.
-
#initialize(private_key, public_key, url_endpoint, transformation_position = nil, options = nil) ⇒ ImageKitRequest
constructor
A new instance of ImageKitRequest.
-
#request(method, url, headers = nil, payload = nil) ⇒ Object
request method communicates with server.
Constructor Details
#initialize(private_key, public_key, url_endpoint, transformation_position = nil, options = nil) ⇒ ImageKitRequest
Returns a new instance of ImageKitRequest.
12 13 14 15 16 17 18 |
# File 'lib/imagekit/resource.rb', line 12 def initialize(private_key, public_key, url_endpoint, transformation_position = nil, = nil) @private_key = private_key @public_key = public_key @url_endpoint = url_endpoint @transformation_position = transformation_position || Default::TRANSFORMATION_POSITION @options = || {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/imagekit/resource.rb', line 10 def @options end |
#private_key ⇒ Object (readonly)
Returns the value of attribute private_key.
10 11 12 |
# File 'lib/imagekit/resource.rb', line 10 def private_key @private_key end |
#public_key ⇒ Object (readonly)
Returns the value of attribute public_key.
10 11 12 |
# File 'lib/imagekit/resource.rb', line 10 def public_key @public_key end |
#transformation_position ⇒ Object (readonly)
Returns the value of attribute transformation_position.
10 11 12 |
# File 'lib/imagekit/resource.rb', line 10 def transformation_position @transformation_position end |
#url_endpoint ⇒ Object (readonly)
Returns the value of attribute url_endpoint.
10 11 12 |
# File 'lib/imagekit/resource.rb', line 10 def url_endpoint @url_endpoint end |
Instance Method Details
#auth_headers ⇒ Object
26 27 28 29 |
# File 'lib/imagekit/resource.rb', line 26 def auth_headers encoded_private_key = Base64.strict_encode64(@private_key+":") {Authorization: "Basic #{encoded_private_key}"} end |
#create_headers ⇒ Object
creates required headers
21 22 23 24 |
# File 'lib/imagekit/resource.rb', line 21 def create_headers headers = {'Accept-Encoding': "application/json", 'Content-Type': "application/json"} headers.update(auth_headers) end |
#request(method, url, headers = nil, payload = nil) ⇒ Object
request method communicates with server
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/imagekit/resource.rb', line 32 def request(method, url, headers = nil, payload = nil) headers ||= create_headers response = {response: nil, error: nil} begin resp = RestClient::Request.new(method: method, url: url, headers: headers, payload: payload).execute if resp.code == 404 raise RestClient::ExceptionWithResponse elsif (resp.code >= 200) && (resp.code < 204) response[:response] = JSON.parse(resp.body.to_s) elsif resp.code == 204 response[:response] = {'success': true} end rescue RestClient::ExceptionWithResponse => err err.http_code == 404 ? response[:error] = {'message': err.response.to_s} : JSON.parse(err.response) end response end |