reason_me_this 0.1.0 → 0.3.0

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.
@@ -3,8 +3,13 @@ require 'reason_me_this/values'
3
3
  require 'reason_me_this/answer'
4
4
  require 'reason_me_this/errors'
5
5
  require 'reason_me_this/explanation'
6
+ require 'reason_me_this/webhook'
6
7
  require 'reason_me_this/connection'
8
+ require 'reason_me_this/blueprint'
9
+ require 'reason_me_this/engines'
7
10
  require 'reason_me_this/configuration'
11
+ require 'reason_me_this/saving'
12
+ require 'reason_me_this/recipe'
8
13
  require 'reason_me_this/model'
9
14
  require 'forwardable'
10
15
 
@@ -12,7 +17,7 @@ module ReasonMeThis
12
17
  class << self
13
18
  extend Forwardable
14
19
 
15
- def_delegators :default, :run, :explain, :push, :publish, :definitions, :definition
20
+ def_delegators :default, :run, :explain, :schema, :prove, :push, :publish, :definitions, :definition
16
21
 
17
22
  def configure
18
23
  yield configuration
@@ -25,22 +30,50 @@ module ReasonMeThis
25
30
  end
26
31
 
27
32
  class Client
28
- def initialize(url: nil, api_key: nil, connection: Connection.new(url, api_key))
29
- @connection = connection
33
+ # What a service that does not yet mark refused: request says of a day, or of a version and a day, it was
34
+ # asked for (Day::Invalid in its API).
35
+ REQUEST_PROBLEM = /\A(?:(?:on|effective_from) should be a date written YYYY-MM-DD, not |give version or on, not both\z)/
36
+
37
+ # With engine, blueprints run in this process from the blueprints folder, and nothing is sent to a service.
38
+ def initialize(url: nil, api_key: nil, engine: nil, blueprints: nil, connection: nil)
39
+ raise Error.new('give a url or an engine, not both') if url && engine
40
+
41
+ @connection = connection || (engine ? Engines.connection(engine, blueprints:) : Connection.new(url, api_key))
30
42
  end
31
43
 
32
- def run(name, facts, calculation: nil, version: nil, trace: false)
33
- name, pinned = name.to_s.split('@', 2)
34
- version ||= pinned&.match?(/\A\d+\z/) ? Integer(pinned) : pinned
35
- answer = request(:post, at(name, :run), {facts:, calculation:, version:, trace: (true if trace)}.compact)
36
- trace ? Explanation.new(outputs: Answer.new(answer[:outputs]), steps: answer[:steps]) : Answer.new(answer)
44
+ def run(name, facts, calculation: nil, version: nil, on: nil, trace: false)
45
+ name, version = pinned(name, version)
46
+ answer = request(:post, at(name, :run), {facts:, calculation:, version:, on:, trace: (true if trace)}.compact)
47
+ return Answer.new(answer) unless trace
48
+
49
+ Explanation.new(outputs: Answer.new(answer[:outputs]), steps: answer[:steps], version: answer[:version],
50
+ events: Array(answer[:events]).map { |event| Event.read(event) })
37
51
  end
38
52
 
39
- def explain(name, facts, calculation: nil, version: nil) = run(name, facts, calculation:, version:, trace: true)
53
+ def explain(name, facts, calculation: nil, version: nil, on: nil) = run(name, facts, calculation:, version:, on:, trace: true)
54
+
55
+ # Checks a blueprint written in Ruby and runs the examples it gives, saving nothing.
56
+ def prove(blueprint)
57
+ report = request(:post, '/check', {yaml: blueprint.yaml, cases: blueprint.cases_yaml})
58
+ raise Rejected.new(report) if report[:problems].any? || report[:failed].to_i.positive?
59
+
60
+ report
61
+ end
40
62
 
41
63
  def push(name, yaml:, cases: '') = request(:put, at(name), {yaml:, cases:}, invalid: Rejected)
42
64
 
43
- def publish(name) = request(:post, at(name, :publish), invalid: Rejected)[:version]
65
+ # live: false leaves the version that is live now live, so the new one waits.
66
+ def publish(name, effective_from: nil, live: nil)
67
+ body = {effective_from:, live:}.compact
68
+ request(:post, at(name, :publish), (body unless body.empty?), invalid: Rejected)
69
+ end
70
+
71
+ # The JSON Schemas of a calculation's inputs and outputs, with string keys and values as sent, for a validator.
72
+ def schema(name, calculation: nil, version: nil, on: nil)
73
+ name, version = pinned(name, version)
74
+ query = URI.encode_www_form(Values.write({calculation:, version:, on:}.compact))
75
+ request(:get, [at(name, :schema), *(query unless query.empty?)].join('?'), invalid: InvalidRequest, read: false)
76
+ end
44
77
 
45
78
  def definitions = request(:get, '/definitions')[:definitions]
46
79
 
@@ -50,21 +83,35 @@ module ReasonMeThis
50
83
 
51
84
  def at(name, *action) = ['/definitions', name, *action].join('/')
52
85
 
53
- def request(method, path, body = nil, invalid: InvalidFacts)
86
+ def pinned(name, version)
87
+ name, pin = name.to_s.split('@', 2)
88
+ [name, version || (pin&.match?(/\A\d+\z/) ? Integer(pin) : pin)]
89
+ end
90
+
91
+ def request(method, path, body = nil, invalid: InvalidFacts, read: true)
54
92
  status, answer = @connection.request(method, path, body)
55
- answer = Values.read(answer)
56
- status.between?(200, 299) ? answer : raise(refusal(status, answer, invalid))
93
+ return (read ? Values.read(answer) : answer) if status.between?(200, 299)
94
+
95
+ raise(refusal(status, Values.read(answer), invalid))
57
96
  end
58
97
 
59
98
  def refusal(status, answer, invalid)
60
99
  case [status, answer]
61
100
  in [401, _] then Unauthorized.new('the service refused the API key')
62
101
  in [404, {error:}] then NotFound.new(error)
63
- in [422, {problems:}] then invalid.new(answer)
102
+ in [410, {error:}] then Gone.new(error)
103
+ in [422, {problems:}] then refused(answer, invalid).new(answer)
64
104
  in [422, {error:}] then StepFailed.new(error)
65
105
  in [503, {error:}] then Timeout.new(error)
66
106
  else Error.new(["the service answered HTTP #{status}", answer[:error]].compact.join(': '))
67
107
  end
68
108
  end
109
+
110
+ def refused(answer, invalid)
111
+ return InvalidRequest if answer[:refused] == 'request'
112
+
113
+ problems = answer[:problems]
114
+ problems.one? && problems.first.match?(REQUEST_PROBLEM) ? InvalidRequest : invalid
115
+ end
69
116
  end
70
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reason_me_this
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rhys Murray
@@ -38,7 +38,8 @@ dependencies:
38
38
  - !ruby/object:Gem::Version
39
39
  version: '2.10'
40
40
  description: 'The client for reason-me-this.com: runs, explains, pushes and publishes
41
- blueprints over HTTP, keeping numbers exact.'
41
+ blueprints over HTTP, keeping numbers exact. With a separately licensed engine installed,
42
+ it runs blueprint files in process instead.'
42
43
  executables:
43
44
  - rmt
44
45
  extensions: []
@@ -47,17 +48,31 @@ files:
47
48
  - LICENSE.txt
48
49
  - README.md
49
50
  - exe/rmt
51
+ - lib/generators/reason_me_this/receipts_generator.rb
52
+ - lib/generators/reason_me_this/templates/create_reason_me_this_receipts.rb.tt
50
53
  - lib/reason_me_this.rb
51
54
  - lib/reason_me_this/answer.rb
55
+ - lib/reason_me_this/blueprint.rb
56
+ - lib/reason_me_this/blueprint/calculation.rb
57
+ - lib/reason_me_this/blueprint/fields.rb
58
+ - lib/reason_me_this/blueprint/ruby.rb
59
+ - lib/reason_me_this/blueprint/rules.rb
60
+ - lib/reason_me_this/blueprint/step.rb
61
+ - lib/reason_me_this/blueprint/yaml.rb
52
62
  - lib/reason_me_this/cli.rb
53
63
  - lib/reason_me_this/cli/output.rb
54
64
  - lib/reason_me_this/configuration.rb
55
65
  - lib/reason_me_this/connection.rb
66
+ - lib/reason_me_this/engines.rb
56
67
  - lib/reason_me_this/errors.rb
57
68
  - lib/reason_me_this/explanation.rb
58
69
  - lib/reason_me_this/model.rb
70
+ - lib/reason_me_this/receipt.rb
71
+ - lib/reason_me_this/recipe.rb
72
+ - lib/reason_me_this/saving.rb
59
73
  - lib/reason_me_this/values.rb
60
74
  - lib/reason_me_this/version.rb
75
+ - lib/reason_me_this/webhook.rb
61
76
  homepage: https://reason-me-this.com
62
77
  licenses:
63
78
  - MIT