Permalink
Browse files

Updated codebase to use new Twitter OAuth mechanism.

  • Loading branch information...
1 parent 6df3e8d commit 4bc731dce76cd69a1a1eb4d93ec7712e51cc76c8 @sflinter committed Dec 23, 2010
Showing with 40 additions and 18 deletions.
  1. +1 −0 .gitignore
  2. +15 −1 application.rb
  3. +5 −2 config/config.yml
  4. +2 −0 environment.rb
  5. +17 −15 lib/tweeter.rb
View
@@ -1,2 +1,3 @@
tmp
log
+.DS_Store
View
@@ -1,6 +1,7 @@
require 'environment'
require 'sinatra'
require 'builder'
+require 'twitter_oauth'
require 'haml'
require 'tweeter'
require 'geonames'
@@ -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
@@ -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|
@@ -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
View
@@ -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
View
@@ -9,6 +9,8 @@
require 'sinatra' unless defined?(Sinatra)
configure do
+ set :sessions, true
+
SiteConfig = OpenStruct.new(
:title => 'SemanticTweet',
:author => 'Steve Flinter',
View
@@ -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
@@ -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

0 comments on commit 4bc731d

Please sign in to comment.