Class: Ketra::Client
- Inherits:
-
Object
- Object
- Ketra::Client
- 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
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#options ⇒ Object
Returns the value of attribute options.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
Instance Method Summary collapse
-
#auth_client ⇒ OAuth2::Client
OAuth Client.
-
#authorization_url ⇒ String
The authorize endpoint URL of the Ketra OAuth2 provider.
-
#authorize(credentials) ⇒ OAuth2::AccessToken
Sets the access token, must supply either the access token, the authorization code, or the Design Studio Username and Password.
-
#get(endpoint, params = {}) ⇒ Hash
performs a GET Request using the OAuth2 Access Token and parses the result as JSON.
-
#initialize(id, secret, options = {}) ⇒ Client
constructor
Instantiate a new Ketra Client using the Client ID and Client Secret registered to your application.
-
#post(endpoint, params = {}) ⇒ Hash
performs a POST request using the OAuth2 Access Token and parses the result as JSON.
Constructor Details
#initialize(id, secret, options = {}) ⇒ Client
Instantiate a new Ketra Client using the Client ID and Client Secret registered to your application.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ketra/client.rb', line 28 def initialize(id, secret, = {}) opts = .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_token ⇒ Object (readonly)
Returns the value of attribute access_token.
17 18 19 |
# File 'lib/ketra/client.rb', line 17 def access_token @access_token end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
17 18 19 |
# File 'lib/ketra/client.rb', line 17 def id @id end |
#options ⇒ Object
Returns the value of attribute options.
16 17 18 |
# File 'lib/ketra/client.rb', line 16 def @options end |
#secret ⇒ Object (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_client ⇒ OAuth2::Client
OAuth 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_url ⇒ String
The authorize endpoint URL of the Ketra OAuth2 provider
44 45 46 |
# File 'lib/ketra/client.rb', line 44 def auth_client.auth_code.(:redirect_uri => [: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
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ketra/client.rb', line 57 def (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 => [: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
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
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 |