0

这是我编写的 Sinatra 代码。所有 gem 都存在,ruby 文件可以完美编译,但是当我转到 localhost:4567/ 时,sinatra 应用程序没有运行。它把我带到“Sinatra 不知道这个小曲子”页面。我在这里犯了什么错误?是语法问题吗?我已经在这里发布了主要的 ruby​​ 文件的代码,其他的只是 haml 文件,仅此而已。

require 'bundler'
Bundler.setup(:default)

require 'sinatra'
require 'haml'
require 'twitter'
require 'oauth'

class MyTweetWeek < Sinatra::Base
  set :haml, :format => :html5, :attr_wrapper => '"'
 enable :sessions, :static, :raise_errors
 set :public_dir, File.join(File.dirname(__FILE__), 'public')

 get '/' do
   haml :index
 end

 get '/login' do
request_token = consumer.get_request_token(:oauth_callback => ENV['OAUTH_CALLBACK'])

session[:request_token] = request_token.token
session[:request_token_secret] = request_token.secret

redirect request_token.authorize_url
 end

  get '/oauth_callback' do
request_token = OAuth::RequestToken.new(
  consumer,
  session[:request_token],
  session[:request_token_secret]
)

session[:request_token] = session[:request_token_secret] = nil

access_token = request_token.get_access_token(:oauth_verifier => params[:oauth_verifier])

session[:access_token] = access_token.token
session[:access_secret] = access_token.secret

redirect '/resume'
 end

  get '/resume' do
redirect '/'  unless authenticated?
today = Date.today #get today's date
monday = today - today.cwday + 1 #calculate Monday
search = Twitter::Search.new

@screen_name = client.verify_credentials.screen_name
@number_of_tweets = 0
@number_of_mentions = 0

results = search.from(@screen_name)
                .since_date(monday)
                .no_retweets
                .per_page(100)
                .fetch

@number_of_tweets += results.size

while search.next_page?
  results = search.fetch_next_page
  @number_of_tweets += results.size
end

search.clear

results = search.q("@#{@screen_name.gsub('@', '')}")
                .since_date(monday)
                .no_retweets
                .per_page(100)
                .fetch

@number_of_mentions += results.size

while search.next_page?
  results = search.fetch_next_page
  @number_of_mentions += results.size
end

haml :resume
 end

 error Twitter::Error::Unauthorized do
    redirect '/'
    end

 not_found do
  haml :not_found
  end

 private

 def consumer
   @consumer ||= OAuth::Consumer.new(
    ENV['CONSUMER_KEY'],
    ENV['CONSUMER_SECRET'],
    :site => "https://api.twitter.com"
    )
 end

  def client
   Twitter.configure do |config|
    config.consumer_key = ENV['CONSUMER_KEY']
    config.consumer_secret = ENV['CONSUMER_SECRET']
    config.oauth_token =  session[:access_token]
    config.oauth_token_secret = session[:access_secret]
    end

@client ||= Twitter::Client.new
end

 def authenticated?
  !session[:access_token].nil? && !session[:access_secret].nil?
    end
    end
4

2 回答 2

1

由于您有一个模块化应用程序,您是否需要“sinatra/base”而不是“sinatra”?看这里

于 2013-04-10T11:57:24.740 回答
0

请参阅Serving a Modular Apprun! if app_file == $0并在课程末尾添加该行。另请参阅 DavB 的答案。

于 2013-04-10T12:10:44.400 回答