Class: Rubyhexagon::Tag

Inherits:
Object
  • Object
show all
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.

Author:

  • Maxine Michalski

Since:

  • 1.4.0

Defined Under Namespace

Classes: Alias, Implication, Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag) ⇒ Object

Initializer for Tag.

Parameters:

  • tag (Hash)

    tag data

Raises:

  • InvalidIDError|ArgumentError

Author:

  • Maxine Michalski

Since:

  • 1.0.0



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rubyhexagon/tag.rb', line 46

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

#countInteger (readonly)

Returns number of posts that have this tag assigned to them.

Returns:

  • (Integer)

    number of posts that have this tag assigned to them

Since:

  • 1.0.0



32
33
34
# File 'lib/rubyhexagon/tag.rb', line 32

def count
  @count
end

#idInteger (readonly)

Returns id of tag.

Returns:

  • (Integer)

    id of tag

Since:

  • 1.0.0



26
27
28
# File 'lib/rubyhexagon/tag.rb', line 26

def id
  @id
end

#nameString (readonly)

Returns name of tag.

Returns:

  • (String)

    name of tag

Since:

  • 1.0.0



29
30
31
# File 'lib/rubyhexagon/tag.rb', line 29

def name
  @name
end

#typeTag::Type (readonly)

Returns type of this tag.

Returns:

Since:

  • 1.0.0



35
36
37
# File 'lib/rubyhexagon/tag.rb', line 35

def type
  @type
end

Class Method Details

.list(query) ⇒ Array<E621::Tag>

Fetch a list of tags

Parameters:

  • query (Hash)

    query data

Returns:

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 1.4.0



48
49
50
51
52
53
# File 'lib/rubyhexagon/api/tag.rb', line 48

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

Parameters:

  • tag (Hash|E621::Tag)

    a hash or tag object

Returns:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



32
33
34
35
36
37
38
39
# File 'lib/rubyhexagon/api/tag.rb', line 32

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.

Parameters:

  • other (Object)

    object that should be compared

Returns:

  • (TrueClass, FalseClass)

    test result of comparison

Author:

  • Maxine Michalski

Since:

  • 1.0.0



71
72
73
74
# File 'lib/rubyhexagon/tag.rb', line 71

def ==(other)
  return false unless other.is_a?(Tag)
  @id == other.id && @name == other.name && @type == other.type
end

#showE621::Tag

Return a filled Tag object from a tag.

Returns:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



60
61
62
63
# File 'lib/rubyhexagon/api/tag.rb', line 60

def show
  E621::Tag.new(E621::API.fetch(:tag, :show,
                                @id.nil? ? { name: @name } : { id: @id }))
end