fatsecret_lite 0.1.0 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/fatsecret_lite/version.rb +1 -1
- data/lib/fatsecret_lite.rb +60 -5
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c3176512cd1f08db8d7c8985e23ad46b1546b3af468318cd2feaade0238a8288
|
4
|
+
data.tar.gz: ec4f1b4c028b300595c3ee11db90950e3dcebea85bfe2134cfbf85d885228ff7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb1ae96afde72d54aef2fa26d000777be4295ce24d39db3db9a154c69e13149d8002e111792c03745df4110e3da7e393ceb2c787baefd7c13a45da0c228e306a
|
7
|
+
data.tar.gz: e7960007a37e73ba936b382928c8ae37643713539e95d025b2df1ce692050e20eb96f002b99c62da692e639098d3cc9537e186a7373c4ed1aabc31fef3239559
|
data/lib/fatsecret_lite.rb
CHANGED
@@ -1,8 +1,63 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
1
|
+
# lib/fatsecret_lite.rb
|
2
|
+
require "oauth2"
|
3
|
+
require "rest-client"
|
4
|
+
require "json"
|
4
5
|
|
5
6
|
module FatsecretLite
|
6
|
-
class
|
7
|
-
|
7
|
+
class Configuration
|
8
|
+
attr_accessor :client_id, :client_secret
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@client_id = nil
|
12
|
+
@client_secret = nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
@configuration = Configuration.new
|
17
|
+
|
18
|
+
class << self
|
19
|
+
def configure
|
20
|
+
yield(@configuration) if block_given?
|
21
|
+
end
|
22
|
+
|
23
|
+
def configuration
|
24
|
+
@configuration
|
25
|
+
end
|
26
|
+
|
27
|
+
def get_access_token
|
28
|
+
raise "Client ID and Client Secret must be configured" unless configuration.client_id && configuration.client_secret
|
29
|
+
|
30
|
+
client = OAuth2::Client.new(
|
31
|
+
configuration.client_id,
|
32
|
+
configuration.client_secret,
|
33
|
+
site: 'https://oauth.fatsecret.com',
|
34
|
+
token_url: 'https://oauth.fatsecret.com/connect/token'
|
35
|
+
)
|
36
|
+
|
37
|
+
token = client.client_credentials.get_token
|
38
|
+
token.token
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_food_details(food_id)
|
42
|
+
access_token = get_access_token
|
43
|
+
|
44
|
+
response = RestClient.get(
|
45
|
+
'https://platform.fatsecret.com/rest/server.api',
|
46
|
+
params: {
|
47
|
+
method: 'food.get.v2',
|
48
|
+
food_id: food_id,
|
49
|
+
format: 'json'
|
50
|
+
},
|
51
|
+
headers: {
|
52
|
+
Authorization: "Bearer #{access_token}"
|
53
|
+
}
|
54
|
+
)
|
55
|
+
|
56
|
+
JSON.parse(response.body)
|
57
|
+
rescue RestClient::ExceptionWithResponse => e
|
58
|
+
puts "Error: #{e.response}"
|
59
|
+
rescue StandardError => e
|
60
|
+
puts "An error occurred: #{e.message}"
|
61
|
+
end
|
62
|
+
end
|
8
63
|
end
|