Class: Freshjots::Client

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

Constant Summary collapse

DEFAULT_BASE_URL =
"https://freshjots.com/api/v1"

Instance Method Summary collapse

Constructor Details

#initialize(token: ENV["FRESHJOTS_TOKEN"], base_url: DEFAULT_BASE_URL) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


38
39
40
41
42
# File 'lib/freshjots.rb', line 38

def initialize(token: ENV["FRESHJOTS_TOKEN"], base_url: DEFAULT_BASE_URL)
  raise ArgumentError, "FRESHJOTS_TOKEN missing — pass token: or set the env var" if token.nil? || token.empty?

  @token, @base_url = token, base_url
end

Instance Method Details

#append(filename, text, client_encrypted: false) ⇒ Object

On first-touch creation, pass client_encrypted: true to open the stream as a client-encrypted note (send one ciphertext line per append). Ignored once the note exists.



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

def append(filename, text, client_encrypted: false)
  body = { text: text }
  body[:client_encrypted] = true if client_encrypted
  request(:post, "/notes/by-filename/#{escape(filename)}/append", body)
  true
end

#create(title:, body: "", client_encrypted: false) ⇒ Object

Create a note. The API permits note[title, plain_body, format, ...] — NOT filename: the server DERIVES the filename from the title. For a note addressable by an exact, caller-chosen filename, use append (the by-filename endpoint creates it with that exact name on first call). Returns the created note hash (top level); read [:filename] for the server-derived stream name. Pass client_encrypted: true to mark the note as a client-encrypted note — body is opaque ciphertext you produced with Freshjots.encrypt; the server stores it verbatim and never reads it. Personal accounts only.



78
79
80
81
82
83
84
85
86
87
# File 'lib/freshjots.rb', line 78

def create(title:, body: "", client_encrypted: false)
  if title.nil? || title.to_s.empty?
    raise ArgumentError,
          "create requires a title — the API derives the filename from it. " \
          "For a note addressable by an exact filename, use append."
  end
  note = { title: title, plain_body: body, format: "plain" }
  note[:client_encrypted] = true if client_encrypted
  request(:post, "/notes", { note: note })
end

#delete(id_or_filename) ⇒ Object

Delete a note. Accepts a numeric id or a filename (resolved to its id via the by-filename lookup). Locked (append-only) notes are refused by the API with note_locked. Returns true on success.



102
103
104
105
# File 'lib/freshjots.rb', line 102

def delete(id_or_filename)
  request(:delete, "/notes/#{resolve_note_id(id_or_filename)}")
  true
end

#foldersObject

List folders ({ folders: [...] } envelope).



116
117
118
# File 'lib/freshjots.rb', line 116

def folders
  request(:get, "/folders")[:folders]
end

#move(id_or_filename, folder: nil) ⇒ Object

Move a note into a folder. folder may be a folder id, a folder name (resolved via /folders), or nil / "none" / "root" to send the note back to the root.



110
111
112
113
# File 'lib/freshjots.rb', line 110

def move(id_or_filename, folder: nil)
  id = resolve_note_id(id_or_filename)
  request(:post, "/notes/#{id}/move", { folder_id: resolve_folder_id(folder) })
end

#note(filename) ⇒ Object

show-by-filename renders the serializer at the top level (no { note: ... } wrapper), so the response is the note hash.



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

def note(filename)
  request(:get, "/notes/by-filename/#{escape(filename)}")
end

#note_by_id(id) ⇒ Object

Full note by numeric id (GET /notes/:id) — top-level serializer.



65
66
67
# File 'lib/freshjots.rb', line 65

def note_by_id(id)
  request(:get, "/notes/#{escape(id)}")
end

#notes(sort: nil, folder_id: nil, limit: nil, offset: nil) ⇒ Object

List notes (summary projection). Keyword options mirror the API query params: sort (created|updated|appended, default updated), folder_id (a folder id, or "none" for un-foldered notes only), and limit/offset for pagination (the server caps a page at 200).



48
49
50
51
52
53
54
55
56
# File 'lib/freshjots.rb', line 48

def notes(sort: nil, folder_id: nil, limit: nil, offset: nil)
  query = {}
  query[:sort]      = sort      if sort
  query[:folder_id] = folder_id unless folder_id.nil? || folder_id.to_s.empty?
  query[:limit]     = limit     unless limit.nil?
  query[:offset]    = offset    unless offset.nil?
  path = query.empty? ? "/notes" : "/notes?#{URI.encode_www_form(query)}"
  request(:get, path)[:notes]
end