1 | cf set-env [appname] [key] [value] |
1 | cf set-env myconf2014 TWITTER_CONSUMER_KEY abcdefghijklmnopqrstuvwxyz |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | ... configure :production do # Get the credentials for the MongoDB Bluemix service env = JSON.parse(ENV["VCAP_SERVICES"])["mongodb-2.2"].first["credentials"] # Connect to MongoDB and authenticate using environment variables conn = MongoClient.new(env["hostname"], env["port"]) db = conn.db(env["db"]) db.authenticate(env["username"], env["password"]) # Store the DB connection in a setting for easy later retrieval set :db, db end |
1 2 3 4 5 6 7 8 | ... configure :development do conn = MongoClient.new("localhost", 27017) db = conn.db("myconf2014") set :db, db end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | ... configure do # Get Mongo collections locations = settings.db.collection("locations") checkins = settings.db.collection("checkins") tweets = settings.db.collection("tweets") if locations.count < 1 baseLocations = [ {"name" => "Main Pavilion", "slug" => "main-pavilion"}, ... {"name" => "Western Zone 2", "slug" => "west-2"} ] locations.insert(baseLocations) end # Connect to Twitter API twitter = Twitter::REST::Client.new do |config| config.consumer_key = ENV["TWITTER_CONSUMER_KEY"] config.consumer_secret = ENV["TWITTER_CONSUMER_SECRET"] config.access_token = ENV["TWITTER_ACCESS_TOKEN"] config.access_token_secret = ENV["TWITTER_ACCESS_TOKEN_SECRET"] end set :locations, locations set :checkins, checkins set :tweets, tweets set :twitter, twitter end |
1 2 3 4 5 6 7 8 9 10 11 | get '/' do # Get 10 most recent checkins checkins = settings.checkins.find.sort(:timestamp => :desc).limit(10).to_a checkins.each do |checkin| # Get location details for the checkin checkin['location'] = settings.locations.find_one(:_id => checkin['location_id']) end haml :index, :locals => {:checkins => checkins} end |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |