Class: Joplin::Note
- Inherits:
-
Object
- Object
- Joplin::Note
- Defined in:
- lib/joplin.rb
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#parent_id ⇒ Object
Returns the value of attribute parent_id.
-
#title ⇒ Object
Returns the value of attribute title.
Class Method Summary collapse
Instance Method Summary collapse
- #delete! ⇒ Object
-
#initialize(id: nil, parent_id: nil) ⇒ Note
constructor
A new instance of Note.
- #prepare_body_for_writing_by_fixing_resources ⇒ Object
- #resources ⇒ Object
- #save! ⇒ Object
- #to_json(*_args) ⇒ Object
- #to_s ⇒ Object
- #write(path = nil) ⇒ Object
Constructor Details
#initialize(id: nil, parent_id: nil) ⇒ Note
Returns a new instance of Note.
105 106 107 108 109 110 111 112 |
# File 'lib/joplin.rb', line 105 def initialize(id: nil, parent_id: nil) @id = id @parent_id = parent_id return unless id url = "#{Joplin.uri}/notes/#{id}?token=#{Joplin.token}&fields=title,body,id,parent_id" parse HTTP.get url end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
102 103 104 |
# File 'lib/joplin.rb', line 102 def body @body end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
103 104 105 |
# File 'lib/joplin.rb', line 103 def id @id end |
#parent_id ⇒ Object
Returns the value of attribute parent_id.
102 103 104 |
# File 'lib/joplin.rb', line 102 def parent_id @parent_id end |
#title ⇒ Object
Returns the value of attribute title.
102 103 104 |
# File 'lib/joplin.rb', line 102 def title @title end |
Class Method Details
Instance Method Details
#delete! ⇒ Object
175 176 177 178 179 |
# File 'lib/joplin.rb', line 175 def delete! url = "#{Joplin.uri}/notes/#{@id}?token=#{Joplin.token}" response = HTTP.delete url response.status == 200 end |
#prepare_body_for_writing_by_fixing_resources ⇒ Object
126 127 128 129 130 |
# File 'lib/joplin.rb', line 126 def prepare_body_for_writing_by_fixing_resources prepared = String(body) prepared.each_line do |line| end end |
#resources ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/joplin.rb', line 114 def resources url = "#{Joplin.uri}/notes/#{id}/resources?token=#{Joplin.token}&fields=id,filename" response = HTTP.get url raise Error, "#{response}" if response.code != 200 parsed = JSON.parse response.body parsed['items'].map do |resource_data| id = resource_data['id'] Resource.new id end end |
#save! ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/joplin.rb', line 151 def save! if @id url = "#{Joplin.uri}/notes/#{@id}?token=#{Joplin.token}" response = HTTP.put url, body: to_json return response.status == 200 end url = "#{Joplin.uri}/notes/?token=#{Joplin.token}" parse HTTP.post url, body: to_json end |
#to_json(*_args) ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/joplin.rb', line 143 def to_json(*_args) { title:, body:, parent_id: }.to_json end |
#to_s ⇒ Object
162 163 164 |
# File 'lib/joplin.rb', line 162 def to_s %(id: #{id} title: #{title} parent_id: #{parent_id} body: #{body}) end |
#write(path = nil) ⇒ Object
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/joplin.rb', line 132 def write(path = nil) dir = path || title FileUtils.mkdir_p "#{dir}/resources" body_to_write = String(body) # make a copy resources.each do |resource| resource.write "#{dir}/resources/#{resource.id}" body_to_write.gsub!(%r{:/#{resource.id}}, "./resources/#{resource.id}") end IO.write "#{dir}/#{title}.md", body_to_write end |