fatsecret 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Umang Chouhan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
1
+ = fatsecret
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to fatsecret
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Umang Chouhan. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,10 @@
1
+ require 'base64'
2
+ require 'digest'
3
+ require 'hmac'
4
+ require 'hmac-sha1'
5
+ require 'uri'
6
+ require 'cgi'
7
+ require 'uuid'
8
+ require 'net/http'
9
+ require 'oauth/client'
10
+ require 'fatsecret/client'
@@ -0,0 +1,14 @@
1
+ module FatSecret
2
+ class Client < Oauth::Client
3
+ self.site = "http://platform.fatsecret.com/rest/server.api"
4
+ self.format = 'json'
5
+
6
+ def find_food_by_id(id)
7
+ get({ :method => 'food.get', :food_id => id })
8
+ end
9
+
10
+ def create_profile(id = nil)
11
+ get({ :method => 'profile.create', :user_id => id })
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,79 @@
1
+ module Oauth
2
+ class Client
3
+ OAUTH_SIGNATURE_METHOD = 'HMAC-SHA1'
4
+ OAUTH_VERSION = '1.0'
5
+
6
+ attr_reader :params, :key, :secret
7
+
8
+ class << self
9
+ attr_accessor :site
10
+ attr_accessor :format
11
+ end
12
+
13
+ def initialize(key, secret)
14
+ @key = key
15
+ @secret = secret
16
+ setup
17
+ end
18
+
19
+ def get(options = {})
20
+ params.merge!(options)
21
+ result
22
+ end
23
+
24
+ private
25
+ def setup
26
+ @params = {
27
+ :oauth_consumer_key => key,
28
+ :oauth_signature_method => OAUTH_SIGNATURE_METHOD,
29
+ :oauth_timestamp => timestamp,
30
+ :oauth_version => OAUTH_VERSION,
31
+ :oauth_nonce => nonce,
32
+ :format => self.class.format
33
+ }
34
+ end
35
+
36
+ def timestamp
37
+ Time.now.to_i
38
+ end
39
+
40
+ def nonce
41
+ Digest::MD5.hexdigest(UUID.new.generate).to_s
42
+ end
43
+
44
+ def sorted_params
45
+ params.sort { |a, b| a.first.to_s <=> b.first.to_s }
46
+ end
47
+
48
+ def result
49
+ results = Net::HTTP.get(uri)
50
+ end
51
+
52
+ def base(http_method = 'GET')
53
+ [ escape(http_method), escape(self.class.site), escape(sorted_params.collect { |pair| "#{pair.first}=#{pair.last}" }.join('&')) ].join("&")
54
+ end
55
+
56
+ def signature
57
+ escape(sign(base))
58
+ end
59
+
60
+ def request(method = 'GET')
61
+ sorted_params.inject([]) { |arr, pair| arr << "#{pair.first.to_s}=#{pair.last}" }.join("&")
62
+ end
63
+
64
+ def sign(base, token='')
65
+ secret = "#{escape(@secret)}&#{escape(token)}"
66
+ base64 = Base64.encode64(HMAC::SHA1.digest(secret, base)).gsub(/\n/, '')
67
+ end
68
+
69
+ def uri
70
+ parts = request.split('&')
71
+ parts << "oauth_signature=#{signature}"
72
+ URI.parse("#{self.class.site}?#{parts.join('&')}")
73
+ end
74
+
75
+ def escape(string)
76
+ CGI.escape(string).gsub("%7E", "~").gsub("+", "%20")
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe FatSecret::Client, '.site'do
4
+ it 'should set the site to the fatsecret platform API' do
5
+ FatSecret::Client.site.should == 'http://platform.fatsecret.com/rest/server.api'
6
+ end
7
+ end
8
+
9
+ describe FatSecret::Client, '.format'do
10
+ it 'should set the format to JSON' do
11
+ FatSecret::Client.format.should == 'json'
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe Oauth::Client, '#initialize' do
4
+ let(:key) { 'key' }
5
+ let(:secret) { 'secret' }
6
+
7
+ before do
8
+ @client = Oauth::Client.new key, secret
9
+ end
10
+
11
+ it 'should set the API key from the options' do
12
+ @client.key.should == key
13
+ end
14
+
15
+ it 'should set the API secret from the options' do
16
+ @client.secret.should == secret
17
+ end
18
+ end
19
+
20
+ describe Oauth::Client, '.site' do
21
+ let(:site) { 'http://test.com' }
22
+
23
+ before do
24
+ Oauth::Client.site = site
25
+ end
26
+
27
+ it 'should define a class variable accessor for site' do
28
+ Oauth::Client.site.should == site
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ begin
5
+ Bundler.setup(:default, :development, :test)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+
12
+ require 'shoulda'
13
+
14
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
15
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
16
+
17
+ require 'fatsecret'
18
+
19
+ RSpec.configure do |c|
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,192 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fatsecret
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
11
+ platform: ruby
12
+ authors:
13
+ - Umang Chouhan
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-03 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ prerelease: false
23
+ type: :runtime
24
+ name: uuid
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ">="
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
37
+ type: :runtime
38
+ name: ruby-hmac
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ requirement: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ prerelease: false
51
+ type: :runtime
52
+ name: rest-client
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ requirement: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ prerelease: false
65
+ type: :development
66
+ name: ruby-debug
67
+ version_requirements: &id004 !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirement: *id004
77
+ - !ruby/object:Gem::Dependency
78
+ prerelease: false
79
+ type: :development
80
+ name: shoulda
81
+ version_requirements: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ requirement: *id005
91
+ - !ruby/object:Gem::Dependency
92
+ prerelease: false
93
+ type: :development
94
+ name: bundler
95
+ version_requirements: &id006 !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ~>
99
+ - !ruby/object:Gem::Version
100
+ hash: 23
101
+ segments:
102
+ - 1
103
+ - 0
104
+ - 0
105
+ version: 1.0.0
106
+ requirement: *id006
107
+ - !ruby/object:Gem::Dependency
108
+ prerelease: false
109
+ type: :development
110
+ name: jeweler
111
+ version_requirements: &id007 !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ~>
115
+ - !ruby/object:Gem::Version
116
+ hash: 7
117
+ segments:
118
+ - 1
119
+ - 5
120
+ - 2
121
+ version: 1.5.2
122
+ requirement: *id007
123
+ - !ruby/object:Gem::Dependency
124
+ prerelease: false
125
+ type: :development
126
+ name: rcov
127
+ version_requirements: &id008 !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ hash: 3
133
+ segments:
134
+ - 0
135
+ version: "0"
136
+ requirement: *id008
137
+ description: FatSecret API Client
138
+ email: uchouhan@optimiscorp.com
139
+ executables: []
140
+
141
+ extensions: []
142
+
143
+ extra_rdoc_files:
144
+ - LICENSE.txt
145
+ - README.rdoc
146
+ files:
147
+ - lib/fatsecret.rb
148
+ - lib/fatsecret/client.rb
149
+ - lib/oauth/client.rb
150
+ - LICENSE.txt
151
+ - README.rdoc
152
+ - spec/lib/fatsecret/client_spec.rb
153
+ - spec/lib/oauth/client_spec.rb
154
+ - spec/spec_helper.rb
155
+ has_rdoc: true
156
+ homepage: http://github.com/uchouhan/fatsecret
157
+ licenses:
158
+ - MIT
159
+ post_install_message:
160
+ rdoc_options: []
161
+
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ none: false
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ hash: 3
170
+ segments:
171
+ - 0
172
+ version: "0"
173
+ required_rubygems_version: !ruby/object:Gem::Requirement
174
+ none: false
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ hash: 3
179
+ segments:
180
+ - 0
181
+ version: "0"
182
+ requirements: []
183
+
184
+ rubyforge_project:
185
+ rubygems_version: 1.3.7
186
+ signing_key:
187
+ specification_version: 3
188
+ summary: FatSecret API Client
189
+ test_files:
190
+ - spec/lib/fatsecret/client_spec.rb
191
+ - spec/lib/oauth/client_spec.rb
192
+ - spec/spec_helper.rb