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

Examples:

Get new tag instance by ID

Tag.new(id: 1) #=> E621::Tag

Get new tag instance by name

E621::Tag.new(name: 'feline') #=> E621::Tag

Parameters:

  • tag (Hash)

    tag data

Raises:

  • InvalidIDError|ArgumentError

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

#countInteger (readonly)

Number of posts, this tag is assigned to

Examples:

Get post count of tag

tag.count #=> Integer

Returns:

  • (Integer)

    number of posts that have this tag assigned to them

Since:

  • 1.0.0



42
43
44
# File 'lib/rubyhexagon/tag.rb', line 42

def count
  @count
end

#idInteger (readonly)

Tag ID

Examples:

Get Tag ID

tag.id #=> Integer

Returns:

  • (Integer)

    id of tag

Since:

  • 1.0.0



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

def id
  @id
end

#nameString (readonly)

Tag name

Examples:

Get tag name

tag.name #=> String

Returns:

  • (String)

    name of tag

Since:

  • 1.0.0



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

def name
  @name
end

#typeE621::Tag::Type (readonly)

Tag type

Examples:

Get tag type

tag.type $=> E621::Tag::Type

Returns:

  • (E621::Tag::Type)

    type of this tag

Since:

  • 1.0.0



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

Examples:

Get list of tags

E621::Tag.list(name: 'feline') #=> Array<E621::Tag>

Parameters:

  • query (Hash)

    query data

Returns:

Raises:

  • (ArgumentError)

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

Examples:

Get tag information

E621::Tag.show(id: 1) #=> E621::Tag

Parameters:

  • tag (Hash|E621::Tag)

    a hash or tag object

Returns:

See Also:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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

Examples:

Compare two tags, that are unequal

Tag.new(id: 1) == Tag.new(id: 2) #=> false

Parameters:

  • other (Object)

    object that should be compared

Returns:

  • (TrueClass, FalseClass)

    test result of comparison

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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

#showE621::Tag

Return a filled Tag object from a tag

Examples:

Gwt tag information

tag.show #=> E621::Tag

Returns:

Author:

  • Maxine Michalski

Since:

  • 1.4.0



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_hashHash

Turn object into a hash representation of itself

Examples:

Turn a User into a Hash

Tag.new(id: 1).to_hash #=> { id: 1 }

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 1.0.0



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