Class: EtrieveContentApi::Connection
- Inherits:
-
Object
- Object
- EtrieveContentApi::Connection
- Defined in:
- lib/etrieve_content_api/connection.rb
Overview
Auth wrapper for and actual rest client calls to Etrieve Content API
Constant Summary collapse
- CONFIG_KEYS =
%i[ auth_url base_url password timeout username verify_ssl ].freeze
Instance Attribute Summary collapse
-
#auth_url ⇒ Object
readonly
Returns the value of attribute auth_url.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
-
#verify_ssl ⇒ Object
readonly
Returns the value of attribute verify_ssl.
Class Method Summary collapse
Instance Method Summary collapse
- #active? ⇒ Boolean
- #auth_token ⇒ Object
- #connect ⇒ Object
- #connect! ⇒ Object
-
#execute(headers: {}, &block) ⇒ Object
TODO: pass headers differently.
- #get(path, headers: {}, &block) ⇒ Object
-
#get_custom_connection(path = '', headers: {}, &block) ⇒ Object
Use this inside an execute block to make mulitiple calls in the same request.
-
#initialize(config) ⇒ Connection
constructor
A new instance of Connection.
- #post(path, payload: nil, headers: {}, &block) ⇒ Object
-
#post_custom_connection(path, payload: nil, headers: {}, &block) ⇒ Object
Use this inside an execute block to make mulitiple calls in the same request.
- #reset! ⇒ Object
Constructor Details
#initialize(config) ⇒ Connection
Returns a new instance of Connection.
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/etrieve_content_api/connection.rb', line 22 def initialize(config) if config.is_a?(String) configure_with(config) elsif config.is_a?(Hash) configure(config) else raise ConnectionConfigurationError, 'Invalid configuration options supplied.' end end |
Instance Attribute Details
#auth_url ⇒ Object (readonly)
Returns the value of attribute auth_url.
4 5 6 |
# File 'lib/etrieve_content_api/connection.rb', line 4 def auth_url @auth_url end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
5 6 7 |
# File 'lib/etrieve_content_api/connection.rb', line 5 def base_url @base_url end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
6 7 8 |
# File 'lib/etrieve_content_api/connection.rb', line 6 def connection @connection end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
9 10 11 |
# File 'lib/etrieve_content_api/connection.rb', line 9 def expires_at @expires_at end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/etrieve_content_api/connection.rb', line 7 def headers @headers end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
8 9 10 |
# File 'lib/etrieve_content_api/connection.rb', line 8 def timeout @timeout end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
10 11 12 |
# File 'lib/etrieve_content_api/connection.rb', line 10 def username @username end |
#verify_ssl ⇒ Object (readonly)
Returns the value of attribute verify_ssl.
11 12 13 |
# File 'lib/etrieve_content_api/connection.rb', line 11 def verify_ssl @verify_ssl end |
Class Method Details
.auth_token(user, password) ⇒ Object
33 34 35 |
# File 'lib/etrieve_content_api/connection.rb', line 33 def self.auth_token(user, password) Base64.strict_encode64 [user, password].join(':') end |
Instance Method Details
#active? ⇒ Boolean
74 75 76 77 78 |
# File 'lib/etrieve_content_api/connection.rb', line 74 def active? return false unless @expires_at.is_a?(Time) return false if @expires_at < Time.now - 5 true end |
#auth_token ⇒ Object
37 38 39 |
# File 'lib/etrieve_content_api/connection.rb', line 37 def auth_token @auth_token ||= self.class.auth_token(username, @password) end |
#connect ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/etrieve_content_api/connection.rb', line 41 def connect return @connection if @connection && active? begin resp = post_custom_connection( auth_url, payload: 'grant_type=client_credentials&scope=openid', headers: { authorization: "Basic #{auth_token}", accept: :json } ) results = JSON.parse(resp) rescue RestClient::ExceptionWithResponse => err results = JSON.parse(err.response) rescue results = { error: $!. } end @access_token = results['access_token'] if @access_token @headers = { authorization: "Bearer #{@access_token}" } @connection = results @expires_at = Time.now + results['expires_in'].to_i else reset! end results end |
#connect! ⇒ Object
69 70 71 72 |
# File 'lib/etrieve_content_api/connection.rb', line 69 def connect! reset! connect end |
#execute(headers: {}, &block) ⇒ Object
TODO: pass headers differently
97 98 99 100 101 102 103 104 105 |
# File 'lib/etrieve_content_api/connection.rb', line 97 def execute(headers: {}, &block) connect return false unless @connection hold_headers = @headers @headers = merge_headers(headers) out = yield(block) @headers = hold_headers out end |
#get(path, headers: {}, &block) ⇒ Object
84 85 86 87 88 |
# File 'lib/etrieve_content_api/connection.rb', line 84 def get(path, headers: {}, &block) execute(headers: headers) do get_custom_connection path, headers: @headers, &block end end |
#get_custom_connection(path = '', headers: {}, &block) ⇒ Object
Use this inside an execute block to make mulitiple calls in the same request
109 110 111 112 113 |
# File 'lib/etrieve_content_api/connection.rb', line 109 def get_custom_connection(path = '', headers: {}, &block) block ||= default_response_handler url = path =~ /\Ahttp/ ? path : [@base_url, path].join('/') rest_client_wrapper :get, url, headers: headers, &block end |
#post(path, payload: nil, headers: {}, &block) ⇒ Object
90 91 92 93 94 |
# File 'lib/etrieve_content_api/connection.rb', line 90 def post(path, payload: nil, headers: {}, &block) execute(headers: headers) do post_custom_connection path, payload: payload, headers: @headers, &block end end |
#post_custom_connection(path, payload: nil, headers: {}, &block) ⇒ Object
Use this inside an execute block to make mulitiple calls in the same request
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/etrieve_content_api/connection.rb', line 117 def post_custom_connection(path, payload: nil, headers: {}, &block) path ||= '' block ||= default_response_handler url = path =~ /\Ahttp/ ? path : [@base_url, path].join('/') RestClient::Request.execute( method: :post, url: url, payload: payload, headers: headers, verify_ssl: verify_ssl, timeout: timeout, &block ) end |
#reset! ⇒ Object
80 81 82 |
# File 'lib/etrieve_content_api/connection.rb', line 80 def reset! configure(@config) end |