Module: Cloudinary::BaseApi

Included in:
AccountApi, Api
Defined in:
lib/cloudinary/base_api.rb

Defined Under Namespace

Classes: AlreadyExists, AuthorizationRequired, BadRequest, Error, GeneralError, NotAllowed, NotFound, RateLimited, Response

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



35
36
37
38
39
# File 'lib/cloudinary/base_api.rb', line 35

def self.extended(base)
  [Error, NotFound, NotAllowed, AlreadyExists, RateLimited, BadRequest, GeneralError, AuthorizationRequired, Response].each do |constant|
    base.const_set(constant.name.split("::").last, constant)
  end
end

Instance Method Details

#call_json_api(method, api_url, payload, timeout, headers, proxy = nil, user = nil, password = nil) ⇒ Object

Raises:

  • (exception_class)


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
# File 'lib/cloudinary/base_api.rb', line 41

def call_json_api(method, api_url, payload, timeout, headers, proxy = nil, user = nil, password = nil)
  conn = Faraday.new(url: api_url) do |faraday|
    faraday.proxy = proxy if proxy
    faraday.request :json
    faraday.adapter @adapter || Faraday.default_adapter
  end

  response = conn.run_request(method.downcase.to_sym, nil, payload, headers) do |req|
    req.options.timeout = timeout if timeout
    req.basic_auth(user, password) if user && password
  end

  return Response.new(response) if response.status == 200
  exception_class = case response.status
                    when 400 then BadRequest
                    when 401 then AuthorizationRequired
                    when 403 then NotAllowed
                    when 404 then NotFound
                    when 409 then AlreadyExists
                    when 420 then RateLimited
                    when 500 then GeneralError
                    else raise GeneralError.new("Server returned unexpected status code - #{response.status} - #{response.body}")
                    end
  json = Cloudinary::Api.parse_json_response(response)
  raise exception_class.new(json["error"]["message"])
end