Updated Klout to use the latest API #2

Open
wants to merge 4 commits into
from
View
42 .gitignore
@@ -0,0 +1,42 @@
+# rcov generated
+coverage
+
+# rdoc generated
+rdoc
+
+# yard generated
+doc
+.yardoc
+
+# bundler
+.bundle
+
+# jeweler generated
+pkg
+
+# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
+#
+# * Create a file at ~/.gitignore
+# * Include files you want ignored
+# * Run: git config --global core.excludesfile ~/.gitignore
+#
+# After doing this, these files will be ignored in all your git projects,
+# saving you from having to 'pollute' every project you touch with them
+#
+# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
+#
+# For MacOS:
+#
+#.DS_Store
+#
+# For TextMate
+#*.tmproj
+#tmtags
+#
+# For emacs:
+#*~
+#\#*
+#.\#*
+#
+# For vim:
+#*.swp
View
12 Gemfile
@@ -0,0 +1,12 @@
+source "http://rubygems.org"
+
+gem 'json'
+
+group :development do
+ gem "shoulda", ">= 0"
+ gem "bundler", "~> 1.0.0"
+ gem "jeweler", "~> 1.5.1"
+ gem "rcov", ">= 0"
+ gem "fakeweb"
+ gem "rspec"
+end
View
52 Rakefile
@@ -1,15 +1,47 @@
require 'rubygems'
-require 'echoe'
-require 'fileutils'
-require './lib/klout'
+require 'bundler'
+require 'rspec'
+require 'rspec/core'
+require 'rspec/core/rake_task'
+require 'rake/rdoctask'
-Echoe.new 'klout', '0.1.0' do |p|
- p.author = 'Jason Torres'
- p.email = 'jason.e.torres@gmail.com'
- p.url = 'http://github.com/jasontorres/klout'
- p.description = "Klout - Twitter Analytics"
- p.runtime_dependencies = ["typhoeus"]
+begin
+ Bundler.setup(:default, :development)
+rescue Bundler::BundlerError => e
+ $stderr.puts e.message
+ $stderr.puts "Run `bundle install` to install missing gems"
+ exit e.status_code
+end
+require 'rake'
+
+require 'jeweler'
+Jeweler::Tasks.new do |gem|
+ gem.summary = "Ruby library for consuming the Klout API"
+ gem.description = "Klout measures influence on topics across the social web to find the people the world listens to. See http://klout.com for more information about their service"
+ gem.authors = 'Jason Torres'
+ gem.email = 'jason.e.torres@gmail.com'
+ gem.homepage = 'http://github.com/jasontorres/klout'
+ gem.name = "klout"
+ gem.license = "MIT"
+end
+Jeweler::RubygemsDotOrgTasks.new
+RSpec::Core::RakeTask.new(:spec) do |spec|
+ spec.pattern = FileList['spec/**/*_spec.rb']
end
-Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
+RSpec::Core::RakeTask.new(:rcov) do |spec|
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
+end
+
+task :default => :spec
+
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "new_k #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
View
1 VERSION
@@ -0,0 +1 @@
+0.2.0
View
11 examples/examples.rb
@@ -0,0 +1,11 @@
+$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'rubygems'
+require 'klout'
+require 'json'
+
+Klout.api_key = 'YOUR_API_KEY'
+p Klout.score('jsgoecke')
+p Klout.score([ 'jsgoecke', 'chrismatthieu', 'johnnydiggz' ])
+
+p Klout.profile('jsgoecke')
+p Klout.profile([ 'jsgoecke', 'chrismatthieu', 'johnnydiggz' ])
View
73 klout.gemspec
@@ -0,0 +1,73 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE DIRECTLY
+# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{klout}
+ s.version = "0.2.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Jason Torres"]
+ s.date = %q{2010-12-30}
+ s.description = %q{Klout measures influence on topics across the social web to find the people the world listens to. See http://klout.com for more information about their service}
+ s.email = %q{jason.e.torres@gmail.com}
+ s.extra_rdoc_files = [
+ "README"
+ ]
+ s.files = [
+ "Gemfile",
+ "Manifest",
+ "README",
+ "Rakefile",
+ "VERSION",
+ "examples/examples.rb",
+ "lib/klout.rb",
+ "spec/klout_spec.rb",
+ "spec/spec.opts",
+ "spec/spec_helper.rb",
+ "tasks/rspec.rake"
+ ]
+ s.homepage = %q{http://github.com/jasontorres/klout}
+ s.licenses = ["MIT"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.6}
+ s.summary = %q{Ruby library for consuming the Klout API}
+ s.test_files = [
+ "examples/examples.rb",
+ "spec/klout_spec.rb",
+ "spec/spec_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ s.add_runtime_dependency(%q<json>, [">= 0"])
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.1"])
+ s.add_development_dependency(%q<rcov>, [">= 0"])
+ s.add_development_dependency(%q<fakeweb>, [">= 0"])
+ s.add_development_dependency(%q<rspec>, [">= 0"])
+ else
+ s.add_dependency(%q<json>, [">= 0"])
+ s.add_dependency(%q<shoulda>, [">= 0"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
+ s.add_dependency(%q<rcov>, [">= 0"])
+ s.add_dependency(%q<fakeweb>, [">= 0"])
+ s.add_dependency(%q<rspec>, [">= 0"])
+ end
+ else
+ s.add_dependency(%q<json>, [">= 0"])
+ s.add_dependency(%q<shoulda>, [">= 0"])
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
+ s.add_dependency(%q<jeweler>, ["~> 1.5.1"])
+ s.add_dependency(%q<rcov>, [">= 0"])
+ s.add_dependency(%q<fakeweb>, [">= 0"])
+ s.add_dependency(%q<rspec>, [">= 0"])
+ end
+end
+
View
28 lib/klout.rb
@@ -1,5 +1,8 @@
require 'rubygems'
require 'json'
+require 'open-uri'
+require 'net/http'
+
$:.unshift(File.dirname(__FILE__)) unless
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
@@ -19,6 +22,8 @@
class Klout
VERSION = '0.0.1'
+ BASE_URI = 'http://api.klout.com/1'
+
class << self
@@base_host = "klout.com"
@@ -32,20 +37,30 @@ def api_key
@@api_key
end
- def score(username)
- request_uri = "http://klout.com/api/twitter/1/klout/#{@@api_key}/#{username}.json"
+ def score(usernames)
+ request_uri = "#{BASE_URI}/klout.json?key=#{ @@api_key }&users=#{ join_usernames(usernames) }"
return request(request_uri)
end
- def profile(username)
- request_uri = "http://klout.com/api/twitter/1.1/profiledetail/#{@@api_key}/#{username}.json"
+ def profile(usernames)
+ request_uri = "#{BASE_URI}/users/show.json?key=#{ @@api_key }&users=#{ join_usernames(usernames) }"
return request(request_uri)
end
+ private
+
+ def join_usernames(usernames)
+ if usernames.instance_of? Array
+ usernames.join(',')
+ else
+ usernames
+ end
+ end
+
def request(request_uri)
url = URI.parse(request_uri)
response = Net::HTTP.start(url.host, url.port) { |http|
- http.get(url.path)
+ http.get(url.request_uri)
}
case response
@@ -62,8 +77,5 @@ def request(request_uri)
response.error!
end
end
-
-
-
end
end
View
BIN pkg/klout-0.1.0.gem
Binary file not shown.
View
BIN pkg/klout-0.1.0.tar.gz
Binary file not shown.
View
8 pkg/klout-0.1.0/Manifest
@@ -1,8 +0,0 @@
-lib/klout.rb
-Manifest
-Rakefile
-README
-spec/klout_spec.rb
-spec/spec.opts
-spec/spec_helper.rb
-tasks/rspec.rake
View
23 pkg/klout-0.1.0/README
@@ -1,23 +0,0 @@
-Klout measures influence on topics across the social web to find the people the world listens to
-
-See http://klout.com for more information about their service
-
-Install:
-
-sudo gem install klout
-
-Usage and Example
-
->> require 'rubygems'
->> require 'klout'
->> Klout.api_key = "yourkloutapikey"
->> Klout.score('jasontorres')
-=> {"user"=>{"kscore"=>19.74, "status"=>200, "status_message"=>"OK"}}
->> Klout.profile('jasontorres')
- {"user"=>{"score"=>{"slope"=>0, "kscore"=>"19.7429", "kclass"=>"connector", "true_reach"=>"195", "amplification_score"=>"0", "kscore_description"=>"", "network_score"=>"0", "kclass_description"=>"You are a constant source of information to your friends and co-workers. There is a good chance that you probably introduced several of your friends to Twitter. Your taste and opinion is respected and your judgment is trusted.", "date_updated"=>"2009-07-09 00:59:08"}, "twitter_screen_name"=>"jasontorres", "status"=>200, "twitter_id"=>"406073", "status_message"=>"OK"}}
-
-Disclaimer: Author is not anyway involved w/ Klout.com
-
-Klout is a copyright trademark of Klout.com
-
-MIT License
View
15 pkg/klout-0.1.0/Rakefile
@@ -1,15 +0,0 @@
-require 'rubygems'
-require 'echoe'
-require 'fileutils'
-require './lib/klout'
-
-Echoe.new 'klout', '0.1.0' do |p|
- p.author = 'Jason Torres'
- p.email = 'jason.e.torres@gmail.com'
- p.url = 'http://github.com/jasontorres/klout'
- p.description = "Klout - Twitter Analytics"
- p.runtime_dependencies = ["typhoeus"]
-
-end
-
-Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
View
33 pkg/klout-0.1.0/klout.gemspec
@@ -1,33 +0,0 @@
-# -*- encoding: utf-8 -*-
-
-Gem::Specification.new do |s|
- s.name = %q{klout}
- s.version = "0.1.0"
-
- s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
- s.authors = ["Jason Torres"]
- s.date = %q{2010-03-11}
- s.description = %q{Klout - Twitter Analytics}
- s.email = %q{jason.e.torres@gmail.com}
- s.extra_rdoc_files = ["lib/klout.rb", "README", "tasks/rspec.rake"]
- s.files = ["lib/klout.rb", "Manifest", "Rakefile", "README", "spec/klout_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/rspec.rake", "klout.gemspec"]
- s.homepage = %q{http://github.com/jasontorres/klout}
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Klout", "--main", "README"]
- s.require_paths = ["lib"]
- s.rubyforge_project = %q{klout}
- s.rubygems_version = %q{1.3.6}
- s.summary = %q{Klout - Twitter Analytics}
-
- if s.respond_to? :specification_version then
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
- s.specification_version = 3
-
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<typhoeus>, [">= 0"])
- else
- s.add_dependency(%q<typhoeus>, [">= 0"])
- end
- else
- s.add_dependency(%q<typhoeus>, [">= 0"])
- end
-end
View
69 pkg/klout-0.1.0/lib/klout.rb
@@ -1,69 +0,0 @@
-require 'rubygems'
-require 'json'
-$:.unshift(File.dirname(__FILE__)) unless
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
-
-=begin rdoc
-
-Klout measures influence on topics across the social web to find the people the world listens to
-
-See http://klout.com for more information about their service
-
-Usage:
-
-Klout.api_key = ""
-Klout.score('jasontorres')
-
-=end
-
-
-class Klout
- VERSION = '0.0.1'
- class << self
- @@base_host = "klout.com"
-
- @@api_key = ""
-
- def api_key=(api)
- @@api_key = api
- end
-
- def api_key
- @@api_key
- end
-
- def score(username)
- request_uri = "http://klout.com/api/twitter/1/klout/#{@@api_key}/#{username}.json"
- return request(request_uri)
- end
-
- def profile(username)
- request_uri = "http://klout.com/api/twitter/1.1/profiledetail/#{@@api_key}/#{username}.json"
- return request(request_uri)
- end
-
- def request(request_uri)
- url = URI.parse(request_uri)
- response = Net::HTTP.start(url.host, url.port) { |http|
- http.get(url.path)
- }
-
- case response
- when Net::HTTPSuccess
- if response.body
- begin
- JSON.parse(response.body)
- rescue Exception => e
- puts e.backtrace
- false
- end
- end
- else
- response.error!
- end
- end
-
-
-
- end
-end
View
68 pkg/klout-0.1.0/spec/klout_spec.rb
@@ -1,68 +0,0 @@
-require File.dirname(__FILE__) + '/spec_helper.rb'
-
-describe "Klout" do
-
- before do
- Klout.api_key = ""
- end
-
- it "should assign the correct API key" do
- Klout.api_key.should == ""
- end
-
- context "score request" do
- before do
- @score_request ||= lambda {
- Klout.score('jasontorres')
- }
-
- @score_result ||= @score_request.call
- end
-
- it "should score!" do
- @score_result.should be_instance_of(Hash)
- end
-
- it "should have the required keys" do
- @score_result.has_key?('user').should == true
- @score_result['user'].has_key?('kscore').should == true
- @score_result['user'].has_key?('status').should == true
- @score_result['user'].has_key?('status_message').should == true
- end
- end
-
- context "profile request" do
- before do
- @profile_request ||= lambda {
- Klout.profile('jasontorres')
- }
- @profile_result ||= @profile_request.call
- end
-
- it "should have a profile" do
- @profile_result.should be_instance_of(Hash)
- end
-
- it "should have the required keys" do
- @profile_result.has_key?('user').should == true
- @profile_result['user'].has_key?('score').should == true
- @profile_result['user']['score'].has_key?('slope').should == true
- @profile_result['user']['score'].has_key?('kscore').should == true
- @profile_result['user']['score'].has_key?('kclass').should == true
- @profile_result['user']['score'].has_key?('true_reach').should == true
- @profile_result['user']['score'].has_key?('amplification_score').should == true
- @profile_result['user']['score'].has_key?('kscore_description').should == true
- @profile_result['user']['score'].has_key?('network_score').should == true
- @profile_result['user']['score'].has_key?('kclass_description').should == true
- @profile_result['user']['score'].has_key?('date_updated').should == true
- @profile_result['user'].has_key?('twitter_screen_name').should == true
- @profile_result['user'].has_key?('status').should == true
- @profile_result['user'].has_key?('twitter_id').should == true
- @profile_result['user'].has_key?('status_message').should == true
- end
-
- end
-
-
-end
-
View
1 pkg/klout-0.1.0/spec/spec.opts
@@ -1 +0,0 @@
---colour
View
10 pkg/klout-0.1.0/spec/spec_helper.rb
@@ -1,10 +0,0 @@
-begin
- require 'spec'
-rescue LoadError
- require 'rubygems' unless ENV['NO_RUBYGEMS']
- gem 'rspec'
- require 'spec'
-end
-
-$:.unshift(File.dirname(__FILE__) + '/../lib')
-require 'klout'
View
21 pkg/klout-0.1.0/tasks/rspec.rake
@@ -1,21 +0,0 @@
-begin
- require 'spec'
-rescue LoadError
- require 'rubygems' unless ENV['NO_RUBYGEMS']
- require 'spec'
-end
-begin
- require 'spec/rake/spectask'
-rescue LoadError
- puts <<-EOS
-To use rspec for testing you must install rspec gem:
- gem install rspec
-EOS
- exit(0)
-end
-
-desc "Run the specs under spec/models"
-Spec::Rake::SpecTask.new do |t|
- t.spec_opts = ['--options', "spec/spec.opts"]
- t.spec_files = FileList['spec/**/*_spec.rb']
-end
View
46 spec/klout_spec.rb
@@ -1,9 +1,19 @@
require File.dirname(__FILE__) + '/spec_helper.rb'
+FakeWeb.allow_net_connect = false
describe "Klout" do
before do
Klout.api_key = ""
+
+ FakeWeb.register_uri(:get,
+ 'http://api.klout.com/1/klout.json?key=&users=jasontorres',
+ :body => '{"users":[{"kscore":37.65,"twitter_screen_name":"jasontorres"}],"status":200}',
+ :status => ["200", "ok"])
+ FakeWeb.register_uri(:get,
+ 'http://api.klout.com/1/users/show.json?key=&users=jasontorres',
+ :body => "{\"users\":[{\"twitter_screen_name\":\"jasontorres\",\"twitter_id\":\"406073\",\"score\":{\"kscore\":37.65,\"kclass_description\":\"You actively engage in the social web, constantly trying out new ways to interact and network. You're exploring the ecosystem and making it work for you. Your level of activity and engagement shows that you \\\"get it\\\", we predict you'll be moving up.\",\"amplification_score\":17.45,\"slope\":0,\"network_score\":42.58,\"true_reach\":195,\"kscore_description\":\"jasontorres has a low level of influence.\",\"kclass\":\"Explorer\",\"kclass_id\":4,\"description\":\" is effectively using social media to influence their network across a variety of topics.\"}}],\"status\":200}",
+ :status => ["200", "ok"])
end
it "should assign the correct API key" do
@@ -24,10 +34,10 @@
end
it "should have the required keys" do
- @score_result.has_key?('user').should == true
- @score_result['user'].has_key?('kscore').should == true
- @score_result['user'].has_key?('status').should == true
- @score_result['user'].has_key?('status_message').should == true
+ @score_result.has_key?('users').should == true
+ @score_result['users'][0].has_key?('kscore').should == true
+ @score_result['users'][0].has_key?('twitter_screen_name').should == true
+ @score_result.has_key?('status').should == true
end
end
@@ -44,21 +54,19 @@
end
it "should have the required keys" do
- @profile_result.has_key?('user').should == true
- @profile_result['user'].has_key?('score').should == true
- @profile_result['user']['score'].has_key?('slope').should == true
- @profile_result['user']['score'].has_key?('kscore').should == true
- @profile_result['user']['score'].has_key?('kclass').should == true
- @profile_result['user']['score'].has_key?('true_reach').should == true
- @profile_result['user']['score'].has_key?('amplification_score').should == true
- @profile_result['user']['score'].has_key?('kscore_description').should == true
- @profile_result['user']['score'].has_key?('network_score').should == true
- @profile_result['user']['score'].has_key?('kclass_description').should == true
- @profile_result['user']['score'].has_key?('date_updated').should == true
- @profile_result['user'].has_key?('twitter_screen_name').should == true
- @profile_result['user'].has_key?('status').should == true
- @profile_result['user'].has_key?('twitter_id').should == true
- @profile_result['user'].has_key?('status_message').should == true
+ @profile_result.has_key?('users').should == true
+ @profile_result['users'][0].has_key?('score').should == true
+ @profile_result['users'][0]['score'].has_key?('slope').should == true
+ @profile_result['users'][0]['score'].has_key?('kscore').should == true
+ @profile_result['users'][0]['score'].has_key?('kclass').should == true
+ @profile_result['users'][0]['score'].has_key?('true_reach').should == true
+ @profile_result['users'][0]['score'].has_key?('amplification_score').should == true
+ @profile_result['users'][0]['score'].has_key?('kscore_description').should == true
+ @profile_result['users'][0]['score'].has_key?('network_score').should == true
+ @profile_result['users'][0]['score'].has_key?('kclass_description').should == true
+ @profile_result['users'][0].has_key?('twitter_screen_name').should == true
+ @profile_result['users'][0].has_key?('twitter_id').should == true
+ @profile_result.has_key?('status').should == true
end
end
View
15 spec/spec_helper.rb
@@ -1,10 +1,9 @@
-begin
- require 'spec'
-rescue LoadError
- require 'rubygems' unless ENV['NO_RUBYGEMS']
- gem 'rspec'
- require 'spec'
-end
-
$:.unshift(File.dirname(__FILE__) + '/../lib')
+require 'rubygems'
+require 'rspec'
require 'klout'
+require 'fakeweb'
+
+RSpec.configure do |config|
+
+end