ribbon-intercom 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c3dd1e6bc419cb94651ef42356db8b7b8c6c48a6
4
- data.tar.gz: 8c97e000d932acf6012513ab7339e506b0c6852a
3
+ metadata.gz: f19a9d7871332a1a8abfb1a9b3a376c17b7389a0
4
+ data.tar.gz: b946129b0caf8b2d3a2236ec58d1a177e692fc96
5
5
  SHA512:
6
- metadata.gz: 7642b346c15bf955093543ebee843814cace7994bb3565b95db3e833ac13816d7b1f945abea4f8c4adcbe9e284349b7dab6f171271911e5ccf3990ccf8a2856e
7
- data.tar.gz: 1bde67801438c049bf1877b2c78e8cfb44f25ca82f372370afd48ab0fbc67f7c6e87876e18d9bf1c7c810cacef2b7bb6220b021931d60392b0d74e77f56ef589
6
+ metadata.gz: 0cd606324dd603044b14c318fd0194b7ef571c91214931a519e9b4d3e7c8920d7d82f5220693709dc36dfa6f135967306c569d3dd317fa740f901d70688b318d
7
+ data.tar.gz: 3de7e4b3da0da1aa1619c91a1c8ac9676dc9509f033e5af1192b5038286829cba55a828042006ba399da81d114b77be43d8afc9d974ff66c4a17a98288cc67ab
@@ -33,14 +33,20 @@ module Ribbon::Intercom
33
33
  end.merge(@_method_permissions).dup
34
34
  end
35
35
 
36
+ # The call method is needed here because Rails checks to see if a mounted
37
+ # Rack app can respond_to?(:call). Without it, the Service will not mount
38
+ def call(env)
39
+ instance.call(env)
40
+ end
41
+
36
42
  def method_missing(meth, *args, &block)
37
43
  instance.send(meth, *args, &block)
38
44
  end
39
45
 
40
46
  def _load_store
41
- raise "Store name missing" unless (store_name = @_store_name.to_s)
47
+ raise "Store name missing" unless (store_name = @_store_name.to_s)
42
48
 
43
- store = store_name.split("_").map(&:capitalize).join + "Store"
49
+ store = Utils.classify(store_name) + "Store"
44
50
  Intercom::Service::ChannelStores.const_get(store).new(@_store_params)
45
51
  end
46
52
  end # Class methods
@@ -71,7 +77,8 @@ module Ribbon::Intercom
71
77
  end
72
78
 
73
79
  def method_permissions!(method_name)
74
- method_permissions(method_name) or raise Errors::InvalidMethodError
80
+ method_permissions(method_name) or
81
+ _respond!(404, {}, "Method requested not found.")
75
82
  end
76
83
 
77
84
  def sufficient_permissions?(intercom_method)
@@ -80,35 +87,14 @@ module Ribbon::Intercom
80
87
  end
81
88
 
82
89
  def sufficient_permissions!(intercom_method)
83
- sufficient_permissions?(intercom_method) or raise Errors::MissingPermissionsError
84
- end
85
-
86
- def process_request
87
- @request = Rack::Request.new(env)
88
- return _respond!(405) unless request.put?
89
- return _respond!(401) unless _perform_basic_auth(request.env)
90
-
91
- # Check permissions
92
- intercom_method = env['HTTP_X_INTERCOM_METHOD'].to_sym
93
- sufficient_permissions!(intercom_method)
94
-
95
- # Execute the requested method
96
- args = _decode_args(request.body.read)
97
-
98
- [intercom_method, args]
99
- rescue Errors::UnsafeValueError
100
- _respond!(400, {}, "Arg types are invalid")
101
- rescue Errors::MissingPermissionsError
102
- _respond!(403, {"X-Intercom-Permissions-Missing" => _missing_permissions(intercom_method).to_a.join(',')}, "Permissions missing.")
103
- rescue Errors::InvalidMethodError
104
- _respond!(404, {}, "Method requested not found.")
105
- end
106
-
107
- def call_method(intercom_method, *args)
108
- body = Utils.sanitize(send(intercom_method, *args))
109
- _respond!(200, {}, body)
110
- rescue Errors::UnsafeValueError
111
- _respond!(500, {}, "Return value type is invalid")
90
+ sufficient_permissions?(intercom_method) or
91
+ _respond!(
92
+ 403,
93
+ {
94
+ "X-Intercom-Permissions-Missing" => _missing_permissions(intercom_method).to_a.join(',')
95
+ },
96
+ "Permissions missing."
97
+ )
112
98
  end
113
99
 
114
100
  def call(env)
@@ -119,8 +105,9 @@ module Ribbon::Intercom
119
105
  @env = env
120
106
 
121
107
  response = catch(:response) {
122
- intercom_method, args = process_request
123
- call_method(intercom_method, *args)
108
+ _setup_request
109
+ intercom_method, args = _process_request
110
+ _call_method(intercom_method, *args)
124
111
  }
125
112
 
126
113
  response.finish
@@ -130,7 +117,33 @@ module Ribbon::Intercom
130
117
 
131
118
  private
132
119
 
133
- def _perform_basic_auth(env)
120
+ def _setup_request
121
+ @request = Rack::Request.new(env)
122
+ _respond!(405) unless request.put?
123
+ end
124
+
125
+ def _process_request
126
+ _request_authenticated!
127
+
128
+ # Load the method name and args from the request
129
+ intercom_method = _load_method_name
130
+ args = _load_args
131
+
132
+ [intercom_method, args]
133
+ end
134
+
135
+ def _call_method(intercom_method, *args)
136
+ body = Utils.sanitize(send(intercom_method, *args))
137
+ _respond!(200, {}, body)
138
+ rescue Errors::UnsafeValueError
139
+ _respond!(500, {}, "Return value type is invalid")
140
+ end
141
+
142
+ def _request_authenticated!
143
+ _respond!(401) unless _request_authenticated?
144
+ end
145
+
146
+ def _request_authenticated?
134
147
  auth = Rack::Auth::Basic::Request.new(env)
135
148
 
136
149
  if auth.provided? && auth.basic?
@@ -143,6 +156,19 @@ module Ribbon::Intercom
143
156
  end
144
157
  end
145
158
 
159
+ def _load_method_name
160
+ env['HTTP_X_INTERCOM_METHOD'].to_sym.tap { |intercom_method|
161
+ # Check permissions
162
+ sufficient_permissions!(intercom_method)
163
+ }
164
+ end
165
+
166
+ def _load_args
167
+ _decode_args(request.body.read)
168
+ rescue Errors::UnsafeValueError
169
+ _respond!(400, {}, "Arg types are invalid")
170
+ end
171
+
146
172
  def _missing_permissions(intercom_method)
147
173
  method_permissions(intercom_method) - channel.permissions
148
174
  end
@@ -25,6 +25,10 @@ module Ribbon::Intercom
25
25
  def symbolize_keys(hash)
26
26
  hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
27
27
  end
28
+
29
+ def classify(str)
30
+ str.split("_").map(&:capitalize).join
31
+ end
28
32
  end # Class methods
29
33
  end # Utils
30
34
  end # Ribbon::Intercom
@@ -1,5 +1,5 @@
1
1
  module Ribbon
2
2
  module Intercom
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -12,7 +12,7 @@ namespace :intercom do
12
12
 
13
13
  namespace :service do
14
14
  task :open_channel, [:service_name, :channel_name] => :environment do |t, args|
15
- service = args[:service_name].split("_").map(&:capitalize).join
15
+ service = Intercom::Utils.classify(args[:service_name])
16
16
  channel_name = args[:channel_name]
17
17
 
18
18
  Intercom::const_get(service).open_channel(name: channel_name)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ribbon-intercom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robert Honer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-04-04 00:00:00.000000000 Z
12
+ date: 2015-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack