Class: Joplin::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/joplin.rb

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Resource

Returns a new instance of Resource.

Raises:



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

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#mimeObject (readonly)

Returns the value of attribute mime.



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

def mime
  @mime
end

Class Method Details

.allObject



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

.orphanedObject



92
93
94
95
96
# File 'lib/joplin.rb', line 92

def self.orphaned
  resources = all.map { |r| r.id }
  note_resources = Note.all.map { |n| n.resources }.flatten.map { |r| r.id }
  resources.difference(note_resources).map { |id| Resource.new id }
end

Instance Method Details

#deleteObject



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

#fileObject



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_sObject



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