Class: Rubyhexagon::Post::Flag

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

Initializer for flags

Examples:

Get instance of flag

E621::Post::Flag.new(id: 1) #=> E621::Post::Flag instance

Parameters:

  • flag (Hash)

    flag data

Author:

  • Maxine Michalski

Since:

  • 2.0.0



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

def initialize(flag)
  unless flag.is_a?(Hash)
    raise ArgumentError, "#{flag.class} is not a Hash"
  end
  unless flag.key?(:id)
    raise ArgumentError, 'Not all required keys available!'
  end
  flag.each do |k, v|
    if %i[id reason].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 == :created_at
      @created_at = Time.at(v)
    elsif k == :post_id
      @post = E621::Post.new(id: v)
    elsif k == :user_id
      @user = E621::User.new(id: v)
    end
  end
end

Instance Attribute Details

#created_atTime (readonly)

Creation time of flag

Examples:

Get flag creation time

flag.created_at #=> Time

Returns:

  • (Time)

    creation time of flag

Since:

  • 2.0.0



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

def created_at
  @created_at
end

#idInteger (readonly)

Flag ID

Examples:

Get flag ID

flag.id #=> Integer

Returns:

  • (Integer)

    id of flag

Since:

  • 2.0.0



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

def id
  @id
end

#post621::Post (readonly)

Flag post

Examples:

Get post that this flag references

flag.post #=> E621::Post

Returns:

  • (621::Post)

    Post this flag is addressed to

Since:

  • 2.0.0



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

def post
  @post
end

#reasonString (readonly)

Flagging reason

Examples:

Get reason for flagging

flag.reason #=> String

Returns:

  • (String)

    reason for flag

Since:

  • 2.0.0



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

def reason
  @reason
end

#userE621::User (readonly)

User that initiated flagging

Examples:

Get user that flagged post

flag.user #=> E621::User

Returns:

Since:

  • 2.0.0



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

def user
  @user
end

Class Method Details

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

Fetch a list of flags

Examples:

Get a list of posts

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

Returns:

  • (Array<E621::Flag>)

See Also:

Author:

  • Maxine Michalski

Since:

  • 2.0.0



35
36
37
38
39
40
41
42
# File 'lib/rubyhexagon/api/post/flag.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_flag_history, :index, query).map do |note|
    new(note)
  end
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for flags

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



96
97
98
# File 'lib/rubyhexagon/post/flag.rb', line 96

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

#to_hashHash

Turn object into a hash representation of itself

Examples:

Turn a Flag into a Hash

Note.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/post/flag.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