Module: FatsecretLite

Defined in:
lib/fatsecret_lite.rb,
lib/fatsecret_lite/version.rb,
lib/fatsecret_lite/configuration.rb

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
"0.2.5"

Class Method Summary collapse

Class Method Details

.configurationObject



23
24
25
# File 'lib/fatsecret_lite.rb', line 23

def configuration
  @configuration
end

.configure {|@configuration| ... } ⇒ Object

Yields:



19
20
21
# File 'lib/fatsecret_lite.rb', line 19

def configure
  yield(@configuration) if block_given?
end

.find_food_by_barcode(barcode) ⇒ Object

find food id by barcode



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fatsecret_lite.rb', line 67

def find_food_by_barcode(barcode)
  access_token = get_access_token

  response = RestClient.post(
    'https://platform.fatsecret.com/rest/server.api',
    {
      method: 'food.find_id_for_barcode',
      barcode: barcode,
      format: 'json'
    },
    {
      content_type: :json,
      Authorization: "Bearer #{access_token}"
    }
  )

  JSON.parse(response.body)
rescue RestClient::ExceptionWithResponse => e
  puts "Error: #{e.response}"
rescue StandardError => e
  puts "An error occurred: #{e.message}"
end

.find_food_by_search(search_value, max_results = 10) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/fatsecret_lite.rb', line 90

def find_food_by_search(search_value, max_results= 10)
  access_token = get_access_token

  response = RestClient.post(
    'https://platform.fatsecret.com/rest/server.api',
    {
      method: 'foods.search.v3',
      max_results: max_results,
      search_expression: search_value,
      format: 'json'
    },
    {
      content_type: :json,
      Authorization: "Bearer #{access_token}"
    }
  )

  JSON.parse(response.body)
rescue RestClient::ExceptionWithResponse => e
  puts "Error: #{e.response}"
rescue StandardError => e
  puts "An error occurred: #{e.message}"
end

.get_access_tokenObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fatsecret_lite.rb', line 27

def get_access_token
  raise "Client ID and Client Secret must be configured" unless configuration.client_id && configuration.client_secret

  client = OAuth2::Client.new(
    configuration.client_id,
    configuration.client_secret,
    site: 'https://platform.fatsecret.com/rest/server.api',
    token_url: 'https://oauth.fatsecret.com/connect/token'
  )

  token = client.client_credentials.get_token
  token.token
end

.get_food_details(food_id) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/fatsecret_lite.rb', line 41

def get_food_details(food_id)
  access_token = get_access_token

  response = RestClient.post(
    'https://platform.fatsecret.com/rest/server.api',
    {
      method: 'food.get.v4',
      food_id: food_id,
      include_food_images: true,
      include_food_attributes: true,
      format: 'json'
    },
    {
      content_type: :json,
      Authorization: "Bearer #{access_token}"
    }
  )

  JSON.parse(response.body)
rescue RestClient::ExceptionWithResponse => e
  puts "Error: #{e.response}"
rescue StandardError => e
  puts "An error occurred: #{e.message}"
end