class CDNsunCdnApiClient

Constants

REQUEST_TIMEOUT
VERSION

Public Class Methods

new(username: , password: ) click to toggle source
# File lib/cdn_api_client.rb, line 14
def initialize(username: , password: )
  raise InvalidParameters unless [username, password].all? do |arg|
    !arg.to_s.empty?
  end

  @username = username
  @password = password
end

Public Instance Methods

body_needed?(verb) click to toggle source
# File lib/cdn_api_client.rb, line 65
def body_needed?(verb)
  verb == :post || verb == :put
end
delete(options = {}) click to toggle source
# File lib/cdn_api_client.rb, line 35
def delete(options = {})
  request(:delete, options)
end
format_url(url) click to toggle source
# File lib/cdn_api_client.rb, line 69
def format_url(url)
  "/#{url.sub(self.class.base_uri, '')}"
end
get(options = {}) click to toggle source
# File lib/cdn_api_client.rb, line 23
def get(options = {})
  request(:get, options)
end
post(options = {}) click to toggle source
# File lib/cdn_api_client.rb, line 31
def post(options = {})
  request(:post, options)
end
put(options = {}) click to toggle source
# File lib/cdn_api_client.rb, line 27
def put(options = {})
  request(:put, options)
end
request(verb, options) click to toggle source
# File lib/cdn_api_client.rb, line 39
def request(verb, options)
  options = Hash(options)

  if ![:get, :post, :put, :delete].include?(verb) ||
      options.empty? || "#{options[:url]}".empty?
    raise InvalidParameters
  end

  query_key, query = if body_needed?(verb)
    [:body, options[:data].to_json]
  else
    [:query, options[:data]]
  end

  resp = self.class.send(
    verb,
    format_url(options[:url]),
    basic_auth: { username: @username, password: @password },
    timeout: REQUEST_TIMEOUT,
    query_key => query,
    headers: { 'Content-Type' => 'application/json' }
  )

  resp.parsed_response
end