sugestio-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Martin May
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.
data/README.rdoc ADDED
@@ -0,0 +1,29 @@
1
+ = sugestio-ruby
2
+
3
+ A basic Ruby library for interfacing with the Sugestio (http://sugestio.com) recommendation service.
4
+
5
+ = Pre-requisites
6
+
7
+ * oauth gem (http://github.com/pelle/oauth)
8
+
9
+ = Usage
10
+
11
+ require 'sugestio'
12
+ client = Sugestio.new('username', 'password')
13
+ result = client.get_get_recommendations(user_id)
14
+
15
+ == Note on Patches/Pull Requests
16
+
17
+ * Fork the project.
18
+ * Make your feature addition or bug fix.
19
+ * Add tests for it. This is important so I don't break it in a
20
+ future version unintentionally.
21
+ * Commit, do not mess with rakefile, version, or history.
22
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
23
+ * Send me a pull request. Bonus points for topic branches.
24
+
25
+ == Credits
26
+
27
+ Written by Martin May (http://github.com/HiroProt). Portions inspired by (http://github.com/archfear/simplegeo-ruby).
28
+
29
+ Copyright (c) 2010 Martin May. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sugestio-ruby"
8
+ gem.summary = %Q{A basic Ruby library for interfacing with the Sugestio (http://sugestio.com) recommendation service.}
9
+ gem.description = %Q{A basic Ruby library for interfacing with the Sugestio (http://sugestio.com) recommendation service.}
10
+ gem.email = "martin@martinmay.net"
11
+ gem.homepage = "http://github.com/hiroprot/sugestio-ruby"
12
+ gem.authors = ["Martin May"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ gem.add_dependency 'oauth'
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/test_*.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
49
+
50
+ rdoc.rdoc_dir = 'rdoc'
51
+ rdoc.title = "sugestio-ruby #{version}"
52
+ rdoc.rdoc_files.include('README*')
53
+ rdoc.rdoc_files.include('lib/**/*.rb')
54
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/sugestio.rb ADDED
@@ -0,0 +1,110 @@
1
+ require 'oauth'
2
+ require 'json'
3
+
4
+ class Sugestio
5
+
6
+ class SugestioError < StandardError; end
7
+ class Unauthorized < SugestioError; end
8
+ class NotFound < SugestioError; end
9
+ class ServerError < SugestioError; end
10
+ class BadRequest < SugestioError; end
11
+ class DecodeError < SugestioError; end
12
+ class UnknownError < SugestioError; end
13
+
14
+ VERSION = '0.1'
15
+ URL_BASE = 'http://api.sugestio.com'
16
+ USER_AGENT = "sugestio-ruby/#{VERSION}"
17
+
18
+ def initialize(username, password)
19
+ super
20
+ @username = username
21
+ @password = password
22
+
23
+ consumer = OAuth::Consumer.new(@username, @password, :site => URL_BASE)
24
+ @access_token = OAuth::AccessToken.new(consumer)
25
+ end
26
+
27
+ def add_user(user)
28
+ return api_request("/sites/#{@username}/users.json", :post, user)
29
+ end
30
+
31
+ def add_item(item)
32
+ return api_request("/sites/#{@username}/items.json", :post, item)
33
+ end
34
+
35
+ def add_consumption(consumption)
36
+ return api_request("/sites/#{@username}/consumptions.json", :post, consumption)
37
+ end
38
+
39
+ def get_recommendations(user_id, options = {})
40
+ return api_request("/sites/#{@username}/users/#{user_id}/recommendations.json", :get, options)
41
+ end
42
+
43
+ def get_similar_items(item_id, options = {})
44
+ return api_request("/sites/#{@username}/items/#{item_id}/similar.json", :get, options)
45
+ end
46
+
47
+ def get_similar_users(user_id, options = {})
48
+ return api_request("/sites/#{@username}/users/#{user_id}/similar.json", :get, options)
49
+ end
50
+
51
+ def get_analytics
52
+ return api_request("/sites/#{@username}/analytics.json", :get)
53
+ end
54
+
55
+ protected
56
+
57
+ def api_request(path, method, data = {})
58
+ headers = {'User-Agent' => USER_AGENT}
59
+ response = nil
60
+ parsed_response = nil
61
+
62
+ url = URL_BASE + path
63
+
64
+ case method
65
+ when :get, :delete
66
+ url = "#{url}?#{build_query_string(data)}"
67
+ response = @access_token.request(method, url, headers)
68
+ when :post, :put
69
+ response = @access_token.request(method, url, data, headers)
70
+ end
71
+
72
+ puts "Response Code: #{response.code}"
73
+ handle_errors(response)
74
+
75
+ begin
76
+ parsed_response = JSON.parse(response.body)
77
+ rescue JSON::ParserError
78
+ raise DecodeError, "content: <#{response.body}>"
79
+ end
80
+
81
+ return parsed_response
82
+ end
83
+
84
+ def handle_errors(response)
85
+ response_description = "(#{response.code}): #{response.message}"
86
+ response_description += " - #{response.body}" unless response.body.empty?
87
+
88
+ case response.code.to_i
89
+ when 400
90
+ raise BadRequest, response_description
91
+ when 401
92
+ raise Unauthorized, response_description
93
+ when 404
94
+ raise NotFound, response_description
95
+ when 500
96
+ raise ServerError, response_description
97
+ else
98
+ unless [200, 201, 202].include?(response.code.to_i)
99
+ raise UnknownError, response_description
100
+ end
101
+ end
102
+ end
103
+
104
+ def build_query_string(data)
105
+ data.map do |key, value|
106
+ [key.to_s, URI.escape(value.to_s)].join('=')
107
+ end.join('&')
108
+ end
109
+
110
+ end
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{sugestio-ruby}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Martin May"]
12
+ s.date = %q{2010-10-25}
13
+ s.description = %q{A basic Ruby library for interfacing with the Sugestio (http://sugestio.com) recommendation service.}
14
+ s.email = %q{martin@martinmay.net}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/sugestio.rb",
27
+ "sugestio-ruby.gemspec"
28
+ ]
29
+ s.homepage = %q{http://github.com/hiroprot/sugestio-ruby}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.7}
33
+ s.summary = %q{A basic Ruby library for interfacing with the Sugestio (http://sugestio.com) recommendation service.}
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
41
+ s.add_runtime_dependency(%q<oauth>, [">= 0"])
42
+ else
43
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
44
+ s.add_dependency(%q<oauth>, [">= 0"])
45
+ end
46
+ else
47
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
48
+ s.add_dependency(%q<oauth>, [">= 0"])
49
+ end
50
+ end
51
+
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sugestio-ruby
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Martin May
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-10-25 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: thoughtbot-shoulda
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :development
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: oauth
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :runtime
45
+ version_requirements: *id002
46
+ description: A basic Ruby library for interfacing with the Sugestio (http://sugestio.com) recommendation service.
47
+ email: martin@martinmay.net
48
+ executables: []
49
+
50
+ extensions: []
51
+
52
+ extra_rdoc_files:
53
+ - LICENSE
54
+ - README.rdoc
55
+ files:
56
+ - .document
57
+ - .gitignore
58
+ - LICENSE
59
+ - README.rdoc
60
+ - Rakefile
61
+ - VERSION
62
+ - lib/sugestio.rb
63
+ - sugestio-ruby.gemspec
64
+ has_rdoc: true
65
+ homepage: http://github.com/hiroprot/sugestio-ruby
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options:
70
+ - --charset=UTF-8
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.7
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: A basic Ruby library for interfacing with the Sugestio (http://sugestio.com) recommendation service.
96
+ test_files: []
97
+