Class: E621::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/tag.rb

Overview

Class to hold tag information.

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Object

Initializer for Tag.

Parameters:

  • id (Integer)

    tag data, fetched from e621

Raises:

  • InvalidIDError

Since:

  • 1.0.0



44
45
46
47
48
49
# File 'lib/rubyhexagon/tag.rb', line 44

def initialize(id)
  unless id.is_a?(Integer) && id > 0
    raise InvalidIDError, "ID out of range: #{id}"
  end
  @id = id
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



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

def count
  @count
end

#idInteger (readonly)

Returns id of tag.

Returns:

  • (Integer)

    id of tag

Since:

  • 1.0.0



24
25
26
# File 'lib/rubyhexagon/tag.rb', line 24

def id
  @id
end

#nameString (readonly)

Returns name of tag.

Returns:

  • (String)

    name of tag

Since:

  • 1.0.0



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

def name
  @name
end

#typeType (readonly)

Returns type of this tag.

Returns:

  • (Type)

    type of this tag

Since:

  • 1.0.0



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

def type
  @type
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Note:

This method looks for same id and same name only.

Note:

If there is only a an ID set with one argument, then only compare IDs

Comparison operator for Tag, to override the default one.

Parameters:

  • other (Object)

    object that should be compared

Returns:

  • (TrueClass, FalseClass)

    test result of comparison

Since:

  • 1.0.0



92
93
94
95
96
97
98
99
# File 'lib/rubyhexagon/tag.rb', line 92

def ==(other)
  return false unless other.is_a?(Tag)
  if @name.nil? || other.name.nil?
    @id == other.id
  else
    @id == other.id && @name == other.name
  end
end

#show(tag = nil) ⇒ Tag

Note:

This makes an additional API call if no argument is given

Method to return a properly filled Tag object.

Parameters:

  • tag (Hash) (defaults to: nil)

    optional parameter, if information are ready

Returns:

  • (Tag)

    Tag filled with all information

Since:

  • 1.0.0



76
77
78
79
80
# File 'lib/rubyhexagon/tag.rb', line 76

def show(tag = nil)
  new_tag = Tag.new(@id)
  new_tag.show!(tag)
  new_tag
end

#show!(tag = nil) ⇒ NilClass

Bang method to fill tag data.

Parameters:

  • tag (Hash) (defaults to: nil)

    optional parameter with API call information

Returns:

  • (NilClass)

Since:

  • 1.0.0



60
61
62
63
64
65
66
# File 'lib/rubyhexagon/tag.rb', line 60

def show!(tag = nil)
  tag = API.new.fetch('tag', 'show', id: @id) if tag.nil?
  @name = tag[:name]
  @count = tag[:count].to_i
  @type = (Type.new(tag[:type], tag[:type_locked]) unless tag[:type].nil?)
  nil
end