Class: Joplin::Resource
- Inherits:
-
Object
- Object
- Joplin::Resource
- Defined in:
- lib/joplin.rb
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#mime ⇒ Object
readonly
Returns the value of attribute mime.
Class Method Summary collapse
Instance Method Summary collapse
- #delete ⇒ Object
- #file ⇒ Object
-
#initialize(id) ⇒ Resource
constructor
A new instance of Resource.
- #to_s ⇒ Object
- #write(path = nil) ⇒ Object
Constructor Details
#initialize(id) ⇒ Resource
Returns a new instance of Resource.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/joplin.rb', line 58 def initialize(id) raise Error, 'need id' unless id @id = id url = "#{Joplin.uri}/resources/#{id}?token=#{Joplin.token}&fields=mime,filename,id" response = HTTP.get url raise NotFound, "No resource found with id: #{id}" if response.code == 404 resource = @parsed = JSON.parse response.body @filename = resource['filename'] @mime = resource['mime'] end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
45 46 47 |
# File 'lib/joplin.rb', line 45 def filename @filename end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
45 46 47 |
# File 'lib/joplin.rb', line 45 def id @id end |
#mime ⇒ Object (readonly)
Returns the value of attribute mime.
45 46 47 |
# File 'lib/joplin.rb', line 45 def mime @mime end |
Class Method Details
.all ⇒ Object
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/joplin.rb', line 47 def self.all url = "#{Joplin.uri}/resources/?token=#{Joplin.token}&fields=id,filename" response = HTTP.get url parsed = JSON.parse res.body throw Error.new(parsed['error']) if response.status != 200 parsed.map do |resource| Resource.new resource['id'] end end |
Instance Method Details
#delete ⇒ Object
82 83 84 85 86 |
# File 'lib/joplin.rb', line 82 def delete url = "#{Joplin.uri}/resources/#{id}?token=#{Joplin.token}" res = HTTP.delete url res.status == 200 end |
#file ⇒ Object
71 72 73 74 75 76 |
# File 'lib/joplin.rb', line 71 def file url = "#{Joplin.uri}/resources/#{id}/file?token=#{Joplin.token}" response = HTTP.get url throw Error if response.status != 200 response.body end |
#to_s ⇒ Object
88 89 90 |
# File 'lib/joplin.rb', line 88 def to_s %(id: #{id}, mime: #{mime} filename: #{filename}) end |
#write(path = nil) ⇒ Object
78 79 80 |
# File 'lib/joplin.rb', line 78 def write(path = nil) IO.write path || id, file end |