mumukit-bridge 3.5.1 → 3.6.0

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
  SHA256:
3
- metadata.gz: 7f0da5641e73e004830e601984d307dd2a3cef3942db91c575a7ebd0b9b0d1cf
4
- data.tar.gz: 2f95f49d3dddf112c1a0f7569bcbb469b083b97265f688f85226ce81674f4ca8
3
+ metadata.gz: b58fbaed7b8f322059e7482edbdc170f0ef4cacc3a51a8e9e841d5181befde3f
4
+ data.tar.gz: 472387132d4119364c755be1072b1b85acf4a3e8fc413c84e0b291f812e837e3
5
5
  SHA512:
6
- metadata.gz: 1e3f43cb1ccc89c8807b039557d81f1a04e0717831c1d800128a7e40f25573979499b312b0e66761a966525742671863b03e606162a6b9ab9b09b3f80e37a327
7
- data.tar.gz: 6f45f0e107dbace2cb8fcc771b2bd00168621e5e8d368b231631aa5445f2eff99751e2f22634485db5fa64d9a7fb3e7f1bb80bdfc490c493090e0093e834eb9f
6
+ metadata.gz: 16a4a415717dcd553376e095ca10bf252e9b4e8ecdd012745ce7a528c1ab19a3eb9fb9674465a110253a5f561ef8d540c50bc17fb362422170265d18ea6b3812
7
+ data.tar.gz: 20cac161fc03789592bd256489ad54324133c19defc2cf20027f4a8777891a047e849726a8a4f79ec07b734935bae33aa53619864aaa02c29129603f89e7cc2c
@@ -6,11 +6,12 @@ require_relative './runner/boolean'
6
6
  module Mumukit
7
7
  module Bridge
8
8
  class Runner
9
- attr_accessor :test_runner_url
9
+ attr_accessor :test_runner_url, :timeout, :headers
10
10
 
11
- def initialize(test_runner_url, timeout=10)
11
+ def initialize(test_runner_url, timeout=10, headers={})
12
12
  @test_runner_url = test_runner_url
13
13
  @timeout = timeout
14
+ @headers = headers
14
15
  end
15
16
 
16
17
  # Expects a hash
@@ -21,21 +22,26 @@ module Mumukit
21
22
  # status: :passed|:failed|:errored|:aborted|:passed_with_warnings,
22
23
  # expectation_results: [{binding:string, inspection:string, result:symbol}],
23
24
  # feedback: string}
24
- def run_tests!(request)
25
- with_server_response request, 'test' do |response|
25
+ def run_tests!(request, headers={})
26
+ with_server_response 'test', request, headers do |response|
26
27
  response_type = ResponseType.for_response response
27
28
  response_type.parse response
28
29
  end
29
30
  end
30
31
 
31
- def run_query!(request)
32
- with_server_response request, 'query' do |it|
32
+ # Expects a hash
33
+ # {query: string, extra: string, content: string}
34
+ # Returns a hash
35
+ # {result: string,
36
+ # status: :passed|:failed|:errored|:aborted}
37
+ def run_query!(request, headers={})
38
+ with_server_response 'query', request, headers do |it|
33
39
  {status: it['exit'].to_sym, result: it['out']}
34
40
  end
35
41
  end
36
42
 
37
- def run_try!(request)
38
- with_server_response request, 'try' do |it|
43
+ def run_try!(request, headers={})
44
+ with_server_response 'try', request, headers do |it|
39
45
  {
40
46
  status: it['exit'].to_sym,
41
47
  result: it['out'],
@@ -47,8 +53,8 @@ module Mumukit
47
53
  end
48
54
  end
49
55
 
50
- def importable_info
51
- @language_json ||= info.merge('url' => test_runner_url)
56
+ def importable_info(headers={})
57
+ @language_json ||= info(headers).merge('url' => test_runner_url)
52
58
  {
53
59
  name: @language_json['name'],
54
60
  comment_type: @language_json['comment_type'],
@@ -74,37 +80,39 @@ module Mumukit
74
80
  }
75
81
  end
76
82
 
77
- def assets(path, options={})
78
- raw_get_to_server "assets/#{path}", options
83
+ def assets(path, headers={})
84
+ do_get "assets/#{path}", headers
79
85
  end
80
86
 
81
- def info(options={})
82
- JSON.parse raw_get_to_server(:info, options.merge(content_type: :json))
87
+ def info(headers={})
88
+ JSON.parse do_get(:info, json_content_type(headers))
83
89
  end
84
90
 
85
- def with_server_response(request, route, &action)
86
- response = post_to_server(request, route)
91
+ private
92
+
93
+ def with_server_response(route, request, headers, &action)
94
+ response = do_json_post(route, request, headers)
87
95
  action.call(response)
96
+ rescue RestClient::ExceptionWithResponse => e
97
+ {result: "#{e.message}: #{parse_exception_with_response(e)}", status: :aborted}
88
98
  rescue Exception => e
89
99
  {result: e.message, status: :aborted}
90
100
  end
91
101
 
92
- def raw_get_to_server(route, options)
93
- RestClient.get("#{test_runner_url}/#{route}", options)
102
+ def do_get(route, headers)
103
+ RestClient.get "#{test_runner_url}/#{route}", build_headers(headers)
94
104
  end
95
105
 
96
- def post_to_server(request, route)
106
+ def do_json_post(route, request, headers)
97
107
  JSON.parse RestClient::Request.new(
98
108
  method: :post,
99
109
  url: "#{test_runner_url}/#{route}",
100
110
  payload: request.to_json,
101
111
  timeout: @timeout,
102
112
  open_timeout: @timeout,
103
- headers: {content_type: :json}).execute()
113
+ headers: build_headers(json_content_type(headers))).execute
104
114
  end
105
115
 
106
- private
107
-
108
116
  def get_assets_for(kind, content_type)
109
117
  absolutize(@language_json.dig("#{kind}_assets_urls", content_type) || [])
110
118
  end
@@ -112,6 +120,20 @@ module Mumukit
112
120
  def absolutize(urls)
113
121
  urls.map { |url| "#{test_runner_url}/#{url}"}
114
122
  end
123
+
124
+ def build_headers(headers)
125
+ self.headers.merge(headers)
126
+ end
127
+
128
+ def json_content_type(headers)
129
+ headers.merge(content_type: :json)
130
+ end
131
+
132
+ def parse_exception_with_response(error)
133
+ (JSON.parse(error.response)['out'] rescue nil) ||
134
+ error.response.presence ||
135
+ "<no reason>"
136
+ end
115
137
  end
116
138
  end
117
139
  end
@@ -1,5 +1,5 @@
1
1
  module Mumukit
2
2
  module Bridge
3
- VERSION = '3.5.1'
3
+ VERSION = '3.6.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mumukit-bridge
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.1
4
+ version: 3.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franco Leonardo Bulgarelli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-27 00:00:00.000000000 Z
11
+ date: 2018-04-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler