Class: PaprikaClient::Client

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

Overview

Client for the Paprika Recipe Manager Cloud Sync API.

client = PaprikaClient::Client.new(email: "you@example.com", password: "secret")
client.recipes                       # => [{ "uid" => "...", "hash" => "..." }, ...]
recipe = client.recipe(uid)          # => PaprikaClient::Recipe
recipe.nutritional_info = "Calories: 200 calories"
client.save_recipe(recipe)           # recomputes hash, uploads, notifies

Authentication is HTTP Basic (email/password) against the v1 sync endpoints, which is what the desktop/mobile apps use under the hood.

Constant Summary collapse

DEFAULT_BASE_URL =
"https://www.paprikaapp.com/api"
USER_AGENT =
"paprika_client (rubygems.org/gems/paprika_client)"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email:, password:, base_url: DEFAULT_BASE_URL) ⇒ Client

Returns a new instance of Client.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
# File 'lib/paprika_client/client.rb', line 27

def initialize(email:, password:, base_url: DEFAULT_BASE_URL)
  raise ArgumentError, "email is required" if email.nil? || email.empty?
  raise ArgumentError, "password is required" if password.nil? || password.empty?

  @email = email
  @password = password
  @base_url = base_url
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



25
26
27
# File 'lib/paprika_client/client.rb', line 25

def base_url
  @base_url
end

Instance Method Details

#categoriesObject

All recipe categories as an array of hashes ({ "uid" => ..., "name" => ... }).



47
48
49
# File 'lib/paprika_client/client.rb', line 47

def categories
  get("/v1/sync/categories/")
end

#mealsObject

All scheduled meals as an array of hashes.



52
53
54
# File 'lib/paprika_client/client.rb', line 52

def meals
  get("/v1/sync/meals/")
end

#notifyObject

Ask the API to notify the recipe apps that changes have occurred.



70
71
72
73
# File 'lib/paprika_client/client.rb', line 70

def notify
  post("/v1/sync/notify/")
  true
end

#recipe(uid) ⇒ Object

Fetch a single full recipe by uid.



42
43
44
# File 'lib/paprika_client/client.rb', line 42

def recipe(uid)
  Recipe.new(get("/v1/sync/recipe/#{uid}/"))
end

#recipesObject

Lightweight list of every recipe as { "uid" => ..., "hash" => ... }.



37
38
39
# File 'lib/paprika_client/client.rb', line 37

def recipes
  get("/v1/sync/recipes/")
end

#save_recipe(recipe, notify: true) ⇒ Object

Create or update a recipe. Accepts a Recipe or a plain attributes hash. The sync hash is always recomputed before upload so other devices detect the change. Calls #notify afterwards unless notify: false.

Raises:

  • (ArgumentError)


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

def save_recipe(recipe, notify: true)
  recipe = Recipe.new(recipe) unless recipe.is_a?(Recipe)
  raise ArgumentError, "recipe must have a uid" if recipe.uid.nil? || recipe.uid.to_s.empty?

  attributes = recipe.with_recomputed_hash
  post_multipart("/v1/sync/recipe/#{recipe.uid}/", gzip(JSON.generate(attributes)))
  self.notify if notify
  Recipe.new(attributes)
end