Class: Metanorma::Release::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/metanorma/release/index.rb

Defined Under Namespace

Classes: SchemaError

Constant Summary collapse

SCHEMA_VERSION =
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publications:, parameters:, generated_at: nil) ⇒ Index

Returns a new instance of Index.



15
16
17
18
19
20
# File 'lib/metanorma/release/index.rb', line 15

def initialize(publications:, parameters:, generated_at: nil)
  @publications = publications.freeze
  @parameters = parameters
  @generated_at = generated_at || Time.now.utc.iso8601
  freeze
end

Instance Attribute Details

#generated_atObject (readonly)

Returns the value of attribute generated_at.



13
14
15
# File 'lib/metanorma/release/index.rb', line 13

def generated_at
  @generated_at
end

#parametersObject (readonly)

Returns the value of attribute parameters.



13
14
15
# File 'lib/metanorma/release/index.rb', line 13

def parameters
  @parameters
end

#publicationsObject (readonly)

Returns the value of attribute publications.



13
14
15
# File 'lib/metanorma/release/index.rb', line 13

def publications
  @publications
end

Class Method Details

.from_documents(publications, parameters:) ⇒ Object



62
63
64
# File 'lib/metanorma/release/index.rb', line 62

def self.from_documents(publications, parameters:)
  new(publications: publications, parameters: parameters)
end

.from_json(json_string) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/metanorma/release/index.rb', line 66

def self.from_json(json_string)
  data = JSON.parse(json_string)
  validate!(data)
  publications = (data["documents"] || []).map do |d|
    publication_from_h(d)
  end
  new(
    publications: publications,
    parameters: {
      organizations: data.dig("parameters", "organizations") || [],
      channels: data.dig("parameters", "channels") || [],
      topic: data.dig("parameters", "topic"),
      repo_count: data.dig("parameters", "repoCount") || 0,
    },
    generated_at: data["generatedAt"],
  )
end

.publication_from_h(hash) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/metanorma/release/index.rb', line 103

def self.publication_from_h(hash)
  files = (hash["files"] || []).map do |f|
    PublicationFile.new(format: f["format"], name: f["name"],
                        path: f["path"])
  end
  source = if hash["source"]
             PublicationSource.new(
               owner: hash["source"]["owner"],
               repo: hash["source"]["repo"],
               tag: hash["source"]["tag"],
               url: hash["source"]["releaseUrl"],
               date: hash["source"]["releaseDate"],
             )
           end
  Publication.new(
    identifier: hash["identifier"] || hash["id"],
    slug: hash["id"],
    title: hash["title"],
    edition: hash["edition"],
    stage: hash["stage"],
    doctype: hash.fetch("doctype", ""),
    revdate: hash["revdate"],
    channels: hash["channels"] || [],
    files: files,
    source: source,
  )
end

.validate!(data) ⇒ Object

Raises:



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/metanorma/release/index.rb', line 84

def self.validate!(data)
  raise SchemaError, "Missing 'version' field" unless data.key?("version")
  unless data["version"] == SCHEMA_VERSION
    raise SchemaError,
          "Unsupported schema version: #{data['version']}. Expected #{SCHEMA_VERSION}"
  end
  unless data.key?("documents")
    raise SchemaError,
          "Missing 'documents' field"
  end

  data["documents"].each do |doc|
    unless doc.key?("id")
      raise SchemaError,
            "Document missing required field 'id'"
    end
  end
end

Instance Method Details

#channelsObject



22
23
24
# File 'lib/metanorma/release/index.rb', line 22

def channels
  @publications.flat_map(&:channels).uniq.sort
end

#empty?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/metanorma/release/index.rb', line 30

def empty?
  @publications.empty?
end

#publication_countObject



26
27
28
# File 'lib/metanorma/release/index.rb', line 26

def publication_count
  @publications.length
end

#to_hObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/metanorma/release/index.rb', line 34

def to_h
  {
    "version" => SCHEMA_VERSION,
    "generatedAt" => @generated_at,
    "parameters" => {
      "organizations" => @parameters[:organizations] || [],
      "channels" => @parameters[:channels] || [],
      "topic" => @parameters[:topic],
      "repoCount" => @parameters[:repo_count] || 0,
    },
    "summary" => {
      "repoCount" => @parameters[:repo_count] || 0,
      "documentCount" => publication_count,
      "channelsFound" => channels,
    },
    "documents" => @publications.map(&:to_h),
  }
end

#to_json(*_args) ⇒ Object



53
54
55
# File 'lib/metanorma/release/index.rb', line 53

def to_json(*_args)
  JSON.generate(to_h)
end

#write(path) ⇒ Object



57
58
59
60
# File 'lib/metanorma/release/index.rb', line 57

def write(path)
  FileUtils.mkdir_p(File.dirname(path))
  File.write(path, to_json)
end