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)


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

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) ⇒ Object



84
85
86
87
# File 'lib/freshjots.rb', line 84

def append(filename, text)
  request(:post, "/notes/by-filename/#{escape(filename)}/append", { text: text })
  true
end

#create(title:, body: "") ⇒ 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.



74
75
76
77
78
79
80
81
82
# File 'lib/freshjots.rb', line 74

def create(title:, body: "")
  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
  payload = { note: { title: title, plain_body: body, format: "plain" } }
  request(:post, "/notes", payload)
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.



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

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

#foldersObject

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



106
107
108
# File 'lib/freshjots.rb', line 106

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.



100
101
102
103
# File 'lib/freshjots.rb', line 100

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.



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

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.



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

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).



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

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