Class: Rubyhexagon::Tag
- Inherits:
-
Object
- Object
- Rubyhexagon::Tag
- Defined in:
- lib/rubyhexagon/tag.rb,
lib/rubyhexagon/api/tag.rb,
lib/rubyhexagon/tag/type.rb,
lib/rubyhexagon/tag/alias.rb,
lib/rubyhexagon/api/tag/alias.rb,
lib/rubyhexagon/tag/implication.rb,
lib/rubyhexagon/api/tag/implication.rb
Overview
A class to interact with the e621 web interface.
Defined Under Namespace
Classes: Alias, Implication, Type
Instance Attribute Summary collapse
-
#count ⇒ Integer
readonly
Number of posts, this tag is assigned to.
-
#id ⇒ Integer
readonly
Tag ID.
-
#name ⇒ String
readonly
Tag name.
-
#type ⇒ E621::Tag::Type
readonly
Tag type.
Class Method Summary collapse
-
.list(query) ⇒ Array<E621::Tag>
Fetch a list of tags.
-
.show(tag) ⇒ E621::Tag
Fetch tag data.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
Comparison operator for Tag, to override the default one.
-
#initialize(tag) ⇒ Object
constructor
Initializer for Tag.
-
#show ⇒ E621::Tag
Return a filled Tag object from a tag.
-
#to_hash ⇒ Hash
Turn object into a hash representation of itself.
Constructor Details
#initialize(tag) ⇒ Object
Initializer for Tag
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rubyhexagon/tag.rb', line 63 def initialize(tag) raise ArgumentError, "#{tag.class} is not a Hash" unless tag.is_a?(Hash) if tag[:id].nil? && tag[:name].nil? raise ArgumentError, 'At least :id or :name must be given!' end tag[:type_locked] = false if tag[:type_locked].nil? tag.each do |k, v| if %i[id name count].include?(k) if k == :id && !(v.is_a?(Integer) && v.positive?) raise InvalidIDError, "ID out of range: #{v}" end instance_variable_set("@#{k}".to_sym, v) elsif k == :type @type = E621::Tag::Type.new(id: v, locked: tag[:type_locked]) end end end |
Instance Attribute Details
#count ⇒ Integer (readonly)
Number of posts, this tag is assigned to
42 43 44 |
# File 'lib/rubyhexagon/tag.rb', line 42 def count @count end |
#id ⇒ Integer (readonly)
Tag ID
30 31 32 |
# File 'lib/rubyhexagon/tag.rb', line 30 def id @id end |
#name ⇒ String (readonly)
Tag name
36 37 38 |
# File 'lib/rubyhexagon/tag.rb', line 36 def name @name end |
#type ⇒ E621::Tag::Type (readonly)
Tag type
48 49 50 |
# File 'lib/rubyhexagon/tag.rb', line 48 def type @type end |
Class Method Details
.list(query) ⇒ Array<E621::Tag>
Fetch a list of tags
53 54 55 56 57 58 |
# File 'lib/rubyhexagon/api/tag.rb', line 53 def self.list(query) raise ArgumentError, 'A Hash is required' unless query.is_a?(Hash) E621::API.fetch(:tag, :index, query).map do |tag| new(tag) end end |
.show(tag) ⇒ E621::Tag
Fetch tag data
35 36 37 38 39 40 41 42 |
# File 'lib/rubyhexagon/api/tag.rb', line 35 def self.show(tag) unless (tag.is_a?(Hash) && tag[:id].is_a?(Integer)) || tag.is_a?(E621::Tag) raise ArgumentError, 'A Hash or tag data object are required' end id = tag.is_a?(Hash) ? tag[:id] : tag.id new(E621::API.fetch(:tag, :show, id: id)) end |
Instance Method Details
#==(other) ⇒ TrueClass, FalseClass
Comparison operator for Tag, to override the default one
104 105 106 107 |
# File 'lib/rubyhexagon/tag.rb', line 104 def ==(other) return false unless other.is_a?(Tag) @id == other.id && @name == other.name && @type == other.type end |
#show ⇒ E621::Tag
Return a filled Tag object from a tag
66 67 68 69 |
# File 'lib/rubyhexagon/api/tag.rb', line 66 def show E621::Tag.new(E621::API.fetch(:tag, :show, @id.nil? ? { name: @name } : { id: @id })) end |
#to_hash ⇒ Hash
Turn object into a hash representation of itself
88 89 90 91 92 93 94 |
# File 'lib/rubyhexagon/tag.rb', line 88 def to_hash hash = {} instance_variables.each do |i| hash.store(i.to_s.sub(/^@/, '').to_sym, instance_variable_get(i)) end hash end |