fatsecret_lite 0.1.0 → 0.1.1
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: b8bfc4b156e147873fd3dfd1f025a321d438ac50b8574d63719b5aa4fc43d6cf
|
4
|
+
data.tar.gz: 870b5002e79ddd9039b7550d52f6e318030cbc779b1948a93ceca8b293579eb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b92578269aeb7509316a66f38f162a01beef79d08b6b9671c0537bf407790b448bb0218fa9cbab26e407668ebc4ffb541cbca883f301758b15013e627b28ff63
|
7
|
+
data.tar.gz: 69615faadceb36b518c7d2525b20aba3f30266a073e6ef49e10f14372ceb664c6bb0bbb6c02459e727523680397977437eaaf1d7b052978d9e64837a42b98170
|
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
|