Class: HaveAPI::Client::Client

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

Overview

HaveAPI client interface.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • url (String)

    API URL

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

Options Hash (opts):

  • version (String)
  • identity (String)
  • communicator (HaveAPI::Client::Communicator)
  • block (Boolean)
  • block_interval (Integer)
  • block_timeout (Integer)
  • verify_ssl (Boolean)
  • language (String)

    value sent in Accept-Language

  • language_header (String)

    HTTP header used for language



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/haveapi/client/client.rb', line 20

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],
        language: opts[:language],
        language_header: opts[:language_header]
      }.compact
    )
    @api.identity = @opts[:identity]
  end

  @api.language = @opts[:language] if @opts.has_key?(:language)
  @api.language_header = @opts[:language_header] if @opts.has_key?(:language_header)
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.



118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/haveapi/client/client.rb', line 118

def method_missing(symbol, *args)
  return super if @setup

  setup_api

  if @resource_methods.include?(symbol)
    method(symbol).call(*args)

  else
    super
  end
end

Instance Attribute Details

#resourcesObject (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

#authHaveAPI::Client::Authentication::Base?

Get uthentication provider

Returns:



75
76
77
# File 'lib/haveapi/client/client.rb', line 75

def auth
  @api.auth
end

#authenticate(auth_method, **options, &block) ⇒ Object

See Communicator#authenticate.



68
69
70
# File 'lib/haveapi/client/client.rb', line 68

def authenticate(auth_method, **options, &block)
  @api.authenticate(auth_method, options, &block)
end

#blocking?Boolean

return [Boolean] true if global blocking mode is enabled

Returns:

  • (Boolean)


85
86
87
# File 'lib/haveapi/client/client.rb', line 85

def blocking?
  @opts[:block]
end

#client_message(key, **values) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/haveapi/client/client.rb', line 97

def client_message(key, **values)
  if @api.respond_to?(:client_message)
    @api.client_message(key, **values)
  else
    lang = @api.language if @api.respond_to?(:language)
    HaveAPI::Client::I18n.t(lang || @opts[:language], key, values)
  end
end

#communicatorHaveAPI::Client::Communicator



112
113
114
# File 'lib/haveapi/client/client.rb', line 112

def communicator
  @api
end

#compatible?Boolean

Returns:

  • (Boolean)

See Also:



80
81
82
# File 'lib/haveapi/client/client.rb', line 80

def compatible?
  @api.compatible?
end

#inspectObject



47
48
49
# File 'lib/haveapi/client/client.rb', line 47

def inspect
  "#<#{self.class.name} url=#{@api.url} version=#{@opts[:version]}>"
end

#opts(*keys) ⇒ Hash

Returns client options.

Returns:

  • (Hash)

    client options



107
108
109
# File 'lib/haveapi/client/client.rb', line 107

def opts(*keys)
  keys.empty? ? @opts.clone : @opts.slice(*keys)
end

#respond_to_missing?(symbol, *_) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
# File 'lib/haveapi/client/client.rb', line 131

def respond_to_missing?(symbol, *_)
  return super if @setup

  setup_api
  @resource_methods.include?(symbol)
end

#set_opts(opts) ⇒ Object

Override selected client options

Parameters:

  • opts (Hash)

    options



91
92
93
94
95
# File 'lib/haveapi/client/client.rb', line 91

def set_opts(opts)
  @opts.update(opts)
  @api.language = opts[:language] if opts.has_key?(:language)
  @api.language_header = opts[:language_header] if opts.has_key?(:language_header)
end

#setup(v = :_nil) ⇒ Object

Get the description from the API now.



52
53
54
55
# File 'lib/haveapi/client/client.rb', line 52

def setup(v = :_nil)
  @version = v unless v == :_nil
  setup_api
end

#versionsObject

Returns a list of API versions. The return value is a hash, e.g.:

{
versions: [1, 2, 3],
default: 3
}


63
64
65
# File 'lib/haveapi/client/client.rb', line 63

def versions
  @api.available_versions
end