Class: ThemealdbRubygem::Client

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

Constant Summary collapse

BASE_URL =
"https://www.themealdb.com/api/json/v1/1/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, adapter: Faraday.default_adapter) ⇒ Client



10
11
12
13
14
# File 'lib/themealdb_rubygem/client.rb', line 10

def initialize(api_key: nil, adapter: Faraday.default_adapter)
  @adapter = adapter
  @api_key = api_key
  @url = api_key.nil? ? BASE_URL : "https://www.themealdb.com/api/json/v2/#{api_key}/" 
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



8
9
10
# File 'lib/themealdb_rubygem/client.rb', line 8

def adapter
  @adapter
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



8
9
10
# File 'lib/themealdb_rubygem/client.rb', line 8

def api_key
  @api_key
end

Instance Method Details

#connectionObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/themealdb_rubygem/client.rb', line 16

def connection
  @connection ||= Faraday.new do |conn|
    conn.url_prefix = @url
    conn.request :json
    conn.response :json, content_type: "application/json"
    conn.adapter adapter
    conn.headers['Accept'] = 'application/json'
    conn.use Faraday::Response::RaiseError
  end
end

#filter_by_area(area) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/themealdb_rubygem/client.rb', line 102

def filter_by_area(area)
  #clean up spaces in search string
  str = area.gsub(/\s+,/, ',').gsub(/,\s+/, ',').strip
  response = connection.get("filter.php", {a: str})
  if response.body["meals"]
    { code: response.status, data: response.body }
  else
    { code: 404, data: 'No meals found' }
  end
end

#filter_by_category(category) ⇒ Object



91
92
93
94
95
96
97
98
99
100
# File 'lib/themealdb_rubygem/client.rb', line 91

def filter_by_category(category)
  #clean up spaces in search string
  str = category.gsub(/\s+,/, ',').gsub(/,\s+/, ',').strip
  response = connection.get("filter.php", {c: str})
  if response.body["meals"]
    { code: response.status, data: response.body }
  else
    { code: 404, data: 'No meals found' }
  end
end

#filter_by_main_ingredient(ingredient) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/themealdb_rubygem/client.rb', line 80

def filter_by_main_ingredient(ingredient)
  # clean up spaces in search string
  str = ingredient.gsub(/\s+,/, ',').gsub(/,\s+/, ',').strip
  response = connection.get("filter.php", {i: str})
  if response.body["meals"]
    { code: response.status, data: response.body }
  else
    { code: 404, data: 'No meals found' }
  end
end

#list_areasObject



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

def list_areas
  response = connection.get("list.php", {a: 'list'})
  { code: response.status, data: response.body }
end

#list_categoriesObject



65
66
67
68
# File 'lib/themealdb_rubygem/client.rb', line 65

def list_categories
  response = connection.get("list.php", {c: 'list'})
  { code: response.status, data: response.body }
end

#list_categories_fullObject



60
61
62
63
# File 'lib/themealdb_rubygem/client.rb', line 60

def list_categories_full
  response = connection.get("categories.php")
  { code: response.status, data: response.body }
end

#list_ingredientsObject



75
76
77
78
# File 'lib/themealdb_rubygem/client.rb', line 75

def list_ingredients
  response = connection.get("list.php", {i: 'list'})
  { code: response.status, data: response.body }
end

#random_mealObject



55
56
57
58
# File 'lib/themealdb_rubygem/client.rb', line 55

def random_meal
  response = connection.get("random.php")
  { code: response.status, data: response.body }
end

#search_by_first_letter(letter) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/themealdb_rubygem/client.rb', line 37

def search_by_first_letter(letter)
  response = connection.get("search.php", {f: letter})
  if response.body["meals"]
    { code: response.status, data: response.body }
  else
    { code: 404, data: 'No meals found' }
  end
end

#search_by_id(id) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/themealdb_rubygem/client.rb', line 46

def search_by_id(id)
  response = connection.get("lookup.php", {i: id})
  if response.body["meals"]
    { code: response.status, data: response.body }
  else
    { code: 404, data: 'No meals found' }
  end
end

#search_by_name(name) ⇒ Object

endpoints



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

def search_by_name(name)
  response = connection.get("search.php", {s: name})
  if response.body["meals"]
    { code: response.status, data: response.body }
  else
    { code: 404, data: 'No meals found' }
  end
end