-
Notifications
You must be signed in to change notification settings - Fork 5
/
trippy.rb
67 lines (55 loc) · 1.85 KB
/
trippy.rb
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
$:.unshift *Dir[File.dirname(__FILE__) + "/vendor/*/lib"]
require 'rubygems'
require 'active_record'
require 'delayed_job'
require 'sinatra'
require 'haml'
require 'logger'
def log_loc
loc = Sinatra::Application.environment.to_s == "production" ? "log/production.log" : STDOUT
end
LOG = Logger.new(log_loc)
require File.expand_path("../lib/reader.rb", __FILE__)
configure do
config = YAML::load(File.open('config/database.yml'))
environment = ENV['ENV'] || Sinatra::Application.environment.to_s
ActiveRecord::Base.logger = Logger.new($stdout)
ActiveRecord::Base.establish_connection(
config[environment]
)
end
get '/' do
haml :index
end
get '/articles' do
redirect '/'
end
post '/articles' do
@origin = params[:origin]
@destination = params[:destination]
@twitter_account = params[:twitter_account]
@hash = Digest::MD5.hexdigest("#{Time.now.to_i}#{rand(1000)}")
@activity = (params[:commute] && params[:commute] == "on" ? nil : params[:activities] )
@geo = (params[:geolat] == "" ? nil : [params[:geolat],params[:geolong]])
Delayed::Job.enqueue ArticleJob.new(@origin, @destination, @hash, @twitter_account, @activity, @geo)
@msg = "processing"
haml :index
end
get '/articles_ready/:hash' do
content_type :json
@hash = params[:hash]
LOG.info File.expand_path("../public/articles/#{@hash}.json",__FILE__)
if File.exists?(File.expand_path("../public/articles/#{@hash}.json",__FILE__))
json = File.open(File.expand_path("../public/articles/#{@hash}.json",__FILE__),"r").read
@articles = Crack::JSON.parse(json).to_json
FileUtils.rm_r(File.expand_path("../public/articles/#{@hash}.json",__FILE__), :force => true)
else
error = check_job_status
if error
@articles = {:msg => "error", :articles => []}.to_json
else
@articles = {:msg => "not_ready", :articles => []}.to_json
end
end
@articles
end