Class: HaveAPI::Client::Client
- Inherits:
-
Object
- Object
- HaveAPI::Client::Client
- Defined in:
- lib/haveapi/client/client.rb
Overview
HaveAPI client interface.
Instance Attribute Summary collapse
-
#resources ⇒ Object
readonly
Returns the value of attribute resources.
Instance Method Summary collapse
-
#auth ⇒ HaveAPI::Client::Authentication::Base?
Get uthentication provider.
-
#authenticate(auth_method, **options, &block) ⇒ Object
See Communicator#authenticate.
-
#blocking? ⇒ Boolean
return [Boolean] true if global blocking mode is enabled.
- #communicator ⇒ HaveAPI::Client::Communicator
- #compatible? ⇒ Boolean
-
#initialize(url, opts = {}) ⇒ Client
constructor
Create an instance of client.
- #inspect ⇒ Object
-
#method_missing(symbol, *args) ⇒ Object
Initialize the client if it is not yet initialized and call the resource if it exists.
-
#opts(*keys) ⇒ Hash
Client options.
- #respond_to_missing?(symbol, *_) ⇒ Boolean
-
#set_opts(opts) ⇒ Object
Override selected client options.
-
#setup(v = :_nil) ⇒ Object
Get the description from the API now.
-
#versions ⇒ Object
Returns a list of API versions.
Constructor Details
#initialize(url, opts = {}) ⇒ Client
Create an instance of client. The client by default uses the default version of the API. API is asked for description only when needed or by calling #setup. identity is sent in each request to the API in User-Agent header.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/haveapi/client/client.rb', line 18 def initialize(url, opts = {}) @setup = false @opts = opts @version = @opts[:version] @opts[:identity] ||= 'haveapi-client' @opts[:block] = true if @opts[:block].nil? if @opts[:communicator] @api = @opts[:communicator] else @api = HaveAPI::Client::Communicator.new( url, @version, **{ verify_ssl: opts[:verify_ssl] }.compact ) @api.identity = @opts[:identity] end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
Initialize the client if it is not yet initialized and call the resource if it exists.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/haveapi/client/client.rb', line 98 def method_missing(symbol, *args) return super if @setup setup_api if @resources.include?(symbol) method(symbol).call(*args) else super end end |
Instance Attribute Details
#resources ⇒ Object (readonly)
Returns the value of attribute resources.
3 4 5 |
# File 'lib/haveapi/client/client.rb', line 3 def resources @resources end |
Instance Method Details
#auth ⇒ HaveAPI::Client::Authentication::Base?
Get uthentication provider
66 67 68 |
# File 'lib/haveapi/client/client.rb', line 66 def auth @api.auth end |
#authenticate(auth_method, **options, &block) ⇒ Object
See Communicator#authenticate.
59 60 61 |
# File 'lib/haveapi/client/client.rb', line 59 def authenticate(auth_method, **, &block) @api.authenticate(auth_method, , &block) end |
#blocking? ⇒ Boolean
return [Boolean] true if global blocking mode is enabled
76 77 78 |
# File 'lib/haveapi/client/client.rb', line 76 def blocking? @opts[:block] end |
#communicator ⇒ HaveAPI::Client::Communicator
92 93 94 |
# File 'lib/haveapi/client/client.rb', line 92 def communicator @api end |
#compatible? ⇒ Boolean
71 72 73 |
# File 'lib/haveapi/client/client.rb', line 71 def compatible? @api.compatible? end |
#inspect ⇒ Object
38 39 40 |
# File 'lib/haveapi/client/client.rb', line 38 def inspect "#<#{self.class.name} url=#{@api.url} version=#{@opts[:version]}>" end |
#opts(*keys) ⇒ Hash
Returns client options.
87 88 89 |
# File 'lib/haveapi/client/client.rb', line 87 def opts(*keys) keys.empty? ? @opts.clone : @opts.slice(*keys) end |
#respond_to_missing?(symbol, *_) ⇒ Boolean
111 112 113 114 115 116 |
# File 'lib/haveapi/client/client.rb', line 111 def respond_to_missing?(symbol, *_) return super if @setup setup_api @resources.include?(symbol) end |
#set_opts(opts) ⇒ Object
Override selected client options
82 83 84 |
# File 'lib/haveapi/client/client.rb', line 82 def set_opts(opts) @opts.update(opts) end |
#setup(v = :_nil) ⇒ Object
Get the description from the API now.
43 44 45 46 |
# File 'lib/haveapi/client/client.rb', line 43 def setup(v = :_nil) @version = v unless v == :_nil setup_api end |
#versions ⇒ Object
Returns a list of API versions. The return value is a hash, e.g.:
{
versions: [1, 2, 3],
default: 3
}
54 55 56 |
# File 'lib/haveapi/client/client.rb', line 54 def versions @api.available_versions end |