Class: ImageKitIo::Request
- Inherits:
-
Object
- Object
- ImageKitIo::Request
- Includes:
- Constantable
- Defined in:
- lib/imagekitio/request.rb
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) ⇒ Request
constructor
A new instance of Request.
-
#request(method, url, headers = create_headers, payload = nil) ⇒ Object
request method communicates with server.
- #request_stream(method, url, headers: nil, payload: nil, **options, &block) ⇒ Object
Methods included from Constantable
Constructor Details
#initialize(private_key, public_key, url_endpoint, transformation_position = nil, options = nil) ⇒ Request
Returns a new instance of Request.
15 16 17 18 19 20 21 |
# File 'lib/imagekitio/request.rb', line 15 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 || constants.TRANSFORMATION_POSITION @options = || {} end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/imagekitio/request.rb', line 13 def @options end |
#private_key ⇒ Object (readonly)
Returns the value of attribute private_key.
13 14 15 |
# File 'lib/imagekitio/request.rb', line 13 def private_key @private_key end |
#public_key ⇒ Object (readonly)
Returns the value of attribute public_key.
13 14 15 |
# File 'lib/imagekitio/request.rb', line 13 def public_key @public_key end |
#transformation_position ⇒ Object (readonly)
Returns the value of attribute transformation_position.
13 14 15 |
# File 'lib/imagekitio/request.rb', line 13 def transformation_position @transformation_position end |
#url_endpoint ⇒ Object (readonly)
Returns the value of attribute url_endpoint.
13 14 15 |
# File 'lib/imagekitio/request.rb', line 13 def url_endpoint @url_endpoint end |
Instance Method Details
#auth_headers ⇒ Object
29 30 31 32 |
# File 'lib/imagekitio/request.rb', line 29 def auth_headers encoded_private_key = Base64.strict_encode64(@private_key+":") {Authorization: "Basic #{encoded_private_key}"} end |
#create_headers ⇒ Object
creates required headers
24 25 26 27 |
# File 'lib/imagekitio/request.rb', line 24 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
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/imagekitio/request.rb', line 35 def request(method, url, headers = create_headers, payload = nil) headers ||= create_headers response = {} begin if(method.downcase.to_sym == :post && payload.is_a?(Hash) && payload[:multipart]) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == 'https') req = Net::HTTP::Post::Multipart.new uri.path, payload, headers resp = http.request(req) response[:headers] = resp.to_hash else resp = RestClient::Request.new(method: method, url: url, headers: headers, payload: payload).execute response[:headers] = resp.raw_headers end response[:raw_body] = resp.body response[:status_code] = resp.code resp_c = resp.code.to_i if [400, 403].include?(resp_c) raise RestClient::ExceptionWithResponse, OpenStruct.new({ body: resp.body, code: resp_c, headers: response[:headers] }) end if (resp.code.to_i >= 200) && (resp.code.to_i < 204) content_type = resp.respond_to?(:headers) ? resp.headers[:content_type] : resp.content_type if (content_type.include? "application/json") response[:response] = JSON.parse(resp.body.to_s) else raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 404, body: resp.body) end elsif resp.code.to_i == 204 response[:response] = {'success': true} end rescue RestClient::ExceptionWithResponse => err response[:status_code] = err.http_code if response[:status_code].nil? response[:headers] = err.http_headers if response[:headers].nil? response[:error] = if err.http_code.to_i == 404 {'message': err.response.to_s} else err.response.is_a?(OpenStruct) ? JSON.parse(err.response.body) : JSON.parse(err.response) end end response end |
#request_stream(method, url, headers: nil, payload: nil, **options, &block) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/imagekitio/request.rb', line 82 def request_stream(method, url, headers: nil, payload: nil, **, &block) headers ||= create_headers response = { response: nil, error: nil } begin RestClient::Request.execute(method: method, url: url, headers: headers, payload: payload, **, block_response: block ) rescue RestClient::ExceptionWithResponse => err err.http_code == 404 ? response[:error] = {'message': err.response.to_s} : JSON.parse(err.response) end end |