Class: Ketra::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ketra/client.rb

Overview

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

Constant Summary collapse

PRODUCTION_HOST =

Production Host base url

'https://my.goketra.com'
REMOTE_PRODUCTION_HOST =
'https://api.goketra.com'
TEST_HOST =

Test Host base url

'https://internal-my.goketra.com'
REMOTE_TEST_HOST =
'https://internal-api.goketra.com'
LOCAL_ENDPOINT_PREFIX =

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

'ketra.cgi/api/v1'
REMOTE_ENDPOINT_PREFIX =
'webAPI/api/v1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, secret, options = {}) ⇒ Client

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

Parameters:

  • id (String)

    the Client ID value

  • secret (String)

    the Client Secret value

  • options (Hash) (defaults to: {})

    the options to create the client with

Options Hash (options):

  • :server (Symbol) — default: :production

    which authorization server to use to get an Access Token (:production or :test)

  • :redirect_uri (String)

    the redirect uri for the authorization code OAuth2 grant type

  • :hub_serial (String)

    the serial number of the Hub to communicate with



28
29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



17
18
19
# File 'lib/ketra/client.rb', line 17

def access_token
  @access_token
end

#idObject (readonly)

Returns the value of attribute id.



17
18
19
# File 'lib/ketra/client.rb', line 17

def id
  @id
end

#optionsObject

Returns the value of attribute options.



16
17
18
# File 'lib/ketra/client.rb', line 16

def options
  @options
end

#secretObject (readonly)

Returns the value of attribute secret.



17
18
19
# File 'lib/ketra/client.rb', line 17

def secret
  @secret
end

Instance Method Details

#auth_clientOAuth2::Client

OAuth Client

Returns:

  • (OAuth2::Client)

    oauth2 client



72
73
74
75
76
77
# 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_urlString

The authorize endpoint URL of the Ketra OAuth2 provider

Returns:

  • (String)

    authorize endpoint URL



44
45
46
# File 'lib/ketra/client.rb', line 44

def authorization_url
  auth_client.auth_code.authorize_url(:redirect_uri => options[:redirect_uri])
end

#authorize(credentials) ⇒ OAuth2::AccessToken

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

Parameters:

  • credentials (Hash)

Options Hash (credentials):

  • :token (String)

    previously gained access token value

  • :authorization_code (String)

    code value from the Ketra OAuth2 provider

  • :username (String)

    Ketra Desiadgn Studio username

  • :password (String)

    Design Studio password

Returns:

  • (OAuth2::AccessToken)

    Access Token object



57
58
59
60
61
62
63
64
65
66
67
# 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 = {}) ⇒ Hash

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

Parameters:

  • endpoint (String)

    to be appended to the base url

  • params (Hash) (defaults to: {})

    to be used as query params for the request

Returns:

  • (Hash)

    deserialized response hash



84
85
86
# 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 = {}) ⇒ Hash

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

Parameters:

  • endpoint (String)

    to be appended to the base url

  • params (Hash) (defaults to: {})

    except :query_params will be serialized into JSON and supplied as the body of the request

Options Hash (params):

  • :query_params (Hash)

    to be used as the query params for the request

Returns:

  • (Hash)

    deserialized response hash



94
95
96
97
98
99
100
101
# 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