fatsecret-api 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/fatsecret.rb +87 -0
- metadata +45 -0
data/lib/fatsecret.rb
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'openssl'
|
4
|
+
require 'cgi'
|
5
|
+
require 'base64'
|
6
|
+
|
7
|
+
class String
|
8
|
+
def esc
|
9
|
+
CGI.escape(self).gsub("%7E", "~").gsub("+", "%20")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class FatSecret
|
14
|
+
|
15
|
+
@@key = ""
|
16
|
+
@@secret = ""
|
17
|
+
|
18
|
+
SHA1 = "HMAC-SHA1"
|
19
|
+
SITE = "http://platform.fatsecret.com/rest/server.api"
|
20
|
+
DIGEST = OpenSSL::Digest::Digest.new('sha1')
|
21
|
+
|
22
|
+
def self.init(key, secret)
|
23
|
+
@@key = key
|
24
|
+
@@secret = secret
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.search_food(expression)
|
28
|
+
query = {
|
29
|
+
:method => 'foods.search',
|
30
|
+
:search_expression => expression
|
31
|
+
}
|
32
|
+
get(query)
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.food(id)
|
36
|
+
query = {
|
37
|
+
:method => 'food.get',
|
38
|
+
:food_id => id
|
39
|
+
}
|
40
|
+
get(query)
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def self.get(query)
|
46
|
+
params = {
|
47
|
+
:format => 'json',
|
48
|
+
:oauth_consumer_key => @@key,
|
49
|
+
:oauth_nonce => "1234",
|
50
|
+
:oauth_signature_method => SHA1,
|
51
|
+
:oauth_timestamp => Time.now.to_i,
|
52
|
+
:oauth_version => "1.0",
|
53
|
+
}
|
54
|
+
params.merge!(query)
|
55
|
+
sorted_params = params.sort {|a, b| a.first.to_s <=> b.first.to_s}
|
56
|
+
base = base_string("GET", sorted_params)
|
57
|
+
http_params = http_params("GET", params)
|
58
|
+
sig = sign(base).esc
|
59
|
+
uri = uri_for(http_params, sig)
|
60
|
+
results = JSON.parse(Net::HTTP.get(uri))
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.base_string(http_method, param_pairs)
|
64
|
+
param_str = param_pairs.collect{|pair| "#{pair.first}=#{pair.last}"}.join('&')
|
65
|
+
list = [http_method.esc, SITE.esc, param_str.esc]
|
66
|
+
list.join("&")
|
67
|
+
end
|
68
|
+
|
69
|
+
def self.http_params(method, args)
|
70
|
+
pairs = args.sort {|a, b| a.first.to_s <=> b.first.to_s}
|
71
|
+
list = []
|
72
|
+
pairs.inject(list) {|arr, pair| arr << "#{pair.first.to_s}=#{pair.last}"}
|
73
|
+
list.join("&")
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.sign(base, token='')
|
77
|
+
secret_token = "#{@@secret.esc}&#{token.esc}"
|
78
|
+
base64 = Base64.encode64(OpenSSL::HMAC.digest(DIGEST, secret_token, base)).gsub(/\n/, '')
|
79
|
+
end
|
80
|
+
|
81
|
+
def self.uri_for(params, signature)
|
82
|
+
parts = params.split('&')
|
83
|
+
parts << "oauth_signature=#{signature}"
|
84
|
+
URI.parse("#{SITE}?#{parts.join('&')}")
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fatsecret-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ibrahim Muhammad
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-19 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: A simple hello world gem
|
15
|
+
email: ibrahim.mohammad@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/fatsecret.rb
|
21
|
+
homepage: http://www.github.com/whistler/fatsecret
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.11
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Connects to FatSecret API and retreives nutritional data
|
45
|
+
test_files: []
|