Class: PortaText::Client::BaseClient
- Inherits:
-
Object
- Object
- PortaText::Client::BaseClient
- Defined in:
- lib/portatext/client/base_client.rb
Overview
A generic PortaText client.
- Author
-
Marcelo Gornstein ([email protected])
- Copyright
-
Copyright © 2015 PortaText
- License
-
Apache-2.0
rubocop:disable Metrics/ClassLength
Direct Known Subclasses
Instance Attribute Summary collapse
-
#api_key ⇒ Object
writeonly
Sets the attribute api_key.
-
#credentials ⇒ Object
writeonly
Sets the attribute credentials.
-
#endpoint ⇒ Object
writeonly
Sets the attribute endpoint.
-
#executor ⇒ Object
writeonly
Sets the attribute executor.
-
#logger ⇒ Object
writeonly
Sets the attribute logger.
Instance Method Summary collapse
- #command_class_name(method) ⇒ Object
-
#initialize ⇒ BaseClient
constructor
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize rubocop:enable Metrics/ParameterLists.
- #method_missing(method, *_arguments, &_block) ⇒ Object
- #respond_to_missing?(method, *_arguments, &_block) ⇒ Boolean
-
#run(endpoint, method, content_type, accept_content_type, body, output_file = nil, auth = nil) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/ParameterLists.
Constructor Details
#initialize ⇒ BaseClient
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize rubocop:enable Metrics/ParameterLists
71 72 73 74 75 76 77 78 |
# File 'lib/portatext/client/base_client.rb', line 71 def initialize @logger = Logger.new nil @endpoint = DEFAULT_ENDPOINT @api_key = nil @credentials = nil @session_token = nil @executor = self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *_arguments, &_block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/portatext/client/base_client.rb', line 23 def method_missing(method, *_arguments, &_block) class_name = command_class_name(method) super unless defined?(class_name) command = class_name.new command.client = self command end |
Instance Attribute Details
#api_key=(value) ⇒ Object (writeonly)
Sets the attribute api_key
14 15 16 |
# File 'lib/portatext/client/base_client.rb', line 14 def api_key=(value) @api_key = value end |
#credentials=(value) ⇒ Object (writeonly)
Sets the attribute credentials
15 16 17 |
# File 'lib/portatext/client/base_client.rb', line 15 def credentials=(value) @credentials = value end |
#endpoint=(value) ⇒ Object (writeonly)
Sets the attribute endpoint
13 14 15 |
# File 'lib/portatext/client/base_client.rb', line 13 def endpoint=(value) @endpoint = value end |
#executor=(value) ⇒ Object (writeonly)
Sets the attribute executor
16 17 18 |
# File 'lib/portatext/client/base_client.rb', line 16 def executor=(value) @executor = value end |
#logger=(value) ⇒ Object (writeonly)
Sets the attribute logger
17 18 19 |
# File 'lib/portatext/client/base_client.rb', line 17 def logger=(value) @logger = value end |
Instance Method Details
#command_class_name(method) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/portatext/client/base_client.rb', line 31 def command_class_name(method) method = method.to_s.split('_').map(&:capitalize) Object.const_get('PortaText') .const_get('Command') .const_get('Api') .const_get(method.join('')) end |
#respond_to_missing?(method, *_arguments, &_block) ⇒ Boolean
19 20 21 |
# File 'lib/portatext/client/base_client.rb', line 19 def respond_to_missing?(method, *_arguments, &_block) defined?(command_class_name method) || super end |
#run(endpoint, method, content_type, accept_content_type, body, output_file = nil, auth = nil) ⇒ Object
rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize rubocop:disable Metrics/ParameterLists
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 |
# File 'lib/portatext/client/base_client.rb', line 42 def run( endpoint, method, content_type, accept_content_type, body, output_file = nil, auth = nil ) true_endpoint = "#{@endpoint}/#{endpoint}" auth ||= auth_method(auth) headers = form_headers content_type, accept_content_type, auth @logger.debug "Calling #{method} #{true_endpoint} with #{auth}" descriptor = PortaText::Command::Descriptor.new( true_endpoint, method, headers, body, output_file ) ret_code, ret_headers, ret_body = @executor.execute descriptor @logger.debug "Got: #{ret_code} / #{ret_headers} / #{ret_body}" ret_body = '{}' if ret_body.nil? ret_body = JSON.parse ret_body result = PortaText::Command::Result.new ret_code, ret_headers, ret_body if ret_code.eql?(401) && auth.eql?(:session_token) login! result = run( endpoint, method, content_type, accept_content_type, body, output_file, auth ) end assert_result descriptor, result end |