Skip to content

Commit

Permalink
Keep moving web specific behavior & classes under Web.
Browse files Browse the repository at this point in the history
  • Loading branch information
blambeau committed Mar 14, 2021
1 parent 122005e commit 7bcd521
Show file tree
Hide file tree
Showing 18 changed files with 255 additions and 210 deletions.
2 changes: 1 addition & 1 deletion lib/webspicy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def fatal(*args, &bl)
HttpClient = Web::HttpClient
RackTestClient = Web::RackTestClient
Resource = Specification
FileUpload = Specification::FileUpload
FileUpload = Web::Specification::FileUpload
Scope = Configuration::Scope
Checker = Tester::FileChecker

Expand Down
2 changes: 1 addition & 1 deletion lib/webspicy/configuration/scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def expand_example(service, example)
return example unless service.default_example
h1 = service.default_example.to_info
h2 = example.to_info
ex = Specification::TestCase.new(merge_maps(h1, h2))
ex = config.factory.test_case(merge_maps(h1, h2), self)
ex.bind(service, example.counterexample?)
end

Expand Down
55 changes: 0 additions & 55 deletions lib/webspicy/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ def initialize(raw, location = nil)
attr_accessor :config
attr_reader :location

def self.info(raw)
new(raw)
end

def self.singleservice(raw)
converted = {
name: raw[:name] || "Unamed specification",
url: raw[:url],
services: [
Webspicy::Web.service(raw.reject{|k| k==:url or k==:name }, Webspicy.current_scope)
]
}
info(converted)
end

def located_at!(location)
@location = Path(location)
end
Expand All @@ -39,51 +24,12 @@ def name
@raw[:name]
end

def url
@raw[:url]
end

def url_pattern
@url_pattern ||= Mustermann.new(url, type: :template)
end

def services
@raw[:services] || []
end

def url_placeholders
url.scan(/\{([a-zA-Z]+(\.[a-zA-Z]+)*)\}/).map{|x| x.first }
end

def instantiate_url(params)
url, rest = self.url, params.dup
url_placeholders.each do |placeholder|
value, rest = extract_placeholder_value(rest, placeholder)
url = url.gsub("{#{placeholder}}", value.to_s)
end
[ url, rest ]
end

def to_singleservice
raise NotImplementedError
end

private

def extract_placeholder_value(params, placeholder, split = nil)
return extract_placeholder_value(params, placeholder, placeholder.split(".")) unless split

key = [ split.first, split.first.to_sym ].find{|k| params.has_key?(k) }
raise "Missing URL parameter `#{placeholder}`" unless key

if split.size == 1
[ params[key], params.dup.delete_if{|k| k == key } ]
else
value, rest = extract_placeholder_value(params[key], placeholder, split[1..-1])
[ value, params.merge(key => rest) ]
end
end

def bind_services
services.each do |s|
s.specification = self
Expand All @@ -99,4 +45,3 @@ def bind_services
require_relative 'specification/err'
require_relative 'specification/oldies'
require_relative 'specification/test_case'
require_relative 'specification/file_upload'
37 changes: 0 additions & 37 deletions lib/webspicy/specification/file_upload.rb

This file was deleted.

4 changes: 0 additions & 4 deletions lib/webspicy/specification/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ def config
specification.config
end

def method
@raw[:method]
end

def description
@raw[:description]
end
Expand Down
49 changes: 0 additions & 49 deletions lib/webspicy/specification/test_case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ def seeds
@raw[:seeds]
end

def headers
@raw[:headers] ||= {}
end

def metadata
@raw[:metadata] ||= {}
end
Expand All @@ -55,47 +51,10 @@ def tags
@raw[:tags] ||= []
end

def dress_params
@raw.fetch(:dress_params){ true }
end
alias :dress_params? :dress_params

def params
@raw[:params] || {}
end

def body
@raw[:body]
end

def file_upload
@raw[:file_upload]
end

def located_file_upload
file_upload ? file_upload.locate(specification) : nil
end

def expected
@raw[:expected] || {}
end

def expected_content_type
expected[:content_type]
end

def expected_status
expected[:status]
end

def is_expected_status?(status)
expected_status === status
end

def has_expected_status?
not expected[:status].nil?
end

def expected_error
expected[:error]
end
Expand All @@ -104,14 +63,6 @@ def has_expected_error?
!expected_error.nil?
end

def expected_headers
expected[:headers] || {}
end

def has_expected_headers?
!expected_headers.empty?
end

def assert
@raw[:assert] || []
end
Expand Down
2 changes: 2 additions & 0 deletions lib/webspicy/web.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Webspicy
module Web

require_relative 'web/specification'

FORMALDOC = Finitio.system(Path.dir/("web/formaldoc.fio"))

def specification(raw, file = nil, scope = Webspicy.default_scope)
Expand Down
8 changes: 4 additions & 4 deletions lib/webspicy/web/formaldoc.fio
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ Method =
String( s | s =~ /^(GET|POST|POST_FORM|PUT|DELETE|PATCH|PUT|OPTIONS)$/ )

FileUpload =
.Webspicy::Specification::FileUpload <info> {
.Webspicy::Web::Specification::FileUpload <info> {
path : String
content_type : String
param_name :? String
}

Specification = .Webspicy::Specification
Specification = .Webspicy::Web::Specification
<info> {
name: String
url: String
Expand All @@ -35,7 +35,7 @@ Specification = .Webspicy::Specification
}

Service =
.Webspicy::Specification::Service <info> {
.Webspicy::Web::Specification::Service <info> {
method : Method
description : String
preconditions :? [String]|String
Expand All @@ -51,7 +51,7 @@ Service =
}

TestCase =
.Webspicy::Specification::TestCase <info> {
.Webspicy::Web::Specification::TestCase <info> {
description :? String
dress_params :? Boolean
params :? Params
Expand Down
68 changes: 68 additions & 0 deletions lib/webspicy/web/specification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
module Webspicy
module Web
class Specification < Webspicy::Specification

class << self
def info(raw)
new(raw)
end

def singleservice(raw)
converted = {
name: raw[:name] || "Unamed specification",
url: raw[:url],
services: [
Webspicy::Web.service(raw.reject{|k| k==:url or k==:name }, Webspicy.current_scope)
]
}
info(converted)
end
end

def url
@raw[:url]
end

def url_pattern
@url_pattern ||= Mustermann.new(url, type: :template)
end

def url_placeholders
url.scan(/\{([a-zA-Z]+(\.[a-zA-Z]+)*)\}/).map{|x| x.first }
end

def instantiate_url(params)
url, rest = self.url, params.dup
url_placeholders.each do |placeholder|
value, rest = extract_placeholder_value(rest, placeholder)
url = url.gsub("{#{placeholder}}", value.to_s)
end
[ url, rest ]
end

def to_singleservice
raise NotImplementedError
end

private

def extract_placeholder_value(params, placeholder, split = nil)
return extract_placeholder_value(params, placeholder, placeholder.split(".")) unless split

key = [ split.first, split.first.to_sym ].find{|k| params.has_key?(k) }
raise "Missing URL parameter `#{placeholder}`" unless key

if split.size == 1
[ params[key], params.dup.delete_if{|k| k == key } ]
else
value, rest = extract_placeholder_value(params[key], placeholder, split[1..-1])
[ value, params.merge(key => rest) ]
end
end

end # class Specification
end # module Web
end # module Webspicy
require_relative 'specification/service'
require_relative 'specification/test_case'
require_relative 'specification/file_upload'
39 changes: 39 additions & 0 deletions lib/webspicy/web/specification/file_upload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Webspicy
module Web
class Specification
class FileUpload

def initialize(raw)
@path = raw[:path]
@content_type = raw[:content_type]
@param_name = raw[:param_name] || "file"
end

attr_reader :path, :content_type, :param_name

def self.info(raw)
new(raw)
end

def locate(specification)
FileUpload.new({
path: specification.locate(path),
content_type: content_type
})
end

def to_info
{ path: path.to_s,
content_type: content_type,
param_name: param_name }
end

def to_s
"FileUpload(#{to_info})"
end
alias :inspect :to_s

end # class FileUpload
end # class Specification
end # module Web
end # module Webspicy
13 changes: 13 additions & 0 deletions lib/webspicy/web/specification/service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Webspicy
module Web
class Specification
class Service < Webspicy::Specification::Service

def method
@raw[:method]
end

end # class Service
end # class Specification
end # module Web
end # module Webspicy
Loading

0 comments on commit 7bcd521

Please sign in to comment.