Class: Rubyhexagon::Tag

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

Overview

Class to hold tag information.

Author:

  • Maxine Michalski

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

Author:

  • Maxine Michalski

Since:

  • 1.0.0



46
47
48
49
50
51
# File 'lib/rubyhexagon/tag.rb', line 46

def initialize(id)
  unless id.is_a?(Integer) && id.positive?
    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



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

#typeType (readonly)

Returns type of this tag.

Returns:

  • (Type)

    type of this tag

Since:

  • 1.0.0



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

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

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

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

Author:

  • Maxine Michalski

Since:

  • 1.0.0



78
79
80
81
82
# File 'lib/rubyhexagon/tag.rb', line 78

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)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

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