Class: Apitools::Middleware::Spec

Inherits:
Object
  • Object
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 =

should be apitools.json

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

#repoObject (readonly)

Returns the value of attribute repo.



12
13
14
# File 'lib/apitools/middleware/spec.rb', line 12

def repo
  @repo
end

Instance Method Details

#codeObject



27
28
29
# File 'lib/apitools/middleware/spec.rb', line 27

def code
  @code ||= load_code
end

#endpointObject



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? # no manifest at the end
    [pathname, MANIFEST_FILE]
  else
    [pathname.dirname, pathname.basename]
  end
end

#filesObject



43
44
45
# File 'lib/apitools/middleware/spec.rb', line 43

def files
  Array(manifest.files)
end

#manifestObject



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_fileObject



35
36
37
# File 'lib/apitools/middleware/spec.rb', line 35

def manifest_file
  @manifest_file.to_s
end

#pathObject



31
32
33
# File 'lib/apitools/middleware/spec.rb', line 31

def path
  @path.to_s
end

#respond_to_missing?(method) ⇒ Boolean

Returns:

  • (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_hObject



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

Returns:

  • (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

#versionObject



64
65
66
67
68
# File 'lib/apitools/middleware/spec.rb', line 64

def version
  Semantic::Version.new(manifest.version)
rescue ArgumentError
  false
end