Class: ImageKitRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/imagekit/resource.rb

Overview

ImageKitRequest requests and sends data from server

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = nil)
  @private_key = private_key
  @public_key = public_key
  @url_endpoint = url_endpoint
  @transformation_position = transformation_position || Default::TRANSFORMATION_POSITION
  @options = options || {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/imagekit/resource.rb', line 10

def options
  @options
end

#private_keyObject (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_keyObject (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_positionObject (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_endpointObject (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_headersObject



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_headersObject

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