Class: Rubyhexagon::Post::TagItem

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyhexagon/post/tag_item.rb,
lib/rubyhexagon/api/post/tag_item.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(tag_item) ⇒ Object

Initializer for tag change items

Examples:

Get instance of flag

E621::Post::TagItem.new(id: 1, created_at: 1
post_id: 1, tags: '', source: '') #=> E621::Post::TagItem instance

Parameters:

  • tag_item (Hash)

    tag item data

Author:

  • Maxine Michalski

Since:

  • 2.0.0



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rubyhexagon/post/tag_item.rb', line 67

def initialize(tag_item)
  unless tag_item.is_a?(Hash)
    raise ArgumentError, "#{tag_item.class} is not a Hash"
  end
  unless (miss = %i[id created_at post_id tags source] -
      tag_item.keys).empty?
    raise ArgumentError, 'Not all required keys available! '\
      "Missing: #{miss}"
  end
  id = tag_item[:id]
  unless id.is_a?(Integer) && id.positive?
    raise InvalidIDError, "ID out of range: #{id}"
  end
  @id = id
  @created_at = Time.at(tag_item[:created_at])
  @post = E621::Post.new(id: tag_item[:post_id])
  @tags = tag_item[:tags].split(' ').map { |t| E621::Tag.new(name: t) }
  @sources = tag_item[:source].split($INPUT_RECORD_SEPARATOR)
end

Instance Attribute Details

#created_atTime (readonly)

Creation time of tag item

Examples:

Get creation time of tag item

tag_item.created_at #=> Time

Returns:

  • (Time)

    creation time of tag item

Since:

  • 2.0.0



37
38
39
# File 'lib/rubyhexagon/post/tag_item.rb', line 37

def created_at
  @created_at
end

#idInteger (readonly)

Tag item ID

Examples:

Get tag item ID

tag_item.id #=> Integer

Returns:

  • (Integer)

    id of tag item

Since:

  • 2.0.0



31
32
33
# File 'lib/rubyhexagon/post/tag_item.rb', line 31

def id
  @id
end

#postE621::Post (readonly)

Post for this tag item

Examples:

Get post this tag item belongs to

tag_item.post #=> E621::Post

Returns:

Since:

  • 2.0.0



43
44
45
# File 'lib/rubyhexagon/post/tag_item.rb', line 43

def post
  @post
end

#sourcesArray<String> (readonly)

New sources for post in tag item

Examples:

Get sources of tag item

tag_item.sources #=> Array<String>

Returns:

  • (Array<String>)

    changed sources

Since:

  • 2.0.0



55
56
57
# File 'lib/rubyhexagon/post/tag_item.rb', line 55

def sources
  @sources
end

#tagsArray<E621::Tags> (readonly)

Changed tags inside tag item

Examples:

Get changed tags of tag item

tag_item.tags #=> Array<E621::Tag>

Returns:

  • (Array<E621::Tags>)

    tags this tag item contains

Since:

  • 2.0.0



49
50
51
# File 'lib/rubyhexagon/post/tag_item.rb', line 49

def tags
  @tags
end

Class Method Details

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

Fetch a list of tag items

Examples:

Get a list of posts

E621::Post::TagItem.list(page: 1) #=> Array<E621::Post::TagItem>

Returns:

  • (Array<E621::TagItem>)

See Also:

Author:

  • Maxine Michalski

Since:

  • 2.0.0



35
36
37
38
39
40
41
42
# File 'lib/rubyhexagon/api/post/tag_item.rb', line 35

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

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for tag change items

Examples:

Compare two notes, that are unequal

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

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



94
95
96
# File 'lib/rubyhexagon/post/tag_item.rb', line 94

def ==(other)
  other.is_a?(E621::Post::TagItem) && @id == other.id
end

#to_hashHash

Turn object into a hash representation of itself

Examples:

Turn a TagItem into a Hash

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

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



105
106
107
108
109
110
111
# File 'lib/rubyhexagon/post/tag_item.rb', line 105

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