Class: Rubyhexagon::Note

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

Parameters:

  • note (Hash)

    note information in hash form

Raises:

  • (ArgumentError)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubyhexagon/note.rb', line 72

def initialize(note)
  raise ArgumentError, "#{note.class} is not a Hash" unless note.is_a?(Hash)
  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)

Returns text content of note.

Returns:

  • (String)

    text content of note

Since:

  • 2.0.0



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

def body
  @body
end

#created_atTime (readonly)

Returns time of creation.

Returns:

  • (Time)

    time of creation

Since:

  • 2.0.0



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

def created_at
  @created_at
end

#creatorE621::User (readonly)

Returns user object of creator.

Returns:

Since:

  • 2.0.0



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

def creator
  @creator
end

#heightInteger (readonly)

Returns height of object.

Returns:

  • (Integer)

    height of object

Since:

  • 2.0.0



51
52
53
# File 'lib/rubyhexagon/note.rb', line 51

def height
  @height
end

#idInteger (readonly)

Returns id for this note.

Returns:

  • (Integer)

    id for this note

Since:

  • 2.0.0



27
28
29
# File 'lib/rubyhexagon/note.rb', line 27

def id
  @id
end

#pointArray<Integer> (readonly)

Returns combined position (x, y).

Returns:

  • (Array<Integer>)

    combined position (x, y)

Since:

  • 2.0.0



45
46
47
# File 'lib/rubyhexagon/note.rb', line 45

def point
  @point
end

#postE621::Post (readonly)

Returns post this note belongs to.

Returns:

Since:

  • 2.0.0



57
58
59
# File 'lib/rubyhexagon/note.rb', line 57

def post
  @post
end

#rectArray<Integer> (readonly)

Returns combined rectangular (x, y, width, height).

Returns:

  • (Array<Integer>)

    combined rectangular (x, y, width, height)

Since:

  • 2.0.0



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

def rect
  @rect
end

#updated_atTime (readonly)

Returns time of last update.

Returns:

  • (Time)

    time of last update

Since:

  • 2.0.0



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

def updated_at
  @updated_at
end

#versionInteger (readonly)

Returns version of this note.

Returns:

  • (Integer)

    version of this note

Since:

  • 2.0.0



63
64
65
# File 'lib/rubyhexagon/note.rb', line 63

def version
  @version
end

#widthInteger (readonly)

Returns width of object.

Returns:

  • (Integer)

    width of object

Since:

  • 2.0.0



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

def width
  @width
end

#xInteger (readonly)

Returns position x value.

Returns:

  • (Integer)

    position x value

Since:

  • 2.0.0



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

def x
  @x
end

#yInteger (readonly)

Returns position y value.

Returns:

  • (Integer)

    position y value

Since:

  • 2.0.0



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

def y
  @y
end

Class Method Details

.history(query) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 2.0.0



43
44
45
46
47
48
# File 'lib/rubyhexagon/api/note.rb', line 43

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) ⇒ Object

Since:

  • 2.0.0



25
26
27
28
29
30
31
32
33
34
# File 'lib/rubyhexagon/api/note.rb', line 25

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) ⇒ Object

Raises:

  • (ArgumentError)

Since:

  • 2.0.0



36
37
38
39
40
41
# File 'lib/rubyhexagon/api/note.rb', line 36

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.

Returns:

  • (TrueClass, FalseClass)

Author:

  • Maxine Michalski

Since:

  • 2.0.0



108
109
110
# File 'lib/rubyhexagon/note.rb', line 108

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

#active?TrueClass|FalseClass

Determine if note is active or not.

Returns:

  • (TrueClass|FalseClass)

    active state of note

Author:

  • Maxine Michalski

Since:

  • 2.0.0



98
99
100
101
# File 'lib/rubyhexagon/note.rb', line 98

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