Skip to content

Instantly share code, notes, and snippets.

@jbowles
Created September 6, 2012 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbowles/3658518 to your computer and use it in GitHub Desktop.
Save jbowles/3658518 to your computer and use it in GitHub Desktop.
Bad web server file example for learntoprogram course at One on One
require File.join(File.dirname(__FILE__), 'lib','new_class.rb')
has_many :hadoop_logs
def self.twitter_creds
tconfig_file = Rails.root.join 'config', 'twitter_oauth.yml'
NimbleTwitter.twitter_config(NimbleTwitter.dev_twitter("#{tconfig_file}"))
end
def self.json_encoder
Yajl::Encoder.new
end
def self.embedded_user_stream
HashWithIndifferentAccess.new
end
def self.call_serial_stream
twitter_creds
TweetStream::Client.new.sample do |tts|
@stream = Stream.new
# Need to pull off ids as strings so json encoding is nice and neat
# The 'possibly_sensitive' field causes problems (it's a string but
# sometimes only the first letter saves, other times the full string
# 'false/true')
mt_stream = HashWithIndifferentAccess.new
mt_stream[:language_code] = tts.user.lang
mt_stream[:message_text] = tts.text
mt_stream[:profile_image_url] = tts.user.profile_image_url
mt_stream[:username] = tts.user.name
mt_stream[:screen_name] = tts.user.screen_name
mt_stream[:user_id] = tts.user[:id_str]
mt_stream[:tweet_id] = tts[:id_str]
## @stream.taboo, mt_stream[:taboo] = tts.possibly_sensitive
@stream.language_code = tts.user.lang
@stream.message_text = tts.text
@stream.profile_image_url = tts.user.profile_image_url
@stream.username = tts.user.name
@stream.screen_name = tts.user.screen_name
@stream.user_id = tts.user.id
@stream.stream_type_id = 1
@stream.occurrent_type_id = 1
#TODO: raise "Hash for storing meta stream must be complete, #{mt_stream.inspect}" if mt_stream.each_value{|v| v.blank? }
# Transaction serialization
serial_meta_stream = json_encoder.encode(mt_stream)
@stream.meta_stream = serial_meta_stream
if @stream.save!
Rails.logger.debug "#{@stream.id} saved"
Resque.enqueue(StreamJob, @stream.id)
else
Rails.logger.error "Something has gone wrong: #{@stream.inspect}"
end
end
end
def self.run_event(increment,baseline,runtime,stream_name='call_serial_stream')
EventMachine::run {
incr_start = increment
@incr_stop = baseline + (incr_start/runtime)
@incr_count = baseline
@timer = EventMachine::PeriodicTimer.new(incr_start) {
@incr_count += 1
eval "#{stream_name}"
Rails.logger.info "----- In timer block: EM peridoic timer=#{incr_start}; Start=#{@incr_count}, end=#{@incr_stop}"
if @incr_count == @incr_stop
@timer.cancel
#EventMachine.stop
end
}
Rails.logger.info "**** In EM run block: #{Time.now}"
}
Rails.logger.info "Out of EM block: #{Time.now}"
end
tell_me = NewClass.tell_me_something
rs = nc.randomize_select
app = Proc.new do |env|
response = []
response << 200
response << { 'Content-Type' => 'text/plain'}
response << ["Here is our class: #{nc}.\n Here is our method call from the class: #{tell_me}.\n And check this out: #{rs}"]
end
run app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment