class CueCloudRequest

Attributes

api_key[R]

uncomment the following line to debug the entire http party and have fun ! debug_output $stdout

api_pass[R]

uncomment the following line to debug the entire http party and have fun ! debug_output $stdout

auth_key[R]

uncomment the following line to debug the entire http party and have fun ! debug_output $stdout

base_url[R]

uncomment the following line to debug the entire http party and have fun ! debug_output $stdout

body[R]

uncomment the following line to debug the entire http party and have fun ! debug_output $stdout

logger[R]

uncomment the following line to debug the entire http party and have fun ! debug_output $stdout

Public Class Methods

new(api_key, api_pass,base_url=nil) click to toggle source

base_uri CueCloudApi::DEFAULT_BASE_URL + CueCloudApi::API_VERSION

# File lib/cuecloud/cuecloud_request.rb, line 12
def initialize(api_key, api_pass,base_url=nil)
  @api_key = api_key.to_s
  @api_pass = api_pass.to_s
  @auth_key = {username: api_key, password: api_pass}
  if base_url
    @base_url = base_url
  else
    @base_url = CueCloudApi::DEFAULT_BASE_URL + CueCloudApi::API_VERSION
  end
end

Public Instance Methods

build_request(resource, method, data=nil) click to toggle source
# File lib/cuecloud/cuecloud_request.rb, line 23
def build_request(resource, method, data=nil)

  url = build_url(resource)
  body = prepare_body(method, data)
  nonce = generate_nonce

  message = nonce + url + body

  headers = {
      'Access-Key' => api_key,
      'Access-Signature' => signature(message),
      'Access-Nonce' => nonce,
      'Content-Type' => 'application/json',
  }

  request_call(method, url, auth_key, headers, data)

end

Private Instance Methods

build_url(resource) click to toggle source
# File lib/cuecloud/cuecloud_request.rb, line 44
def build_url(resource)
  base_url + "/" + resource
end
generate_nonce() click to toggle source
# File lib/cuecloud/cuecloud_request.rb, line 75
def generate_nonce
  (Time.now.to_f * 1e6).to_i.to_s
end
prepare_body(method, data) click to toggle source
# File lib/cuecloud/cuecloud_request.rb, line 48
def prepare_body(method, data)
  if data && method =~ /post/i
    data.to_json
  else
    ""
  end
end
request_call(method, url, auth_key, headers, data) click to toggle source
# File lib/cuecloud/cuecloud_request.rb, line 56
def request_call(method, url, auth_key, headers, data)
  case method
    when /get/i
      body = self.class.get(url, basic_auth: auth_key, headers: headers).response.body
      return JSON.parse(body)
    when /post/i
      body = self.class.post(url, basic_auth: auth_key, headers: headers, body: data.to_json).response.body
      return JSON.parse(body)
    when /put/i
    when /delete/i
    else
      raise CueCloudException.new("Invalid method:#{method}")
  end
end
signature(message) click to toggle source
# File lib/cuecloud/cuecloud_request.rb, line 71
def signature(message)
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), api_pass, message)
end