Class: Ribbon::Intercom::Client::SDK

Inherits:
Object
  • Object
show all
Includes:
Utils::Mixins::MockSafe
Defined in:
lib/ribbon/intercom/client/sdk.rb,
lib/ribbon/intercom/client/sdk/adapters.rb

Direct Known Subclasses

MockSDK

Defined Under Namespace

Modules: Adapters

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Mixins::MockSafe

#mock_safe, #mock_safe!, #mock_safe?

Constructor Details

#initialize(*args) ⇒ SDK

Returns a new instance of SDK.



12
13
14
15
16
17
18
19
# File 'lib/ribbon/intercom/client/sdk.rb', line 12

def initialize(*args)
  case args.first
  when Adapters::Adapter
    self.adapter = args.first
  else
    self.adapter = Adapters::HttpAdapter.new(*args)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)

Simulates calling the remote method as if it were a local method.

Intended to be called by end-users.



78
79
80
# File 'lib/ribbon/intercom/client/sdk.rb', line 78

def method_missing(meth, *args, &block)
  call(meth, *args).retval
end

Instance Attribute Details

#adapterObject

Returns the value of attribute adapter.



10
11
12
# File 'lib/ribbon/intercom/client/sdk.rb', line 10

def adapter
  @adapter
end

Instance Method Details

#beginObject



21
22
23
24
25
26
27
# File 'lib/ribbon/intercom/client/sdk.rb', line 21

def begin
  Utils::MethodChain.begin { |methods|
    queue = Packet::MethodQueue.new
    methods.each { |meth, *args| queue.enqueue(meth, *args) }
    send_packet(method_queue: queue).retval
  }
end

#call(method_name, *args) ⇒ Object

Calls the method on the adapter returning the response Packet



46
47
48
49
50
# File 'lib/ribbon/intercom/client/sdk.rb', line 46

def call(method_name, *args)
  send_packet(
    method_queue: Packet::MethodQueue.new.enqueue(method_name, *args)
  )
end

#connect(*args) ⇒ Object



36
37
38
# File 'lib/ribbon/intercom/client/sdk.rb', line 36

def connect(*args)
  adapter.connect(*args)
end

#connected?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/ribbon/intercom/client/sdk.rb', line 40

def connected?
  adapter.connected?
end

#dupObject



60
61
62
63
64
# File 'lib/ribbon/intercom/client/sdk.rb', line 60

def dup
  super.tap { |sdk|
    sdk.adapter = adapter.dup
  }
end

#headers(new_headers = {}) ⇒ Object

Returns the headers defined for this SDK. Optionally, you may also define additional headers you’d like to add/override.



32
33
34
# File 'lib/ribbon/intercom/client/sdk.rb', line 32

def headers(new_headers={})
  adapter.headers(new_headers)
end

#send_packet(packet) ⇒ Object

Sends the packet with the adapter, returning the response Packet.

Intended to be called by Package.



56
57
58
# File 'lib/ribbon/intercom/client/sdk.rb', line 56

def send_packet(packet)
  _process_response(adapter.send_packet(packet))
end