blueprint-api-rails 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 67fc79456b463a6eb05d67efd1140268c5b35afc
4
- data.tar.gz: aa3c1baa961ce4b425278719259e35319c17dcbe
3
+ metadata.gz: 25fbdd1b8e7997ef380128605fe7ab82e722a7d0
4
+ data.tar.gz: 8507af124a651f53a6c5c4adf942f4f896b20c36
5
5
  SHA512:
6
- metadata.gz: 6a902708353af1dcd9800f0f170be411bcfd91e99b533e006eedb2fdeaa5f6234584fc50d7a2611d4ec86a8dc8753abb35a8e9a10996bcadb389cd26214eb661
7
- data.tar.gz: 45352ac51e430b72b193c174f1bf057ba3d3519dd2b370d2b35e2e9b72f949dd50c97d6dfd8054d8763b60f130b7dc3afba948eba511250d14fcb9dd8ef69379
6
+ metadata.gz: 6754321e91df4685448af47e3abf5ee7b827af0a4ab883bcbe254d9f4171b9c775319e3aacfcd4d6a375c572f0fc3c93d8b03a772d7127cb512ca6bcb1de06b8
7
+ data.tar.gz: df721189d10ae7d9bc4b9de5dcb1a4a77dd813a00073cdd8266d7707dd296fdd0865003b4b92410d35fa798b57d06b56cca1ee470ed9b98aa1de211af062d279
@@ -1,7 +1,7 @@
1
1
  module Blueprint
2
2
  module Api
3
3
  module Rails
4
- VERSION = '0.1.4'
4
+ VERSION = '0.1.5'
5
5
  end
6
6
  end
7
7
  end
@@ -6,18 +6,14 @@ module Blueprint
6
6
  BLUEPRINT_SERVER = ENV['BLUEPRINT_SERVER'] || 'http://anaxim.io'
7
7
  BLUEPRINT_URL_CONTEXT = '/api/v1/events'
8
8
 
9
+ # TODO deprecate all of the branch stuff
10
+ # TODO limit messages that are lists to a maximum of 10 elements
9
11
 
10
12
  STRUCTURE_NEW = 'start'
11
13
 
12
14
  MESSAGE = 'message'
13
15
  CLASSIFIER_NEW = 'classifier-new'
14
16
 
15
- ACTIVITY_NEW = 'activity-new'
16
- ACTIVITY_PERFORM = 'activity-perform'
17
- ACTIVITY_DECIDE = 'activity-decide'
18
- ACTIVITY_OUTCOME = 'activity-outcome'
19
- ACTIVITY_END = 'activity-end'
20
-
21
17
  # allow users to set the API key
22
18
  def self.set_api_key api_key
23
19
  @api_key = api_key
@@ -25,7 +21,7 @@ module Blueprint
25
21
 
26
22
  # call this to create a structure and get a design context
27
23
  def self.start(structure_id, name)
28
- DesignContext.new(@api_key, structure_id).send STRUCTURE_NEW, { :name => name }
24
+ StructureDesignContext.new(@api_key, structure_id).send STRUCTURE_NEW, { :name => name }
29
25
  end
30
26
 
31
27
 
@@ -35,6 +31,41 @@ module Blueprint
35
31
 
36
32
  class DesignContext
37
33
 
34
+ # utility method for sending JSON payloads to the Anaxim servers - should never be called directly
35
+ def send(event_type, properties = { }, message = { })
36
+
37
+ # construct the base payload
38
+ payload = {
39
+ :header => {
40
+ :structure_id => @structure_id,
41
+ :properties => properties,
42
+ :branch => {
43
+ :name => @branch || 'master'
44
+ },
45
+ :eventType => event_type
46
+ }
47
+ }
48
+
49
+ # merge (append) the properties hash onto the payload (properties vary per event type)
50
+ payload.merge! message
51
+
52
+ @conn.post do |req|
53
+ req.url BLUEPRINT_URL_CONTEXT
54
+ req.headers['Content-Type'] = 'application/json'
55
+ req.headers['X-Api-Key'] = @api_key
56
+ req.body = payload.to_json
57
+ end
58
+
59
+ self
60
+ end
61
+
62
+ end
63
+
64
+ ##
65
+ # Design context for a structure (a group of nodes and links)
66
+ ##
67
+ class StructureDesignContext < DesignContext
68
+
38
69
  def initialize(api_key, structure_id)
39
70
  @api_key = api_key
40
71
  @structure_id = structure_id
@@ -47,6 +78,7 @@ module Blueprint
47
78
  end
48
79
  end
49
80
 
81
+
50
82
  # creates a design context between source and target
51
83
  def link(source, target)
52
84
  LinkDesignContext.new(@api_key, @structure_id, source, target)
@@ -63,75 +95,45 @@ module Blueprint
63
95
  nil
64
96
  end
65
97
 
66
- def activity(component, name, start_state)
67
- self.send ACTIVITY_NEW,
68
- {
69
- :component => component,
70
- :name => name,
71
- :state => start_state
72
- }
73
-
74
- ActivityDesignContext.new(@api_key, @structure_id, component, name)
98
+ def begin(name)
99
+ ActivityDesignContext.new(@api_key, @structure_id, name)
75
100
  end
76
101
 
77
102
  # logs a message between two nodes in the structure
78
- def log(source, target, type, message = { }, extras = { })
79
- self.send MESSAGE,
80
- {
81
- :source => source,
82
- :target => target
83
- },
84
- {
85
- :type => type,
86
- :payload => {
87
- :message => message, # the message that was sent
88
- :extras => extras # extra details about the message that can aid classification
89
- }
90
- }
91
- nil
92
- end
93
-
94
- # utility method for sending JSON payloads to the Anaxim servers - should never be called directly
95
- def send(event_type, properties = { }, message = { })
96
-
97
- # construct the base payload
98
- payload = {
99
- :header => {
100
- :structure_id => @structure_id,
101
- :properties => properties,
102
- :branch => {
103
- :name => @branch || 'master'
104
- },
105
- :eventType => event_type
106
- }
107
- }
108
-
109
- # merge (append) the properties hash onto the payload (properties vary per event type)
110
- payload.merge! message
103
+ def log(message = { }, extras = { }, type = nil, source = nil, target = nil)
104
+ properties = Hash.new.tap do |h|
105
+ h[:source] = source unless source.blank?
106
+ h[:target] = target unless target.blank?
107
+ end
111
108
 
112
- @conn.post do |req|
113
- req.url BLUEPRINT_URL_CONTEXT
114
- req.headers['Content-Type'] = 'application/json'
115
- req.headers['X-Api-Key'] = @api_key
116
- req.body = payload.to_json
109
+ payload = Hash.new.tap do |h|
110
+ h[:type] = type unless type.blank?
111
+ h[:payload] = {
112
+ :message => message,
113
+ :extras => extras
114
+ }
117
115
  end
118
116
 
119
- self
117
+ # send the message
118
+ self.send MESSAGE, properties, payload
119
+
120
+ # return nil so that no further calls can be made to the fluent API
121
+ nil
120
122
  end
121
123
 
122
124
  end
123
125
 
124
126
  ##
125
- # Design context for activities
127
+ # Design context for a group of messages (steps)
126
128
  ##
127
129
 
128
130
  class ActivityDesignContext < DesignContext
129
131
 
130
- def initialize(api_key, structure_id, component_name, activity_name)
132
+ def initialize(api_key, structure_id, name)
131
133
  @api_key = api_key
132
134
  @structure_id = structure_id
133
- @component_name = component_name
134
- @activity_name = activity_name
135
+ @instance_id = SecureRandom.uuid
136
+ @name = name
135
137
  @branch = `git rev-parse --abbrev-ref HEAD 2>&1`.strip! || 'master'
136
138
 
137
139
  # initialise faraday
@@ -141,69 +143,25 @@ module Blueprint
141
143
  end
142
144
  end
143
145
 
144
- def perform(description)
145
- self.send ACTIVITY_PERFORM,
146
- {
147
- :component => @component_name,
148
- :activity => @activity_name,
149
- :description => description
150
- }
151
-
152
- self
146
+ def instance_id
147
+ @instance_id
153
148
  end
154
149
 
155
- def decide(description)
156
- self.send ACTIVITY_DECIDE,
157
- {
158
- :component => @component_name,
159
- :activity => @activity_name,
160
- :description => description
161
- }
162
-
163
- ActivityDecisionDesignContext.new(@api_key, @structure_id, @component_name, @activity_name, description)
150
+ def name
151
+ @name
164
152
  end
165
153
 
166
- def end(description)
167
- self.send ACTIVITY_END,
168
- {
169
- :component => @component_name,
170
- :activity => @activity_name,
171
- :description => description
172
- }
173
-
174
- ActivityEndDesignContext.new(@api_key, @structure_id, @activity_name)
154
+ def step(description)
155
+ StepDesignContext.new(@api_key, @structure_id, self, description)
175
156
  end
176
157
 
177
- def log(source, target, type, message = { }, extras = { })
178
- self.send MESSAGE,
179
- {
180
- :source => source,
181
- :target => target,
182
- :input_for => @activity_name
183
- },
184
- {
185
- :type => type,
186
- :payload => {
187
- :message => message, # the message that was sent
188
- :extras => extras # extra details about the message that can aid classification
189
- }
190
- }
158
+ class StepDesignContext < DesignContext
191
159
 
192
- self
193
- end
194
-
195
- ##
196
- # Design context for activity decisions
197
- ##
198
-
199
- class ActivityDecisionDesignContext < DesignContext
200
-
201
- def initialize(api_key, structure_id, component_name, activity_name, parent = nil)
160
+ def initialize(api_key, structure_id, activity_ctx, step)
202
161
  @api_key = api_key
203
162
  @structure_id = structure_id
204
- @component_name = component_name
205
- @activity_name = activity_name
206
- @parent = parent
163
+ @activity_ctx = activity_ctx
164
+ @step = step
207
165
  @branch = `git rev-parse --abbrev-ref HEAD 2>&1`.strip! || 'master'
208
166
 
209
167
  # initialise faraday
@@ -213,114 +171,31 @@ module Blueprint
213
171
  end
214
172
  end
215
173
 
216
- def yes
217
- OutcomeDesignContext.new @api_key, @structure_id, @component_name, @activity_name, @parent
218
- end
219
-
220
- def no
221
- OutcomeDesignContext.new @api_key, @structure_id, @component_name, @activity_name, @parent, false
222
- end
223
-
224
- ##
225
- # Design context for decision outcomes (only possible after an activity decision)
226
- ##
227
-
228
- class OutcomeDesignContext < DesignContext
229
-
230
- def initialize(api_key, structure_id, component_name, activity_name, parent = nil, positive = true)
231
- @api_key = api_key
232
- @structure_id = structure_id
233
- @component_name = component_name
234
- @activity_name = activity_name
235
- @parent = parent
236
- @positive = positive
237
- @branch = `git rev-parse --abbrev-ref HEAD 2>&1`.strip! || 'master'
238
-
239
- # initialise faraday
240
- @conn = Faraday.new(:url => BLUEPRINT_SERVER) do |faraday|
241
- faraday.response :logger # log requests to STDOUT
242
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
243
- end
244
- end
245
174
 
246
- def perform(description)
247
- self.send ACTIVITY_PERFORM,
248
- {
249
- :component => @component_name,
250
- :activity => @activity_name,
251
- :parent => @parent,
252
- :description => description,
253
- :positive => @positive
254
- }
255
-
256
- self
175
+ def log(message = { }, extras = { }, type = nil, source = nil, target = nil)
176
+ properties = Hash.new.tap do |h|
177
+ h[:instance_id] = @activity_ctx.instance_id
178
+ h[:activity] = @activity_ctx.name
179
+ h[:step] = @step
180
+ h[:source] = source unless source.blank?
181
+ h[:target] = target unless target.blank?
257
182
  end
258
183
 
259
- def decide(description)
260
- self.send ACTIVITY_DECIDE,
261
- {
262
- :component => @component_name,
263
- :activity => @activity_name,
264
- :parent => @parent,
265
- :description => description,
266
- :positive => @positive
267
- }
268
-
269
- ActivityDecisionDesignContext.new(@api_key, @structure_id, @component_name, @activity_name, description)
184
+ payload = Hash.new.tap do |h|
185
+ h[:type] = type unless type.blank?
186
+ h[:payload] = {
187
+ :message => message,
188
+ :extras => extras
189
+ }
270
190
  end
271
191
 
272
- def end(description)
273
- self.send ACTIVITY_END,
274
- {
275
- :component => @component_name,
276
- :activity => @activity_name,
277
- :parent => @parent,
278
- :description => description,
279
- :positive => @positive
280
- }
281
-
282
- ActivityEndDesignContext.new(@api_key, @structure_id, @activity_name)
283
- end
192
+ self.send MESSAGE, properties, payload
284
193
 
194
+ # return the parent activity context so that users can keep making calls to it to add further steps
195
+ @activity_ctx
285
196
  end
286
197
 
287
198
  end
288
- end
289
-
290
- ##
291
- # Design context for the end of an activity
292
- ##
293
- class ActivityEndDesignContext < DesignContext
294
-
295
- def initialize(api_key, structure_id, activity_name)
296
- @api_key = api_key
297
- @structure_id = structure_id
298
- @activity_name = activity_name
299
- @branch = `git rev-parse --abbrev-ref HEAD 2>&1`.strip! || 'master'
300
-
301
- # initialise faraday
302
- @conn = Faraday.new(:url => BLUEPRINT_SERVER) do |faraday|
303
- faraday.response :logger # log requests to STDOUT
304
- faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
305
- end
306
- end
307
-
308
- def log(source, target, type, message = { }, extras = { })
309
- self.send MESSAGE,
310
- {
311
- :source => source,
312
- :target => target,
313
- :output_for => @activity_name
314
- },
315
- {
316
- :type => type,
317
- :payload => {
318
- :message => message, # the message that was sent
319
- :extras => extras # extra details about the message that can aid classification
320
- }
321
- }
322
- self
323
- end
324
199
 
325
200
  end
326
201
 
@@ -341,20 +216,23 @@ module Blueprint
341
216
  end
342
217
 
343
218
  # logs a message between two nodes in the structure
344
- def log(type, message = { }, extras = { })
345
- self.send MESSAGE,
346
- {
347
- :source => @source,
348
- :target => @target
219
+ def log(message = { }, extras = { }, type = nil)
220
+ properties = Hash.new.tap do |h|
221
+ h[:source] = @source unless @source.blank?
222
+ h[:target] = @target unless @target.blank?
223
+ end
349
224
 
350
- },
351
- {
352
- :type => type,
353
- :payload => {
354
- :message => message, # the message that was sent
355
- :extras => extras # extra details about the message that can aid classification
356
- }
357
- }
225
+ payload = Hash.new.tap do |h|
226
+ h[:type] = type unless type.blank?
227
+ h[:payload] = {
228
+ :message => message,
229
+ :extras => extras
230
+ }
231
+ end
232
+
233
+ self.send MESSAGE, properties, payload
234
+
235
+ # return nil so that no further calls can be made to the fluent API
358
236
  nil
359
237
  end
360
238
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprint-api-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - benjii
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-05-24 00:00:00.000000000 Z
11
+ date: 2016-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler