Class: Rubyhexagon::Tag::Alias

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

Overview

A class to interact with the e621 web interface.

Author:

  • Maxine Michalski

Since:

  • 2.0.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ali) ⇒ Object

Initializer for Alias

Examples:

Get alias isntance

E621::Tag::Alias.new(id: 1, name: 'test', alias_id: 1,
 pending: false) #=> E621::Tag::Alias instance

Parameters:

  • ali (Hash)

    alias data, fetched from e621

Options Hash (ali):

  • :id (Integer)

    alias ID

  • :name (String)

    alias for tag

  • :alias_id (Integer)

    ID of tag to alias

  • :pending (TrueClass|FalseClass)

    statud

Raises:

  • InvalidIDError if alias ID is not valid

  • ArgumentError hash not valid

Author:

  • Maxine Michalski

Since:

  • 2.0.0



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubyhexagon/tag/alias.rb', line 61

def initialize(ali)
  raise ArgumentError, "#{ali.class} is not a Hash" unless ali.is_a?(Hash)
  if ali.keys != %i[id name alias_id pending]
    mis = %i[id name alias_id pending] - ali.keys
    raise ArgumentError, "Missing key#{mis.count > 1 ? 's' : ''}: #{mis}"
  end
  ali.each do |k, v|
    if %i[id name pending].include?(k)
      if k == :id && !(v.is_a?(Integer) && v.positive?)
        raise InvalidIDError, "Invalid id: #{v}"
      end
      instance_variable_set("@#{k}".to_sym, v)
    elsif k == :alias_id
      @alias_to = E621::Tag.new(id: v)
    end
  end
end

Instance Attribute Details

#alias_toE621::Tag (readonly)

Tag this alias is for

Examples:

Get aliased tag

alias.alias_to #=> E621::Tag

Returns:

  • (E621::Tag)

    tag this alias is aliased to

Since:

  • 2.0.0



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

def alias_to
  @alias_to
end

#idInteger (readonly)

Alias ID

Examples:

Get alias ID

alias.id #=> Integer

Returns:

  • (Integer)

    id of alias

Since:

  • 2.0.0



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

def id
  @id
end

#nameString (readonly)

Alias name

Examples:

Get alias name

alias.name #=> String

Returns:

  • (String)

    name of alias

Since:

  • 2.0.0



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

def name
  @name
end

Class Method Details

.list(query) ⇒ Array[E621::Tag::Alias]

Fetch tag aliases

Examples:

Get a list of posts

E621::Tag::Alias.list(implied_to: 'feline',
page: 1) #=> Array<E621::Tag::Alias>

Parameters:

  • query (Hash)

    Parameter for query

Returns:

  • (Array[E621::Tag::Alias])

Raises:

  • (ArgumentError)

See Also:

Author:

  • Maxine Michalski

Since:

  • 2.0.0



38
39
40
41
42
43
# File 'lib/rubyhexagon/api/tag/alias.rb', line 38

def self.list(query)
  raise ArgumentError, 'A Hash is required' unless query.is_a?(Hash)
  E621::API.fetch(:tag_alias, :index, query).map do |tag|
    new(tag)
  end
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for Types, to give a more meaningful comparison

Examples:

Compare two tags, that are unequal

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

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



85
86
87
# File 'lib/rubyhexagon/tag/alias.rb', line 85

def ==(other)
  other.is_a?(Alias) && @id == other.id
end

#pending?TrueClass, FalseClass

Check if this alias is pending confirmation

Examples:

Is alias pending?

alias.pending? #=> true or false

Returns:

  • (TrueClass)

    tag alias is pending

  • (FalseClass)

    tag alias is not pending

Author:

  • Maxine Michalski

Since:

  • 2.0.0



96
97
98
# File 'lib/rubyhexagon/tag/alias.rb', line 96

def pending?
  @pending
end

#to_hashHash

Turn object into a hash representation of itself

Examples:

Turn an Alias into a Hash

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

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



107
108
109
110
111
112
113
# File 'lib/rubyhexagon/tag/alias.rb', line 107

def to_hash
  hash = {}
  instance_variables.each do |i|
    hash.store(i.to_s.sub(/^@/, '').to_sym, instance_variable_get(i))
  end
  hash
end