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



69
70
71
72
# File 'lib/freshjots.rb', line 69

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.



59
60
61
62
63
64
65
66
67
# File 'lib/freshjots.rb', line 59

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

#note(filename) ⇒ Object

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



49
50
51
# File 'lib/freshjots.rb', line 49

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

#notesObject



43
44
45
# File 'lib/freshjots.rb', line 43

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