Skip to content

Commit

Permalink
Merge pull request #10 from frommww/master
Browse files Browse the repository at this point in the history
Added clarity to MongoDB and IndexTank examples
  • Loading branch information
frommww committed May 17, 2011
2 parents 3917204 + 2669520 commit a89a42f
Show file tree
Hide file tree
Showing 13 changed files with 443 additions and 123 deletions.
16 changes: 16 additions & 0 deletions lib/klout.com/config.yml
@@ -0,0 +1,16 @@
sw_access_key: ABC
sw_secret_key: XYZ

klout_api_key: ABC
twitter_usernames: ["simpleworker", "chadarimura", "klout", "techcrunch", "monkchips", "cote", "sgblank", "venturehacks", "nulltweet12345"]

aws_access_key: ABC
aws_secret_key: XYZ

mongo_host: somehostname.mongohq.com
mongo_port: somenumber
mongo_username: ABC
mongo_password: XYZ
mongo_db_name: yourdbname


47 changes: 47 additions & 0 deletions lib/klout.com/klout_hello_worker.rb
@@ -0,0 +1,47 @@
#--
# SimpleWorker.com
# Developer: Roman Kononov / Ken Fromm
#
# Klout_HelloWorker is a simple example of how to connect a worker to the Klout API.
#
# There is a klout.rb gem that has some nice methods and error checking but this
# makes the API call directly so you can see the formats.
#
# A set of usernames can be obtained by changing the API param to :users=>twitter_usernames.join(",")
# Of course, you'd want to take out or modify the twitter_usernames do loop.
#
#++

require 'simple_worker'
require 'json'
require 'open-uri'
require 'rest-client'

class KloutHelloWorker < SimpleWorker::Base

attr_accessor :klout_api_key, :twitter_usernames

def run
log "Running Klout HelloWorker"

twitter_usernames.each do |username|
begin
log "Processing username #{username}"

# Call the Klout API
response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => klout_api_key, :users=>username}}
parsed = JSON.parse(response)

daily_score = parsed["users"][0]["kscore"] if parsed["users"] && parsed["users"][0]

log "Processing: #{username} Score: #{daily_score}"

rescue =>ex
puts "EXCEPTION #{ex.inspect}"
end
end

log "Done with Klout HelloWorker!"
end

end
51 changes: 51 additions & 0 deletions lib/klout.com/klout_hello_worker_runner.rb
@@ -0,0 +1,51 @@
#--
# Klout_Hello_Worker_Runner
# Developer: Roman Kononov / Ken Fromm
#
# TO USE:
# 1) Get accounts/credentials for Klout and SimpleWorker
#
# 2) Create a file in this directory called config.yml with the following parameters:
# (Note: Your keys will be different than below. Get the right values from each service.)
#
# sw_access_key: ABC
# sw_secret_key: XYZ
# klout_api_key: ABC
# twitter_usernames: ["simpleworker", "klout", "techcrunch"]
#

require 'simple_worker'
require_relative 'klout_hello_worker'

@config = YAML.load_file('config.yml')
p @config

SimpleWorker.configure do |config|
config.access_key = @config["sw_access_key"]
config.secret_key = @config["sw_secret_key"]
end

# Create the worker and set some attributes
khw = KloutHelloWorker.new
khw.klout_api_key = @config["klout_api_key"]
khw.twitter_usernames = @config["twitter_usernames"]


# Run the job (with several alternatives included)
#
#khw.run_local
#khw.schedule(:start_at => 2.minutes.since, :run_every => 60, :run_times => 2)
#khw.queue(:priority => 2)

khw.queue


# This gets stats on the job after completion. You can see the same in the
# SimpleWorker dashboard.
#
# Note that wait_until_complete has issues with run_local and schedule.
# Included for use with .queue so that we can display the log file.
#
status = khw.wait_until_complete
p status
puts khw.get_log
71 changes: 71 additions & 0 deletions lib/klout.com/klout_mongo_worker.rb
@@ -0,0 +1,71 @@
#--
# KloutMongoWorker is a simple example of how the Klout API can be called from a Worker and the results
# stored into a MongoHQ DB (using Mongoid). An example of this would be to go through the user base
# on a nightly basis, get the Klout score, and store it back into your own database.
#
#++

require 'simple_worker'
require 'json'
require 'open-uri'
require 'rest-client'

class KloutMongoWorker < SimpleWorker::Base

merge File.join(File.dirname(__FILE__), "user_klout_mongo_stat.rb")
attr_accessor :klout_api_key, :twitter_usernames,
:mongo_db_name, :mongo_host, :mongo_port, :mongo_username, :mongo_password

def run
log "Running Klout MongoWorker"

init_mongohq

# We only want to store the Klout for each user once per day - so this allows us to check
today = Time.now.utc.at_beginning_of_day

# Iterate through the usernames passed into the worker from the runner (or your app)
# One optimization is to send the set of usernames (change param to :users=>twitter_usernames.join(","))
# and adjust the loop to work on the returned set.

twitter_usernames.each do |username|
begin
# Check if there's a current score already set for today
if daily_score = UserKloutMongoStat.first(conditions: {username: username, for_date: today})
log "Existing daily score of #{daily_score.username}: #{daily_score.score}"
else
# If a daily score record doesn't exist, call the Klout API and create one
response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => klout_api_key, :users=>username}}
parsed = JSON.parse(response)

daily_score = UserKloutMongoStat.new(:username => username, :for_date => today, :score => 0)
daily_score.score = parsed["users"][0]["kscore"] if parsed["users"] && parsed["users"][0]
daily_score.save

log "New daily score of #{daily_score.username}: #{daily_score.score}"
end

rescue =>ex
# If no username, .RestClient.get will return: 404 Resource Not Found.
log "EXCEPTION #{ex.inspect}"
end
end

log "Finishing Klout MongoWorker!"
end

# Configures settings for MongoDB. Values for mongo_host and mongo_port passed in to
# make the example easy to understand. Could be placed directly inline to streamline.
def init_mongohq
Mongoid.configure do |config|
config.database = Mongo::Connection.new(mongo_host, mongo_port).db(mongo_db_name)
config.database.authenticate(mongo_username, mongo_password)
# config.slaves = [
# Mongo::Connection.new(host, 27018, :slave_ok => true).db(name)
# ]
config.persist_in_safe_mode = false
end
end

end

68 changes: 68 additions & 0 deletions lib/klout.com/klout_mongo_worker_runner.rb
@@ -0,0 +1,68 @@
#
# This is a mashup of the Klout HelloWorker example and the MongoWorker example.
#
# TO USE:
# 1) Get accounts/credentials for Klout and SimpleWorker
#
# 2) Create a file in this directory called config.yml with the following parameters:
# (Note: Your keys and values will be different than below. Get the right values from each service.)
#
# sw_access_key: ABC
# sw_secret_key: XYZ
# klout_api_key: ABC
# twitter_usernames: ["simpleworker", "klout", "techcrunch"]
# mongo_host: somehostname.mongohq.com
# mongo_port: somenumber
# mongo_username: ABC
# mongo_password: XYZ
# mongo_db_name: yourdbname
#


require 'simple_worker'

# Require_relative on the class name will also work
load "klout_mongo_worker.rb"

@config = YAML.load_file('config.yml')
p @config

SimpleWorker.configure do |config|
config.access_key = @config["sw_access_key"]
config.secret_key = @config["sw_secret_key"]
end




# Create the worker and set some attributes
kmw = KloutMongoWorker.new
kmw.klout_api_key = @config["klout_api_key"]
kmw.twitter_usernames = @config["twitter_usernames"]

kmw.mongo_db_name = @config["mongo_db_name"]
kmw.mongo_host = @config["mongo_host"]
kmw.mongo_port = @config["mongo_port"]
kmw.mongo_username = @config['mongo_username']
kmw.mongo_password = @config['mongo_password']


# Run the job (with several alternatives included)
#
#kmw.run_local
#kmw.schedule(:start_at => 2.minutes.since, :run_every => 60, :run_times => 2)
#kmw.queue(:priority=>2)

kmw.queue


# This gets stats on the job after completion. You can see the same in the
# SimpleWorker dashboard.
#
# Note that wait_until_complete has issues with run_local and schedule.
# Included for use with .queue so that we can display the log file.
#
status = kmw.wait_until_complete
p status
puts kmw.get_log

69 changes: 69 additions & 0 deletions lib/klout.com/klout_simpledb_worker.rb
@@ -0,0 +1,69 @@
#--
# Copyright (c) 2011 SimpleWorker.com
# Developer: Roman Kononov
#
# KloutSimpleDBWorker is a simple example of how the Klout API can be called from a Worker and the results
# stored into a SimpleDB domain (using SimpleRecord). An example of this would be to
# go through your entire user base on a nightly basis, get the Klout score, and store it
# back into your own database.
#
# This example can be modified and optimized as you see fit.
#
#++

require 'simple_worker'
require 'simple_record'
require 'json'
require 'open-uri'
require 'rest-client'

class KloutSimpleDBWorker < SimpleWorker::Base

merge File.join(File.dirname(__FILE__), "user_klout_stat.rb")
attr_accessor :klout_api_key, :twitter_usernames,
:aws_access_key, :aws_secret_key, :sdb_prefix

def run
log "Running Klout SimpleDBWorker"

init_simpledb

# We only want to store the Klout for each user once per day - so this allows us to check
today = Time.now.utc.at_beginning_of_day

# Iterate through the usernames passed into the worker from the runner (or your app)
twitter_usernames.each do |username|
begin
# Call the Klout API
response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => klout_api_key, :users=>username}}
parsed = JSON.parse(response)

# Check if there's a current score already set for today
if daily_score = UserKloutStat.find(:first, :conditions=>["username=? and for_date=?", username, today])
log "Existing daily score of #{daily_score.username}: #{daily_score.score}"
else
# Create if no score today
daily_score = UserKloutStat.new(:username => username, :for_date => today, :score => 0) unless daily_score
daily_score.score = parsed["users"][0]["kscore"] if parsed["users"] && parsed["users"][0]
daily_score.save

log "New daily score of #{daily_score.username}: #{daily_score.score}"
end
rescue =>ex
log "EXCEPTION #{ex.inspect}"
end
end

UserKloutStat.close_connection

log "Finishing Klout SimpleDBWorker!"
end

# Establish connection to SimpleDB
def init_simpledb
SimpleRecord.establish_connection(aws_access_key, aws_secret_key, :connection_mode=>:per_thread)
SimpleRecord::Base.set_domain_prefix("sample_worker_dev_")
end

end

60 changes: 60 additions & 0 deletions lib/klout.com/klout_simpledb_worker_runner.rb
@@ -0,0 +1,60 @@

#
# TO USE:
# 1) Get accounts/credentials for Klout, SimpleWorker, and AWS/SimpleDB
#
# 2) Create a file in this directory called config.yml with the following parameters:
# (Note: Your keys will be different than below. Get the right values from each service.)
#
# sw_access_key: ABC
# sw_secret_key: XYZ
# klout_api_key: ABC
# twitter_usernames: ["simpleworker", "klout", "techcrunch"]
# aws_access_key: ABC
# aws_secret_key: XYZ
#

require 'simple_worker'
require 'json'
require 'open-uri'
require 'rest-client'

# Require_relative on the class name will also work
load "klout_simpledb_worker.rb"

@config = YAML.load_file('config.yml')
p @config

SimpleWorker.configure do |config|
config.access_key = @config["sw_access_key"]
config.secret_key = @config["sw_secret_key"]
end

# Create the worker and set some attributes
ksw = KloutSimpleDBWorker.new
ksw.klout_api_key = @config["klout_api_key"]
ksw.twitter_usernames = @config["twitter_usernames"]

ksw.aws_access_key = @config["aws_access_key"]
ksw.aws_secret_key = @config["aws_secret_key"]


# Run the job (with several alternatives included)
#
#ksw.run_local
#ksw.schedule(:start_at => 2.minutes.since, :run_every => 60, :run_times => 2)
#ksw.queue(:priority=>2)

ksw.queue



# This gets stats on the job after completion. You can see the same in the
# SimpleWorker dashboard.
#
# Note that wait_until_complete has issues with run_local and schedule.
# Included for use with .queue so that we can display the log file.
#
status = ksw.wait_until_complete
p status
puts ksw.get_log

0 comments on commit a89a42f

Please sign in to comment.