Class: Mangadex::Chapter

Inherits:
MangadexObject show all
Includes:
Internal::WithAttributes
Defined in:
lib/mangadex/chapter.rb

Instance Attribute Summary

Attributes included from Internal::WithAttributes

#attributes, #id, #relationships, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MangadexObject

#eq?, #hash, #initialize, #inspect

Constructor Details

This class inherits a constructor from Mangadex::MangadexObject

Class Method Details

.attributes_to_inspectObject



101
102
103
# File 'lib/mangadex/chapter.rb', line 101

def self.attributes_to_inspect
  [:id, :type, :title, :volume, :chapter, :page_count, :publish_at]
end

.delete(id) ⇒ Object



58
59
60
61
62
# File 'lib/mangadex/chapter.rb', line 58

def self.delete(id)
  Mangadex::Internal::Request.delete(
    '/chapter/%{id}' % {id: id},
  )
end

.get(id, **args) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/mangadex/chapter.rb', line 33

def self.get(id, **args)
  Mangadex::Internal::Request.get(
    '/chapter/%{id}' % {id: id},
    Mangadex::Internal::Definition.validate(args, {
      includes: { accepts: [String] },
    }),
  )
end

.list(**args) ⇒ Object



25
26
27
28
29
30
# File 'lib/mangadex/chapter.rb', line 25

def self.list(**args)
  Mangadex::Internal::Request.get(
    '/chapter',
    Mangadex::Internal::Definition.chapter_list(args),
  )
end

.update(id, **args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mangadex/chapter.rb', line 43

def self.update(id, **args)
  Mangadex::Internal::Request.put(
    '/chapter/%{id}' % {id: id},
    payload: Mangadex::Internal::Definition.validate(args, {
      title: { accepts: String },
      volume: { accepts: String },
      chapter: { accepts: String },
      translated_language: { accepts: %r{^[a-zA-Z\-]{2,5}$} },
      groups: { accepts: [String] },
      version: { accepts: Integer, required: true },
    }),
  )
end

Instance Method Details

#as_jsonObject



94
95
96
97
98
99
# File 'lib/mangadex/chapter.rb', line 94

def as_json(*)
  super.merge({
    locale_name: locale_name,
    preview_image_url: preview_image_url,
  })
end

#localeObject



70
71
72
73
74
75
# File 'lib/mangadex/chapter.rb', line 70

def locale
  found_locale = translated_language.split('-').first
  return if found_locale.nil?

  ISO_639.find(found_locale)
end

#locale_nameObject



78
79
80
# File 'lib/mangadex/chapter.rb', line 78

def locale_name
  locale&.english_name
end

#page_countObject



83
84
85
# File 'lib/mangadex/chapter.rb', line 83

def page_count
  Array(data).count
end

#preview_image_urlObject



88
89
90
91
92
# File 'lib/mangadex/chapter.rb', line 88

def preview_image_url
  return if data_saver.empty?

  "https://uploads.mangadex.org/data-saver/#{attributes.hash}/#{data_saver.first}"
end

#titleObject



65
66
67
# File 'lib/mangadex/chapter.rb', line 65

def title
  attributes&.title.presence || chapter.presence && "Chapter #{chapter}" || "N/A"
end