Class: Rubyhexagon::Post::Note
- Inherits:
-
Object
- Object
- Rubyhexagon::Post::Note
- Defined in:
- lib/rubyhexagon/post/note.rb,
lib/rubyhexagon/api/post/note.rb
Overview
A class to interact with the e621 web interface.
Instance Attribute Summary collapse
-
#body ⇒ String
readonly
Text of note.
-
#created_at ⇒ Time
readonly
Note creation time.
-
#creator ⇒ E621::User
readonly
Note creator.
-
#height ⇒ Integer
readonly
Height of note.
-
#id ⇒ Integer
readonly
Note ID.
-
#point ⇒ Array<Integer>
readonly
Convenience method for getting the entire point.
-
#post ⇒ E621::Post
readonly
Post this note belongs to.
-
#rect ⇒ Array<Integer>
readonly
Describe note position and size via a rectangular.
-
#updated_at ⇒ Time
readonly
Time note was last updated.
-
#version ⇒ Integer
readonly
Version of note.
-
#width ⇒ Integer
readonly
Width of note.
-
#x ⇒ Integer
readonly
X position of note.
-
#y ⇒ Integer
readonly
Y position of note.
Class Method Summary collapse
-
.history(query) ⇒ Array<E621::Note>
Fetch a list of note versions.
-
.list(post) ⇒ Array<E621::Note>
Fetch a list of notes.
-
.search(query) ⇒ Array<E621::Note>
Search for notes.
Instance Method Summary collapse
-
#==(other) ⇒ TrueClass, FalseClass
Comparison method for Notes, to give a more meaningful comparison.
-
#active? ⇒ TrueClass|FalseClass
Determine if note is active or not.
-
#initialize(note) ⇒ Object
constructor
Initializer for Note.
-
#to_hash ⇒ Hash
Turn object into a hash representation of itself.
Constructor Details
#initialize(note) ⇒ Object
Initializer for Note
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
#body ⇒ String (readonly)
Text of note
97 98 99 |
# File 'lib/rubyhexagon/post/note.rb', line 97 def body @body end |
#created_at ⇒ Time (readonly)
Note creation time
38 39 40 |
# File 'lib/rubyhexagon/post/note.rb', line 38 def created_at @created_at end |
#creator ⇒ E621::User (readonly)
Note creator
49 50 51 |
# File 'lib/rubyhexagon/post/note.rb', line 49 def creator @creator end |
#height ⇒ Integer (readonly)
Height of note
79 80 81 |
# File 'lib/rubyhexagon/post/note.rb', line 79 def height @height end |
#id ⇒ Integer (readonly)
Note ID
32 33 34 |
# File 'lib/rubyhexagon/post/note.rb', line 32 def id @id end |
#point ⇒ Array<Integer> (readonly)
Convenience method for getting the entire point
67 68 69 |
# File 'lib/rubyhexagon/post/note.rb', line 67 def point @point end |
#post ⇒ E621::Post (readonly)
Post this note belongs to
91 92 93 |
# File 'lib/rubyhexagon/post/note.rb', line 91 def post @post end |
#rect ⇒ Array<Integer> (readonly)
Describe note position and size via a rectangular
85 86 87 |
# File 'lib/rubyhexagon/post/note.rb', line 85 def rect @rect end |
#updated_at ⇒ Time (readonly)
Time note was last updated
43 44 45 |
# File 'lib/rubyhexagon/post/note.rb', line 43 def updated_at @updated_at end |
#version ⇒ Integer (readonly)
Version of note
103 104 105 |
# File 'lib/rubyhexagon/post/note.rb', line 103 def version @version end |
#width ⇒ Integer (readonly)
Width of note
73 74 75 |
# File 'lib/rubyhexagon/post/note.rb', line 73 def width @width end |
#x ⇒ Integer (readonly)
X position of note
55 56 57 |
# File 'lib/rubyhexagon/post/note.rb', line 55 def x @x end |
#y ⇒ Integer (readonly)
Y position of note
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
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
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
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
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
142 143 144 145 |
# File 'lib/rubyhexagon/post/note.rb', line 142 def active? return false if @is_active.nil? @is_active end |
#to_hash ⇒ Hash
Turn object into a hash representation of itself
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 |