Class: Rubyhexagon::Post::Note

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

Initializer for Note

Examples:

Get new note instance

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

Parameters:

  • note (Hash)

    note information in hash form

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rubyhexagon/post/note.rb', line 113

def initialize(note)
  unless note.is_a?(Hash)
    raise ArgumentError, "#{note.class} is not a Hash"
  end
  raise ArgumentError, 'An id is required' if note[:id].nil?
  note.each do |k, v|
    if i[id x y width height is_active version body].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 i[created_at updated_at].include?(k)
      instance_variable_set("@#{k}".to_sym, Time.at(v[:s]))
    elsif k == :post_id
      @post = E621::Post.new(id: v)
    elsif k == :creator_id
      @creator = E621::User.new(id: v)
    end
  end
  @point = [@x, @y]
  @rect = [@x, @y, @width, @height]
end

Instance Attribute Details

#bodyString (readonly)

Text of note

Examples:

Get text of this note

note.body #=> String

Returns:

  • (String)

    text content of note

Since:

  • 2.0.0



97
98
99
# File 'lib/rubyhexagon/post/note.rb', line 97

def body
  @body
end

#created_atTime (readonly)

Note creation time

Examples:

Get time note was created

note.created_at #=> Time

Returns:

  • (Time)

    time of creation

Since:

  • 2.0.0



38
39
40
# File 'lib/rubyhexagon/post/note.rb', line 38

def created_at
  @created_at
end

#creatorE621::User (readonly)

Note creator

Examples:

Get creator of note

note.creator #=> E621::User

Returns:

Since:

  • 2.0.0



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

def creator
  @creator
end

#heightInteger (readonly)

Height of note

Examples:

Get height of note

note.height #=> Integer

Returns:

  • (Integer)

    height of object

Since:

  • 2.0.0



79
80
81
# File 'lib/rubyhexagon/post/note.rb', line 79

def height
  @height
end

#idInteger (readonly)

Note ID

Examples:

Get note ID

note.id #=> Integer

Returns:

  • (Integer)

    id for this note

Since:

  • 2.0.0



32
33
34
# File 'lib/rubyhexagon/post/note.rb', line 32

def id
  @id
end

#pointArray<Integer> (readonly)

Convenience method for getting the entire point

Examples:

Get position of note as a point

note.point #=> [x, y]

Returns:

  • (Array<Integer>)

    combined position (x, y)

Since:

  • 2.0.0



67
68
69
# File 'lib/rubyhexagon/post/note.rb', line 67

def point
  @point
end

#postE621::Post (readonly)

Post this note belongs to

Examples:

Get post this note belongs to

note.post #=> E621::Post

Returns:

Since:

  • 2.0.0



91
92
93
# File 'lib/rubyhexagon/post/note.rb', line 91

def post
  @post
end

#rectArray<Integer> (readonly)

Describe note position and size via a rectangular

Examples:

Get note position and size

note.rect #=> [x, y, width, height]

Returns:

  • (Array<Integer>)

    combined rectangular (x, y, width, height)

Since:

  • 2.0.0



85
86
87
# File 'lib/rubyhexagon/post/note.rb', line 85

def rect
  @rect
end

#updated_atTime (readonly)

Time note was last updated

Examples:

Get time note was last updated


Returns:

  • (Time)

    time of last update

Since:

  • 2.0.0



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

def updated_at
  @updated_at
end

#versionInteger (readonly)

Version of note

Examples:

Get version of note

note.version => Integer

Returns:

  • (Integer)

    version of this note

Since:

  • 2.0.0



103
104
105
# File 'lib/rubyhexagon/post/note.rb', line 103

def version
  @version
end

#widthInteger (readonly)

Width of note

Examples:

Get width of note

note.width #=> Integer

Returns:

  • (Integer)

    width of object

Since:

  • 2.0.0



73
74
75
# File 'lib/rubyhexagon/post/note.rb', line 73

def width
  @width
end

#xInteger (readonly)

X position of note

Examples:

Get x position of note

note.x #=> Integer

Returns:

  • (Integer)

    position x value

Since:

  • 2.0.0



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

def x
  @x
end

#yInteger (readonly)

Y position of note

Examples:

Get y position of note

note.y #=> Integer

Returns:

  • (Integer)

    position y value

Since:

  • 2.0.0



61
62
63
# File 'lib/rubyhexagon/post/note.rb', line 61

def y
  @y
end

Class Method Details

.history(query) ⇒ Array<E621::Note>

Fetch a list of note versions

Examples:

Get a list of notes

E621::Post::Note.list(post_id: 1) #=> Array<E621::Post::Note>

Returns:

  • (Array<E621::Note>)

Raises:

  • (ArgumentError)

See Also:

Author:

  • Maxine Michalski

Since:

  • 2.0.0



69
70
71
72
73
74
# File 'lib/rubyhexagon/api/post/note.rb', line 69

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

.list(post) ⇒ Array<E621::Note>

Fetch a list of notes

Examples:

Get a list of notes

E621::Post::Note.list(id: 1) #=> Array<E621::Post::Note>

Returns:

  • (Array<E621::Note>)

See Also:

Author:

  • Maxine Michalski

Since:

  • 2.0.0



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

def self.list(post)
  unless post.is_a?(Hash) || post.is_a?(E621::Post)
    raise ArgumentError, 'A Hash or Post object is required'
  end
  post.store(:post_id, post.delete(:id)) if post.is_a?(Hash)
  post = { post_id: post.id } if post.is_a?(E621::Post)
  E621::API.fetch(:note, :index, post).map do |note|
    new(note)
  end
end

.search(query) ⇒ Array<E621::Note>

Search for notes

Examples:

Get a list of notes

E621::Post::Note.search(query: 'moo') #=> Array<E621::Post::Note>

Returns:

  • (Array<E621::Note>)

Raises:

  • (ArgumentError)

See Also:

Author:

  • Maxine Michalski

Since:

  • 2.0.0



54
55
56
57
58
59
# File 'lib/rubyhexagon/api/post/note.rb', line 54

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

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Comparison method for Notes, to give a more meaningful comparison

Examples:

Compare two notes, that are unequal

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

Parameters:

  • other (Object)

    object that should be compared

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



154
155
156
# File 'lib/rubyhexagon/post/note.rb', line 154

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

#active?TrueClass|FalseClass

Determine if note is active or not

Examples:

Is note active?

note.active? #=> true or false

Returns:

  • (TrueClass|FalseClass)

    active state of note

Author:

  • Maxine Michalski

Since:

  • 2.0.0



142
143
144
145
# File 'lib/rubyhexagon/post/note.rb', line 142

def active?
  return false if @is_active.nil?
  @is_active
end

#to_hashHash

Turn object into a hash representation of itself

Examples:

Turn a Note into a Hash

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

Returns:

  • (Hash)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



165
166
167
168
169
170
171
# File 'lib/rubyhexagon/post/note.rb', line 165

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