Class: ImageKitIo::Request

Inherits:
Object
  • Object
show all
Includes:
Constantable
Defined in:
lib/imagekitio/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Constantable

#constants, included

Constructor Details

#initialize(private_key, public_key, url_endpoint, transformation_position = nil, options = nil) ⇒ Request

Returns a new instance of Request.



14
15
16
17
18
19
20
# File 'lib/imagekitio/request.rb', line 14

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 || constants.TRANSFORMATION_POSITION
  @options = options || {}
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/imagekitio/request.rb', line 12

def options
  @options
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



12
13
14
# File 'lib/imagekitio/request.rb', line 12

def private_key
  @private_key
end

#public_keyObject (readonly)

Returns the value of attribute public_key.



12
13
14
# File 'lib/imagekitio/request.rb', line 12

def public_key
  @public_key
end

#transformation_positionObject (readonly)

Returns the value of attribute transformation_position.



12
13
14
# File 'lib/imagekitio/request.rb', line 12

def transformation_position
  @transformation_position
end

#url_endpointObject (readonly)

Returns the value of attribute url_endpoint.



12
13
14
# File 'lib/imagekitio/request.rb', line 12

def url_endpoint
  @url_endpoint
end

Instance Method Details

#auth_headersObject



28
29
30
31
# File 'lib/imagekitio/request.rb', line 28

def auth_headers
  encoded_private_key = Base64.strict_encode64(@private_key+":")
  {Authorization: "Basic #{encoded_private_key}"}
end

#create_headersObject

creates required headers



23
24
25
26
# File 'lib/imagekitio/request.rb', line 23

def create_headers
  headers = {'Accept-Encoding': "application/json", 'Content-Type': "application/json"}
  headers.update(auth_headers)
end

#request(method, url, headers = create_headers, payload = nil) ⇒ Object

request method communicates with server



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/imagekitio/request.rb', line 34

def request(method, url, headers = create_headers, 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 >= 200) && (resp.code < 204)
      if (resp.headers[:content_type].include? "application/json")
        response[:response] = JSON.parse(resp.body.to_s)
      else
        raise =RestClient::ExceptionWithResponse
      end
    elsif resp.code == 204
      response[:response] = {'success': true}
    end

  rescue RestClient::ExceptionWithResponse => err
    response[:error] = if err.http_code == 404
                         {'message': err.response.to_s}
                       else
                         JSON.parse(err.response)
                       end
  end
  response
end

#request_stream(method, url, headers: nil, payload: nil, **options, &block) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/imagekitio/request.rb', line 63

def request_stream(method, url, headers: nil, payload: nil, **options, &block)
  headers ||= create_headers
  response = { response: nil, error: nil }
  begin
    RestClient::Request.execute(method: method,
                                url: url,
                                headers: headers,
                                payload: payload,
                                **options,
                                block_response: block
                                )
  rescue RestClient::ExceptionWithResponse => err
    err.http_code == 404 ? response[:error] = {'message': err.response.to_s} : JSON.parse(err.response)
  end
end