Class: Apitools::Middleware::Spec
- Inherits:
-
Object
- Object
- Apitools::Middleware::Spec
show all
- Defined in:
- lib/apitools/middleware/spec.rb
Constant Summary
collapse
- SPEC_ATTRIBUTES =
I[path code endpoint description name author github_user version]
- MANIFEST_FILE =
Pathname('apitools.json')
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(repo, path) ⇒ Spec
Returns a new instance of Spec.
14
15
16
17
|
# File 'lib/apitools/middleware/spec.rb', line 14
def initialize(repo, path)
@repo = repo
@path, @manifest_file = extract_path_and_manifest(path)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/apitools/middleware/spec.rb', line 47
def method_missing(method, *)
hash = manifest.to_h
if hash && hash.has_key?(method)
hash.fetch(method)
end
end
|
Instance Attribute Details
#repo ⇒ Object
Returns the value of attribute repo.
12
13
14
|
# File 'lib/apitools/middleware/spec.rb', line 12
def repo
@repo
end
|
Instance Method Details
#code ⇒ Object
27
28
29
|
# File 'lib/apitools/middleware/spec.rb', line 27
def code
@code ||= load_code
end
|
#endpoint ⇒ Object
39
40
41
|
# File 'lib/apitools/middleware/spec.rb', line 39
def endpoint
Array(endpoints).first
end
|
#extract_path_and_manifest(path) ⇒ Object
79
80
81
82
83
84
85
86
87
|
# File 'lib/apitools/middleware/spec.rb', line 79
def extract_path_and_manifest(path)
pathname = Pathname(path)
if pathname.extname.empty?
[pathname, MANIFEST_FILE]
else
[pathname.dirname, pathname.basename]
end
end
|
#files ⇒ Object
43
44
45
|
# File 'lib/apitools/middleware/spec.rb', line 43
def files
Array(manifest.files)
end
|
#manifest ⇒ Object
23
24
25
|
# File 'lib/apitools/middleware/spec.rb', line 23
def manifest
@manifest ||= Manifest.parse(load_manifest)
end
|
#manifest=(json) ⇒ Object
19
20
21
|
# File 'lib/apitools/middleware/spec.rb', line 19
def manifest=(json)
@manifest = Manifest.parse(json)
end
|
#manifest_file ⇒ Object
35
36
37
|
# File 'lib/apitools/middleware/spec.rb', line 35
def manifest_file
@manifest_file.to_s
end
|
#path ⇒ Object
31
32
33
|
# File 'lib/apitools/middleware/spec.rb', line 31
def path
@path.to_s
end
|
#respond_to_missing?(method) ⇒ Boolean
55
56
57
58
|
# File 'lib/apitools/middleware/spec.rb', line 55
def respond_to_missing?(method, *)
hash = manifest.to_h
hash && hash.has_key?(method) || super
end
|
#to_h ⇒ Object
60
61
62
|
# File 'lib/apitools/middleware/spec.rb', line 60
def to_h
SPEC_ATTRIBUTES.map{|attr| [attr, public_send(attr)] }.to_h
end
|
#valid? ⇒ Boolean
70
71
72
73
74
75
76
77
|
# File 'lib/apitools/middleware/spec.rb', line 70
def valid?
required_attributes = [name, path, author, endpoint, version, description]
return unless required_attributes.all?
lua_files = Array(files).map { |file| content(file) }
return if lua_files.empty?
lua_files.none?{ |file| !file || file.nil? || file.empty? }
end
|
#version ⇒ Object
64
65
66
67
68
|
# File 'lib/apitools/middleware/spec.rb', line 64
def version
Semantic::Version.new(manifest.version)
rescue ArgumentError
false
end
|