Class: Icarus::Mod::Tools::Baseinfo

Inherits:
Object
  • Object
show all
Defined in:
lib/icarus/mod/tools/baseinfo.rb

Overview

Base class for Modinfo and Toolinfo

Direct Known Subclasses

Modinfo, Toolinfo

Constant Summary collapse

HASHKEYS =
%i[name author version compatibility description files imageURL readmeURL].freeze
GITHUB_BLOB_RAW_URL_PATTERN =

Match github.com URLs with /blob/ or /raw/ and convert to raw.githubusercontent.com

%r{
  (https?)://(?:www\.)?github\.com/  # Protocol and GitHub domain (capture group 1)
  ([^/]+)/                            # owner (capture group 2)
  ([^/]+)/                            # repo (capture group 3)
  (?:blob|raw)/                       # /blob/ or /raw/ to remove
  (.+)                                # branch and file path (capture group 4)
}x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, id: nil, created: nil, updated: nil) ⇒ Baseinfo

Returns a new instance of Baseinfo.



22
23
24
25
26
27
28
29
30
31
# File 'lib/icarus/mod/tools/baseinfo.rb', line 22

def initialize(data, id: nil, created: nil, updated: nil)
  @id = id
  @created_at = created
  @updated_at = updated
  @errors = []
  @warnings = []
  @validated = false

  read(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args, &_block) ⇒ Object



117
118
119
# File 'lib/icarus/mod/tools/baseinfo.rb', line 117

def method_missing(method_name, *_args, &_block)
  @data[method_name.to_sym] if @data.key?(method_name.to_sym)
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



9
10
11
# File 'lib/icarus/mod/tools/baseinfo.rb', line 9

def created_at
  @created_at
end

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/icarus/mod/tools/baseinfo.rb', line 8

def data
  @data
end

#idObject

Returns the value of attribute id.



9
10
11
# File 'lib/icarus/mod/tools/baseinfo.rb', line 9

def id
  @id
end

#updated_atObject

Returns the value of attribute updated_at.



9
10
11
# File 'lib/icarus/mod/tools/baseinfo.rb', line 9

def updated_at
  @updated_at
end

Instance Method Details

#author_idObject



33
34
35
# File 'lib/icarus/mod/tools/baseinfo.rb', line 33

def author_id
  author.downcase.gsub(/\s+/, "_")
end

#errorsObject



43
44
45
46
# File 'lib/icarus/mod/tools/baseinfo.rb', line 43

def errors
  validate
  @errors.compact.uniq
end

#errors?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/icarus/mod/tools/baseinfo.rb', line 48

def errors?
  errors.any?
end

#file_typesObject



109
110
111
# File 'lib/icarus/mod/tools/baseinfo.rb', line 109

def file_types
  files&.keys || []
end

#file_urlsObject



113
114
115
# File 'lib/icarus/mod/tools/baseinfo.rb', line 113

def file_urls
  files&.values || []
end

#read(data) ⇒ Object



37
38
39
40
41
# File 'lib/icarus/mod/tools/baseinfo.rb', line 37

def read(data)
  @data = data.is_a?(String) ? JSON.parse(data, symbolize_names: true) : data
  normalize_github_urls_in_data
  @data
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/icarus/mod/tools/baseinfo.rb', line 121

def respond_to_missing?(method_name, include_private = false)
  @data.key?(method_name.to_sym) || super
end

#statusObject



61
62
63
64
65
66
67
68
# File 'lib/icarus/mod/tools/baseinfo.rb', line 61

def status
  validate

  {
    errors:,
    warnings:
  }
end

#to_hObject



78
79
80
81
82
83
84
85
# File 'lib/icarus/mod/tools/baseinfo.rb', line 78

def to_h
  db_hash = HASHKEYS.each_with_object({}) { |key, hash| hash[key] = @data[key] }

  db_hash[:version] = "1.0" if version.nil?
  db_hash[:files] = db_hash[:files]&.reject { |_, url| url.nil? || url.to_s.strip.empty? }

  db_hash
end

#to_json(*args) ⇒ Object



74
75
76
# File 'lib/icarus/mod/tools/baseinfo.rb', line 74

def to_json(*args)
  JSON.generate(@data, *args)
end

#uniq_nameObject



70
71
72
# File 'lib/icarus/mod/tools/baseinfo.rb', line 70

def uniq_name
  "#{author.strip}/#{name.strip}"
end

#valid?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/icarus/mod/tools/baseinfo.rb', line 103

def valid?
  validate

  !errors?
end

#validateObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/icarus/mod/tools/baseinfo.rb', line 87

def validate
  return true if @validated

  validate_version

  %w[name author description].each do |key|
    @errors << "#{key.capitalize} cannot be blank" unless validate_string(@data[key.to_sym])
  end

  %w[imageURL readmeURL].each do |key|
    @errors << "Invalid URL #{key.capitalize}: #{@data[key.to_sym] || "blank"}" unless validate_url(@data[key.to_sym])
  end

  @validated = true
end

#warningsObject



52
53
54
55
# File 'lib/icarus/mod/tools/baseinfo.rb', line 52

def warnings
  validate
  @warnings.compact.uniq
end

#warnings?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/icarus/mod/tools/baseinfo.rb', line 57

def warnings?
  warnings.any?
end