Skip to content

Commit

Permalink
Updated codebase to use new Twitter OAuth mechanism.
Browse files Browse the repository at this point in the history
  • Loading branch information
sflinter committed Dec 23, 2010
1 parent 6df3e8d commit 4bc731d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
tmp
log
.DS_Store
16 changes: 15 additions & 1 deletion application.rb
@@ -1,6 +1,7 @@
require 'environment'
require 'sinatra'
require 'builder'
require 'twitter_oauth'
require 'haml'
require 'tweeter'
require 'geonames'
Expand All @@ -23,6 +24,16 @@ def valid_uri?(uri = "")
set :views, "#{File.dirname(__FILE__)}/views"
end

before do
next if request.path_info =~ /ping$/
@client = TwitterOAuth::Client.new(
:consumer_key => ENV['CONSUMER_KEY'] || APP_CONFIG[:twitter][:consumer_key],
:consumer_secret => ENV['CONSUMER_SECRET'] || APP_CONFIG[:twitter][:consumer_secret],
:token => APP_CONFIG[:twitter][:access_token],
:secret => APP_CONFIG[:twitter][:access_token_secret]
)
end

get '/' do
haml :index
end
Expand All @@ -35,6 +46,9 @@ def valid_uri?(uri = "")
haml :contact
end

get '/favicon.ico' do
end

# Note the formulation of a specific regexp is required here. The
# preferred option of '/tag/:tags' doesn't match all URIs properly
get %r{\A/tags/(.*)} do |tags|
Expand All @@ -48,7 +62,7 @@ def valid_uri?(uri = "")
end

get '/:screen_name/:who' do
@tweeter = Tweeter.new(params[:screen_name], params[:who])
@tweeter = Tweeter.new(@client, params[:screen_name], params[:who])
if @tweeter.exists?
content_type :rdf
builder :foaf
Expand Down
7 changes: 5 additions & 2 deletions config/config.yml
Expand Up @@ -8,8 +8,11 @@ development: &non_production_settings
:base_uri: api.twitter.com
:search_uri: http://search.twitter.com
:api_version: 1
:basic_auth_username: semantictweet
:basic_auth_password: secret
:consumer_key: REPLACE
:consumer_secret: REPLACE
:callback_url: REPLACE
:access_token: REPLACE
:access_token_secret: REPLACE

:geonames:
:base_uri: ws.geonames.org
Expand Down
2 changes: 2 additions & 0 deletions environment.rb
Expand Up @@ -9,6 +9,8 @@
require 'sinatra' unless defined?(Sinatra)

configure do
set :sessions, true

SiteConfig = OpenStruct.new(
:title => 'SemanticTweet',
:author => 'Steve Flinter',
Expand Down
32 changes: 17 additions & 15 deletions lib/tweeter.rb
@@ -1,27 +1,24 @@
require 'httparty'
require 'uri'
require 'pp'
require 'geonames'

class Tweeter
include Comparable
include HTTParty
base_uri APP_CONFIG[:twitter][:base_uri]
basic_auth APP_CONFIG[:twitter][:basic_auth_username], APP_CONFIG[:twitter][:basic_auth_password]

attr_reader :foafs, :geoname

def initialize(tweeter, who = 'friends')
def initialize(client, tweeter, who = 'friends')
@foafs = []
@client = client

case tweeter
when String
@given_screen_name = URI.escape(tweeter)
puts "@given_screen_name: #{@given_screen_name}"
@tweeter = self.show
if self.exists?
foafs = self.who(who)
@foafs = foafs.map { |foaf| Tweeter.new(foaf) }
@geoname = GeoNames.new(@tweeter['location']) unless @tweeter['location'].blank?
@foafs = foafs.map { |foaf| Tweeter.new(@client, foaf) }
# @geoname = GeoNames.new(@tweeter['location']) unless @tweeter['location'].blank?
end
when Hash
@tweeter = tweeter
Expand All @@ -32,26 +29,31 @@ def initialize(tweeter, who = 'friends')
@tweeter['url'] = '' unless @tweeter['url']
end

def exists(resp)
@exists = resp['error'] != 'Not found'
end

def exists?
@exists
end

def show
puts "Calling: #{APP_CONFIG[:twitter][:base_uri]}/#{APP_CONFIG[:twitter][:api_version]}/users/show.json?screen_name=#{@given_screen_name}"
resp = self.class.get("/#{APP_CONFIG[:twitter][:api_version]}/users/show.json?screen_name=#{@given_screen_name}")
@exists = resp.code.between?(200,299)
puts "Calling @client.show(#{@given_screen_name})"
resp = @client.show(@given_screen_name)
exists(resp)
resp
end

def friends
puts "Calling: #{APP_CONFIG[:twitter][:base_uri]}/#{APP_CONFIG[:twitter][:api_version]}/statuses/friends/#{@given_screen_name}.json"
resp = self.class.get("/#{APP_CONFIG[:twitter][:api_version]}/statuses/friends/#{@given_screen_name}.json")
puts "Calling @client.friends"
resp = @client.friends
# puts resp.to_yaml
resp
end

def followers
puts "Calling: #{APP_CONFIG[:twitter][:base_uri]}/#{APP_CONFIG[:twitter][:api_version]}/statuses/followers/#{@given_screen_name}.json"
self.class.get("/#{APP_CONFIG[:twitter][:api_version]}/statuses/followers/#{@given_screen_name}.json")
puts "Calling @client.followers"
resp = @client.followers
end

def all
Expand Down

0 comments on commit 4bc731d

Please sign in to comment.