webspicy 0.9.1 → 0.10.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
  SHA1:
3
- metadata.gz: 2a450b7c37c1dd58a30dcb2bc1e9317509f1d5e5
4
- data.tar.gz: 6f471bbb4aea22f92564c22f720e6eaf50ff5937
3
+ metadata.gz: 297f6b02ba888bdb7d04063e70ab34a53d572c64
4
+ data.tar.gz: 551b6a4a2e66adfc8274f949943ede49e8d926a9
5
5
  SHA512:
6
- metadata.gz: 4bf135af59ee9a2b9dff46a9849b2b2539bf612728d88c719835ed5012c101ae8f0b8b8c1ba4d00010f8db93c42aeb48200e9ccf2b086d133d492bb43bfb7ac1
7
- data.tar.gz: 65d79ba0f514277d7bc52e81b3cd92d311708fc58bd570476ab03cc7e4d78ef51e1780a06accc59fbeb686ac17d88fac10e80f6347100ff0f38c23b46d2ee33d
6
+ metadata.gz: 910823668c7fda4b201c4508ac7c4ac4b43f026970e40c43569fff53756ca4bd2f6d6241e4ad07285aafce3a89628f414ba7f5997b6c6411745200b2791ad0b9
7
+ data.tar.gz: 2a8769e405659e083e37cdcd0b28125ea3eccfd1783653822e5ce2dc477f737cb72e2ca22d1bc4fcaa960e4e03606773d334997e2941bfcaafab55adae29a22c
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ../..
3
3
  specs:
4
- webspicy (0.9.0)
4
+ webspicy (0.10.0)
5
5
  finitio (>= 0.5.2)
6
6
  http (~> 2)
7
7
  path (~> 1.3)
@@ -16,7 +16,7 @@ GEM
16
16
  public_suffix (>= 2.0.2, < 4.0)
17
17
  citrus (3.0.2)
18
18
  diff-lcs (1.3)
19
- domain_name (0.5.20170404)
19
+ domain_name (0.5.20180417)
20
20
  unf (>= 0.0.5, < 1.0.0)
21
21
  finitio (0.5.2)
22
22
  citrus (>= 2.4, < 4.0)
@@ -119,7 +119,22 @@ def loaded_body
119
119
  when /csv/
120
120
  csv = ::CSV.new(request.body.read, :headers => true, :header_converters => :symbol)
121
121
  csv.map {|row| row.to_hash }
122
+ when /multipart\/form-data/
123
+ file_body params[:file]
122
124
  else
123
- halt [415,{},["Unsupported content type"]]
125
+ halt [415,{},["Unsupported content type: `#{ctype}`"]]
124
126
  end
125
- end
127
+ end
128
+
129
+ def file_body(file)
130
+ ctype = Path(file[:filename] || file["filename"]).extname
131
+ ctype = (file[:type] || file["type"]) if ctype.nil? or ctype.empty?
132
+ case ctype
133
+ when /csv/
134
+ str = file[:tempfile].read
135
+ csv = ::CSV.new(str, :headers => true, :header_converters => :symbol)
136
+ csv.map {|row| row.to_hash }
137
+ else
138
+ halt [415,{},["Unsupported content type: `#{ctype}`\n#{file.inspect}"]]
139
+ end
140
+ end
@@ -0,0 +1,37 @@
1
+ ---
2
+ name: |-
3
+ Todo
4
+
5
+ url: |-
6
+ /todo/
7
+
8
+ services:
9
+ - method: |-
10
+ POST
11
+
12
+ description: |-
13
+ Creates new todo items through a CSV file upload
14
+
15
+ preconditions: |-
16
+
17
+ input_schema: |-
18
+ .
19
+
20
+ output_schema: |-
21
+ { count: Integer }
22
+
23
+ error_schema: |-
24
+ ErrorSchema
25
+
26
+ examples:
27
+
28
+ - description: |-
29
+ when requested with a valid CSV file
30
+ file_upload:
31
+ path: ./todos.csv
32
+ content_type: "text/csv"
33
+ expected:
34
+ content_type: application/json
35
+ status: 201
36
+ assert:
37
+ - "pathFD('', count: 3)"
@@ -0,0 +1,4 @@
1
+ id,description
2
+ 10,Write documentation
3
+ 11,Create website
4
+ 12,Add smart tools
data/lib/webspicy.rb CHANGED
@@ -14,6 +14,7 @@ module Webspicy
14
14
  ###
15
15
 
16
16
  require 'webspicy/configuration'
17
+ require 'webspicy/file_upload'
17
18
  require 'webspicy/scope'
18
19
  require 'webspicy/client'
19
20
  require 'webspicy/client/http_client'
@@ -46,7 +47,9 @@ module Webspicy
46
47
 
47
48
  def resource(raw, file = nil, scope = default_scope)
48
49
  with_scope(scope) do
49
- FORMALDOC["Resource"].dress(raw)
50
+ r = FORMALDOC["Resource"].dress(raw)
51
+ r.located_at!(file) if file
52
+ r
50
53
  end
51
54
  end
52
55
  module_function :resource
@@ -23,7 +23,8 @@ module Webspicy
23
23
 
24
24
  # Instantiate the parameters
25
25
  headers = test_case.headers
26
- params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
26
+ params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
27
+ body = test_case.body || test_case.located_file_upload
27
28
 
28
29
  # Instantiate the url and strip parameters
29
30
  url, params = resource.instantiate_url(params)
@@ -32,7 +33,7 @@ module Webspicy
32
33
  url = scope.to_real_url(url, test_case)
33
34
 
34
35
  # Invoke the service now
35
- api.public_send(service.method.to_s.downcase.to_sym, url, params, headers, test_case.body)
36
+ api.public_send(service.method.to_s.downcase.to_sym, url, params, headers, body)
36
37
 
37
38
  # Return the result
38
39
  Resource::Service::Invocation.new(service, test_case, api.last_response, self)
@@ -70,12 +71,18 @@ module Webspicy
70
71
  url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
71
72
 
72
73
  headers ||= {}
73
- headers['Content-Type'] ||= 'application/json'
74
74
 
75
- if body
76
- @last_response = HTTP[headers].post(url, body: body)
77
- else
75
+ case body
76
+ when NilClass
77
+ headers['Content-Type'] ||= 'application/json'
78
78
  @last_response = HTTP[headers].post(url, body: params.to_json)
79
+ when FileUpload
80
+ @last_response = HTTP[headers].post(url, form: {
81
+ body.param_name.to_sym => HTTP::FormData::File.new(body.path, content_type: body.content_type)
82
+ })
83
+ else
84
+ headers['Content-Type'] ||= 'application/json'
85
+ @last_response = HTTP[headers].post(url, body: body)
79
86
  end
80
87
 
81
88
  Webspicy.debug("Headers: #{@last_response.headers.to_hash}")
@@ -16,8 +16,8 @@ module Webspicy
16
16
 
17
17
  # Instantiate the parameters
18
18
  headers = test_case.headers.dup
19
- params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
20
- body = test_case.body
19
+ params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
20
+ body = test_case.body || test_case.located_file_upload
21
21
 
22
22
  # Instantiate the url and strip parameters
23
23
  url, params = resource.instantiate_url(params)
@@ -94,12 +94,17 @@ module Webspicy
94
94
 
95
95
  url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
96
96
 
97
- Webspicy.info("POST #{url} -- #{params.inspect} -- #{headers.inspect}")
98
-
99
- if body
100
- handler.post(url, body)
101
- else
97
+ case body
98
+ when NilClass
99
+ Webspicy.info("POST #{url} -- #{params.inspect} -- #{headers.inspect}")
102
100
  handler.post(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
101
+ when FileUpload
102
+ file = Rack::Test::UploadedFile.new(body.path, body.content_type)
103
+ Webspicy.info("POST #{url} -- #{params.inspect} -- #{body}")
104
+ handler.post(url, body.param_name.to_sym => file)
105
+ else
106
+ Webspicy.info("POST #{url} -- #{params.inspect} -- #{body.inspect[0..25]}")
107
+ handler.post(url, body)
103
108
  end
104
109
  @last_response = handler.last_response
105
110
 
@@ -0,0 +1,35 @@
1
+ module Webspicy
2
+ class FileUpload
3
+
4
+ def initialize(raw)
5
+ @path = raw[:path]
6
+ @content_type = raw[:content_type]
7
+ @param_name = raw[:param_name] || "file"
8
+ end
9
+
10
+ attr_reader :path, :content_type, :param_name
11
+
12
+ def self.info(raw)
13
+ new(raw)
14
+ end
15
+
16
+ def locate(resource)
17
+ FileUpload.new({
18
+ path: resource.locate(path),
19
+ content_type: content_type
20
+ })
21
+ end
22
+
23
+ def to_info
24
+ { path: path.to_s,
25
+ content_type: content_type,
26
+ param_name: param_name }
27
+ end
28
+
29
+ def to_s
30
+ "FileUpload(#{to_info})"
31
+ end
32
+ alias :inspect :to_s
33
+
34
+ end # class FileUpload
35
+ end # module Webspicy
@@ -6,6 +6,13 @@ Schema =
6
6
  \( s | ::Webspicy.schema(s) )
7
7
  \( s | raise "Unsupported" )
8
8
 
9
+ FileUpload =
10
+ .Webspicy::FileUpload <info> {
11
+ path : String
12
+ content_type : String
13
+ param_name :? String
14
+ }
15
+
9
16
  Resource =
10
17
  .Webspicy::Resource <info> {
11
18
  name: String
@@ -31,9 +38,10 @@ TestCase =
31
38
  .Webspicy::Resource::Service::TestCase <info> {
32
39
  description : String
33
40
  dress_params :? Boolean
34
- params : Params
41
+ params :? Params
35
42
  headers :? .Hash
36
43
  body :? String
44
+ file_upload :? FileUpload
37
45
  seeds :? String
38
46
  requester :? String
39
47
  expected: {
@@ -1,8 +1,9 @@
1
1
  module Webspicy
2
2
  class Resource
3
3
 
4
- def initialize(raw)
4
+ def initialize(raw, location = nil)
5
5
  @raw = raw
6
+ @location = location
6
7
  bind_services
7
8
  end
8
9
 
@@ -10,6 +11,16 @@ module Webspicy
10
11
  new(raw)
11
12
  end
12
13
 
14
+ def located_at!(location)
15
+ @location = Path(location)
16
+ end
17
+
18
+ def locate(relative_path)
19
+ file = @location.parent/relative_path
20
+ raise "File not found: #{file}" unless file.exists?
21
+ file
22
+ end
23
+
13
24
  def url
14
25
  @raw[:url]
15
26
  end
@@ -34,13 +34,21 @@ module Webspicy
34
34
  alias :dress_params? :dress_params
35
35
 
36
36
  def params
37
- @raw[:params]
37
+ @raw[:params] || {}
38
38
  end
39
39
 
40
40
  def body
41
41
  @raw[:body]
42
42
  end
43
43
 
44
+ def file_upload
45
+ @raw[:file_upload]
46
+ end
47
+
48
+ def located_file_upload
49
+ file_upload ? file_upload.locate(resource) : nil
50
+ end
51
+
44
52
  def expected_content_type
45
53
  @raw[:expected].fetch(:content_type){ 'application/json' }
46
54
  end
@@ -1,8 +1,8 @@
1
1
  module Webspicy
2
2
  module Version
3
3
  MAJOR = 0
4
- MINOR = 9
5
- TINY = 1
4
+ MINOR = 10
5
+ TINY = 0
6
6
  end
7
7
  VERSION = "#{Version::MAJOR}.#{Version::MINOR}.#{Version::TINY}"
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webspicy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bernard Lambeau
@@ -146,13 +146,16 @@ files:
146
146
  - examples/restful/webspicy/todo/options.yml
147
147
  - examples/restful/webspicy/todo/patchTodo.yml
148
148
  - examples/restful/webspicy/todo/postCsv.yml
149
+ - examples/restful/webspicy/todo/postFile.yml
149
150
  - examples/restful/webspicy/todo/postTodos.yml
151
+ - examples/restful/webspicy/todo/todos.csv
150
152
  - lib/webspicy.rb
151
153
  - lib/webspicy/checker.rb
152
154
  - lib/webspicy/client.rb
153
155
  - lib/webspicy/client/http_client.rb
154
156
  - lib/webspicy/client/rack_test_client.rb
155
157
  - lib/webspicy/configuration.rb
158
+ - lib/webspicy/file_upload.rb
156
159
  - lib/webspicy/formaldoc.fio
157
160
  - lib/webspicy/resource.rb
158
161
  - lib/webspicy/resource/service.rb