Class: CookbookClient

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

Defined Under Namespace

Classes: Cuisine, Recipe, RecipeType

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.emailObject

Returns the value of attribute email.



7
8
9
# File 'lib/cookbook_client.rb', line 7

def email
  @email
end

.passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/cookbook_client.rb', line 7

def password
  @password
end

.urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/cookbook_client.rb', line 7

def url
  @url
end

Class Method Details

.form_helpersObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cookbook_client.rb', line 20

def self.form_helpers
  response = JSON.parse(http.get('/api/v1/recipes/form_helper').body)
  {
    cuisines: (
      response['cuisines'].map { |c| [c['id'], c['name']] }
    ).to_h,
    recipe_types: (
      response['recipe_types'].map { |rp| [rp['id'], rp['name']] }
    ).to_h
  }
end

.format_recipes(recipes:, content_class:) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/cookbook_client.rb', line 32

def self.format_recipes(recipes:, content_class:)
  recipes = [recipes] unless recipes.is_a?(Array)
  helpers = form_helpers
  recipes.map do |r|
    r['cuisine'] = helpers[:cuisines][r['cuisine_id']]
    r['recipe_type'] = helpers[:recipe_types][r['recipe_type_id']]
    content_class.new info: r
  end
end

.httpObject



10
11
12
13
14
15
16
17
18
# File 'lib/cookbook_client.rb', line 10

def self.http
  if !@email.nil? && !@password.nil?
    return Faraday.new url: @url do |conn|
      conn.basic_auth(@email, @password)
      conn.adapter :net_http
    end
  end
  Faraday.new url: @url
end