Class: Ocs::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, api_key:, secret_key:, path: "/client/api", logger: nil, ssl: true) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
# File 'lib/ocs/client.rb', line 5

def initialize(host:, api_key:, secret_key:, path: "/client/api", logger: nil, ssl: true)
  @host       = host
  @api_key    = api_key
  @secret_key = secret_key
  @path       = path
  @logger     = logger
  @ssl        = ssl
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ocs/client.rb', line 52

def method_missing(method, *args)
  return super unless args.first.to_s =~ /^[a-zA-Z]/
  
  raw_resource_name, *arguments = args
  resource_name = raw_resource_name.to_s.singularize
  if Ocs::Resources.const_defined?(resource_name.camelize)
    return resource_class(resource_name).public_send(method, self, *arguments)
  end

  super
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



3
4
5
# File 'lib/ocs/client.rb', line 3

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/ocs/client.rb', line 3

def host
  @host
end

#loggerObject (readonly)

Returns the value of attribute logger.



3
4
5
# File 'lib/ocs/client.rb', line 3

def logger
  @logger
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/ocs/client.rb', line 3

def path
  @path
end

#secret_keyObject (readonly)

Returns the value of attribute secret_key.



3
4
5
# File 'lib/ocs/client.rb', line 3

def secret_key
  @secret_key
end

#sslObject (readonly)

Returns the value of attribute ssl.



3
4
5
# File 'lib/ocs/client.rb', line 3

def ssl
  @ssl
end

Instance Method Details

#call(name, options = {}) ⇒ Object



14
15
16
# File 'lib/ocs/client.rb', line 14

def call(name, options = {})
  send(name, options).content
end

#connectionObject



18
19
20
21
22
23
24
# File 'lib/ocs/client.rb', line 18

def connection
  @connection ||=
    Faraday.new(url: url_prefix) do |connection|
      connection.response :json
      connection.adapter Faraday.default_adapter
    end
end

#new(resource_name, options = {}) ⇒ Object



26
27
28
# File 'lib/ocs/client.rb', line 26

def new(resource_name, options = {})
  resource_class(resource_name).new(self, options)
end

#send(name, options = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/ocs/client.rb', line 30

def send(name, options = {})
  Request.new(
    name: name,
    options: options,
    client: self
  ).send
end