class Ketra::Client

The Ketra::Client class is used for gaining an Access Token for authorization and performing GET and POST requests to the Ketra API

Constants

LOCAL_ENDPOINT_PREFIX

Endpoint prefix to be attached to the base url before the rest of the endpoint

PRODUCTION_HOST

Production Host base url

REMOTE_ENDPOINT_PREFIX
REMOTE_PRODUCTION_HOST
REMOTE_TEST_HOST
TEST_HOST

Test Host base url

Attributes

access_token[R]
id[R]
options[RW]
secret[R]

Public Class Methods

new(id, secret, options = {}) click to toggle source

Instantiate a new Ketra Client using the Client ID and Client Secret registered to your application.

@param [String] id the Client ID value @param [String] secret the Client Secret value @param [Hash] options the options to create the client with @option options [Symbol] :server (:production) which authorization server to use to get an Access Token (:production or :test) @option options [String] :redirect_uri the redirect uri for the authorization code OAuth2 grant type @option options [String] :hub_serial the serial number of the Hub to communicate with

# File lib/ketra/client.rb, line 28
def initialize(id, secret, options = {})
  opts = options.dup 
  @id = id
  @secret = secret
  @options = {:server             => :production,
              :redirect_uri       => 'urn:ietf:wg:oauth:2.0:oob',
              :hub_discovery_mode => :cloud,
              :api_mode           => :local}.merge(opts)
              
end

Public Instance Methods

auth_client() click to toggle source

OAuth Client

@return [OAuth2::Client] oauth2 client

# File lib/ketra/client.rb, line 72
def auth_client
  @auth_client ||= OAuth2::Client.new(Ketra.client_id,
                                      Ketra.client_secret,
                                      :site => host,
                                      :ssl => { :verify => false })
end
authorization_url() click to toggle source

The authorize endpoint URL of the Ketra OAuth2 provider

@return [String] authorize endpoint URL

# File lib/ketra/client.rb, line 44
def authorization_url
  auth_client.auth_code.authorize_url(:redirect_uri => options[:redirect_uri])
end
authorize(credentials) click to toggle source

Sets the access token, must supply either the access token, the authorization code, or the Design Studio Username and Password

@param [Hash] credentials @option credentials [String] :token previously gained access token value @option credentials [String] :authorization_code code value from the Ketra OAuth2 provider @option credentials [String] :username Ketra Desiadgn Studio username @option credentials [String] :password Design Studio password @return [OAuth2::AccessToken] Access Token object

# File lib/ketra/client.rb, line 57
def authorize(credentials)
  if credentials.key?(:token)
    @access_token = OAuth2::AccessToken.new(auth_client, credentials[:token])
  elsif credentials.key?(:authorization_code)
    @access_token = auth_client.auth_code.get_token(credentials[:authorization_code],
                                                    :redirect_uri => options[:redirect_uri])
  elsif credentials.key?(:username)
    @access_token = auth_client.password.get_token(credentials[:username],
                                                   credentials[:password])
  end
end
get(endpoint, params = {}) click to toggle source

performs a GET Request using the OAuth2 Access Token and parses the result as JSON

@param [String] endpoint to be appended to the base url @param [Hash] params to be used as query params for the request @return [Hash] deserialized response hash

# File lib/ketra/client.rb, line 84
def get(endpoint, params = {})
  JSON.parse access_token.get(url(endpoint), :params => params).body
end
post(endpoint, params = {}) click to toggle source

performs a POST request using the OAuth2 Access Token and parses the result as JSON

@param [String] endpoint to be appended to the base url @param [Hash] params except :query_params will be serialized into JSON and supplied as the body of the request @option params [Hash] :query_params to be used as the query params for the request @return [Hash] deserialized response hash

# File lib/ketra/client.rb, line 94
def post(endpoint, params = {})
  internal_params = params.dup
  resp = access_token.post url(endpoint),
                           :params => internal_params.delete(:query_params),
                           :body => JSON.generate(internal_params),
                           :headers => { 'Content-Type' => 'application/json' }
  JSON.parse resp.body
end

Private Instance Methods

cloud_discovery(serial_number) click to toggle source
# File lib/ketra/client.rb, line 152
def cloud_discovery(serial_number)
  @hub_ip ||= perform_cloud_hub_discovery(serial_number)
end
discover_hub(serial_number) click to toggle source
# File lib/ketra/client.rb, line 147
def discover_hub(serial_number)
  #TODO implement additional hub discovery modes
  cloud_discovery(serial_number)
end
host() click to toggle source
# File lib/ketra/client.rb, line 105
def host
  case options[:server]
  when :test
    TEST_HOST
  else
    PRODUCTION_HOST
  end
end
hub_ip() click to toggle source
# File lib/ketra/client.rb, line 143
def hub_ip
  @hub_ip || discover_hub(options[:hub_serial])
end
local_url() click to toggle source
# File lib/ketra/client.rb, line 135
def local_url
  "https://#{hub_ip}/#{LOCAL_ENDPOINT_PREFIX}"
end
perform_cloud_hub_discovery(serial_number) click to toggle source
# File lib/ketra/client.rb, line 156
def perform_cloud_hub_discovery(serial_number)
  response = auth_client.request :get, "#{host}/api/n4/v1/query"
  info = response.parsed["content"].detect { |h| h["serial_number"] == serial_number }
  raise RuntimeError, "Could not discover hub with serial: #{serial_number}" if info.nil?
  info["internal_ip"]
end
remote_host() click to toggle source
# File lib/ketra/client.rb, line 114
def remote_host
  case options[:server]
  when :test
    REMOTE_TEST_HOST
  else
    REMOTE_PRODUCTION_HOST
  end
end
remote_url() click to toggle source
# File lib/ketra/client.rb, line 139
def remote_url
  "#{remote_host}/#{options[:installation_id]}/#{options[:hub_serial]}/#{REMOTE_ENDPOINT_PREFIX}"
end
url(endpoint) click to toggle source
# File lib/ketra/client.rb, line 123
def url(endpoint)
  #TODO implement additional api modes
  case options[:api_mode]
  when :remote
    base = remote_url
  else
    base = local_url
  end
  url = "#{base}/#{endpoint}"
  Addressable::URI.encode(url)
end