Class: Ribbon::Intercom::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/ribbon/intercom/service.rb,
lib/ribbon/intercom/service/channel.rb,
lib/ribbon/intercom/service/channel/stores/store.rb,
lib/ribbon/intercom/service/channel/stores/mock_store.rb,
lib/ribbon/intercom/service/channel/stores/redis_store.rb

Defined Under Namespace

Classes: Channel, EmptyResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Service

Returns a new instance of Service.



50
51
52
# File 'lib/ribbon/intercom/service.rb', line 50

def initialize(opts={})
  @_opts = opts.dup
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



45
46
47
# File 'lib/ribbon/intercom/service.rb', line 45

def channel
  @channel
end

#envObject (readonly)

Returns the value of attribute env.



48
49
50
# File 'lib/ribbon/intercom/service.rb', line 48

def env
  @env
end

#requestObject (readonly)

Class methods



44
45
46
# File 'lib/ribbon/intercom/service.rb', line 44

def request
  @request
end

#request_packetObject (readonly)

Returns the value of attribute request_packet.



47
48
49
# File 'lib/ribbon/intercom/service.rb', line 47

def request_packet
  @request_packet
end

#subjectObject (readonly)

Returns the value of attribute subject.



46
47
48
# File 'lib/ribbon/intercom/service.rb', line 46

def subject
  @subject
end

Class Method Details

._load_storeObject



36
37
38
39
40
41
# File 'lib/ribbon/intercom/service.rb', line 36

def _load_store
  raise "Store name missing" unless (store_name = @_store_name.to_s)

  store = Utils.classify(store_name) + "Store"
  Intercom::Service::Channel::Stores.const_get(store).new(@_store_params)
end

.call(env) ⇒ Object

The call method is needed here because Rails checks to see if a mounted Rack app can respond_to?(:call). Without it, the Service will not mount



28
29
30
# File 'lib/ribbon/intercom/service.rb', line 28

def call(env)
  instance.call(env)
end

.instanceObject



13
14
15
# File 'lib/ribbon/intercom/service.rb', line 13

def instance
  @instance ||= new(store: _load_store)
end

.method_missing(meth, *args, &block) ⇒ Object



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

def method_missing(meth, *args, &block)
  instance.public_send(meth, *args, &block)
end

.mockObject



17
18
19
# File 'lib/ribbon/intercom/service.rb', line 17

def mock
  Client::MockSDK.new(self)
end

.store(store_name, params = {}) ⇒ Object



21
22
23
24
# File 'lib/ribbon/intercom/service.rb', line 21

def store(store_name, params={})
  @_store_name = store_name
  @_store_params = params
end

Instance Method Details

#call(env) ⇒ Object



84
85
86
# File 'lib/ribbon/intercom/service.rb', line 84

def call(env)
  dup.call!(env)
end

#call!(env) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ribbon/intercom/service.rb', line 88

def call!(env)
  @env = env

  response = catch(:response) {
    begin
      _process_request
    rescue Exception => error
      _respond_with_error!(error)
    end
  }

  response.finish
end

#lookup_channel(token) ⇒ Object



65
66
67
# File 'lib/ribbon/intercom/service.rb', line 65

def lookup_channel(token)
  store.lookup_channel(token)
end

#open_channel(params = {}) ⇒ Object



58
59
60
61
62
63
# File 'lib/ribbon/intercom/service.rb', line 58

def open_channel(params={})
  # Accept either an array of permissions or a string
  store.open_channel(params).tap { |channel|
    channel.may(Utils.method_identifier(self, :rotate_secret))
  }
end

#rotate_secretObject



102
103
104
# File 'lib/ribbon/intercom/service.rb', line 102

def rotate_secret
  channel.rotate_secret!
end

#storeObject



54
55
56
# File 'lib/ribbon/intercom/service.rb', line 54

def store
  @store ||= @_opts[:store] or raise Errors::MissingStoreError
end

#sufficient_permissions?(base, intercom_method) ⇒ Boolean

Check that the channel has sufficient permissions to call the method.

The ‘send` method is forbidden because it breaks the encapsulation guaranteed by intercom (i.e., private methods can’t be called).

In addition to the permissions granted to the channel, all channels have implicit permission to call public methods on basic types.

Returns:

  • (Boolean)


77
78
79
80
81
82
# File 'lib/ribbon/intercom/service.rb', line 77

def sufficient_permissions?(base, intercom_method)
  intercom_method != :send && (
    Utils.basic_type?(base) ||
    channel.may?(Utils.method_identifier(base, intercom_method))
  )
end