Skip to content

Commit

Permalink
Merge branch 'master' into articles
Browse files Browse the repository at this point in the history
Conflicts:
	config/routes.rb
  • Loading branch information
Igor Sutton committed Oct 11, 2011
2 parents ed85537 + 5584601 commit 0dc2ecf
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 98 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Expand Up @@ -8,8 +8,8 @@ gem 'authorization-san', '~> 2.0.1', :require => 'authorization'
gem 'peiji-san', '~> 1.0.0', :require => 'peiji_san'

# These are the versions recommended by Heroku
gem 'thinking-sphinx', :git => 'git://github.com/freelancing-god/thinking-sphinx.git', :branch => 'rails3'
gem 'flying-sphinx', :git => 'git://github.com/flying-sphinx/flying-sphinx.git'
gem 'thinking-sphinx', '~> 2.0.7', :require => 'thinking_sphinx'
gem 'flying-sphinx', '~> 0.6.0', :require => 'flying_sphinx'

gem 'newrelic_rpm'

Expand Down
49 changes: 19 additions & 30 deletions Gemfile.lock
@@ -1,24 +1,3 @@
GIT
remote: git://github.com/flying-sphinx/flying-sphinx.git
revision: 0882ed3
specs:
flying-sphinx (0.5.1)
faraday (~> 0.6.1)
faraday_middleware (~> 0.6.3)
multi_json (~> 1.0.1)
net-ssh (>= 2.0.23)
rash (~> 0.3.0)
thinking-sphinx

GIT
remote: git://github.com/freelancing-god/thinking-sphinx.git
revision: ea7673a
branch: rails3
specs:
thinking-sphinx (2.0.5)
activerecord (>= 3.0.3)
riddle (>= 1.3.3)

GEM
remote: http://rubygems.org/
specs:
Expand Down Expand Up @@ -60,10 +39,17 @@ GEM
faraday (0.6.1)
addressable (~> 2.2.4)
multipart-post (~> 1.1.0)
rack (< 2, >= 1.1.0)
rack (>= 1.1.0, < 2)
faraday_middleware (0.6.5)
faraday (~> 0.6.0)
hashie (1.0.0)
flying-sphinx (0.6.0)
faraday (~> 0.6.1)
faraday_middleware (~> 0.6.3)
multi_json (~> 1.0.1)
net-ssh (>= 2.0.23)
rash (~> 0.3.0)
thinking-sphinx
hashie (1.1.0)
i18n (0.5.0)
json (1.5.2)
mail (2.2.19)
Expand All @@ -74,8 +60,8 @@ GEM
mime-types (1.16)
mocha (0.9.12)
multi_json (1.0.3)
multipart-post (1.1.2)
net-ssh (2.1.4)
multipart-post (1.1.3)
net-ssh (2.2.1)
newrelic_rpm (3.1.0)
oauth (0.4.5)
on-test-spec (0.3.0)
Expand All @@ -102,16 +88,19 @@ GEM
rdoc (~> 3.4)
thor (~> 0.14.4)
rake (0.8.7)
rash (0.3.0)
hashie (~> 1.0.0)
rash (0.3.1)
hashie (~> 1.1.0)
rdoc (3.8)
ri_cal (0.8.8)
riddle (1.3.3)
riddle (1.4.0)
test-spec (0.10.0)
thin (1.2.11)
daemons (>= 1.0.9)
eventmachine (>= 0.12.6)
rack (>= 1.0.0)
thinking-sphinx (2.0.7)
activerecord (>= 3.0.3)
riddle (>= 1.3.3)
thor (0.14.6)
treetop (1.4.8)
polyglot (>= 0.3.1)
Expand All @@ -126,7 +115,7 @@ PLATFORMS

DEPENDENCIES
authorization-san (~> 2.0.1)
flying-sphinx!
flying-sphinx (~> 0.6.0)
mocha
newrelic_rpm
on-test-spec
Expand All @@ -136,5 +125,5 @@ DEPENDENCIES
ri_cal
test-spec
thin
thinking-sphinx!
thinking-sphinx (~> 2.0.7)
twitter_oauth (~> 0.4.3)
29 changes: 12 additions & 17 deletions README.rdoc
Expand Up @@ -11,34 +11,29 @@ First do the regular Rails 3 dance.
$ bundle install
$ rake db:setup

Then configure twitter oauth
Export some environment variables:

$ cp config/initializers/twitter.example.rb config/initializers/twitter.rb
$ open config/initializers/twitter.rb

Fix the consumer credentials. Then configure the meetup API with your key

$ cp config/initializers/meetup.example.rb config/initializers/meetup.rb
$ open config/initializers/meetup.rb

You can get an api key here: http://www.meetup.com/meetup_api/key/.
# You can get Twitter's consumer key and secret here: http://dev.twitter.com/.
$ export TWITTER_CONSUMER_KEY="consumer_key"

# You can get a Meetup api key here: http://www.meetup.com/meetup_api/key/.
$ export TWITTER_CONSUMER_SECRET="consumer_secret"
$ export MEETUP_KEY="meetup_key"

After that install Sphinx

$ brew instal sphinx
$ rake ts:index
$ rake ts:start

Then initialize the application:

$ bundle exec rails s

To import events from all external sources use

$ rake import_events

== Production

Add a cron job which runs once every day that purges outdated classifieds:

0 0 * * * cd /path/to/app && /usr/local/bin/bundle exec /usr/local/rake purge_outdated_classifieds

You also might want to automatically update the Sphinx index

*/5 * * * * cd /path/to/app && /usr/local/bin/bundle exec /usr/local/rake ts:in
Removal of outdated classifieds, updates of Sphinx index and retrieval of new events are done by rake's `cron' task.
33 changes: 21 additions & 12 deletions app/controllers/events_controller.rb
Expand Up @@ -2,18 +2,27 @@ class EventsController < ApplicationController
allow_access :all

def index
now = Time.now
@events = Event.find :all,
:conditions => ["starts_at >= ?", now.beginning_of_day],
:order => 'starts_at',
:limit => 5
from_date = params[:from_date] ? Date.parse(params[:from_date]) : Date.today
to_date = params[:to_date] ? Date.parse(params[:to_date]) : from_date.end_of_week

@events_per_day = @events.inject({}) do |idx, event|
day = event.starts_at.to_date
idx[day] ||= []
idx[day] << event
event.get_geo_coordinates unless event.lat
idx
end
@events = Event.find :all,
:conditions => ["starts_at >= ? AND starts_at <= ?", from_date.beginning_of_day, to_date.end_of_day],
:order => "starts_at"

@events_per_day = @events.inject({}) do |idx, event|
day = event.starts_at.to_date
idx[day] ||= []
idx[day] << event
event.get_geo_coordinates unless event.lat
idx
end

@next_monday = from_date.beginning_of_week + 7

render :index
end

def show
@event = Event.find(params[:id])
end
end
11 changes: 11 additions & 0 deletions app/helpers/events_helper.rb
Expand Up @@ -29,4 +29,15 @@ def linkify(string)
end
end
end

def link_to_event_location event
return "unknown location" unless event.location
url = if event.lat
"http://maps.google.com/maps?z=17&q=loc:#{event.lat}+#{event.lon}"
else
"http://maps.google.com/maps?z=17&q=#{CGI.escape(event.location)}"
end

link_to event.location, url
end
end
35 changes: 24 additions & 11 deletions app/views/events/_event.html.erb
@@ -1,35 +1,48 @@
<% no_map = event.lat.blank? %>

<div class="grid_12 event">
<div class="grid_1 alpha datetime">
<div class="datestamp <%=status_class(event)%>">
<div class="month"><%= event.starts_at.strftime("%b") %></div>
<div class="day"><%= event.starts_at.day %></div>
<div class="day"><%= event.starts_at.strftime("%d") %></div>
</div>

<div class="timestamp <%=status_class(event)%>">
<%= event.starts_at.strftime("%H:%M") %>
</div>
</div>

<div class="map grid_3">
<div id="event_map_<%=event.id%>"></div>
<div class="map grid_3 <%=no_map ? 'no_map' : ''%>">
<div id="event_map_<%=event.id%>">
<%= "no map available" if no_map %>
</div>
</div>

<div class="grid_7 omgega">
<h2 class="eventitle <%=status_class(event)%>"><%= event.name %></h2>
<h2 class="eventitle <%=status_class(event)%>">
<%= link_to event.name, event_url(event.id) %>
<span class="event permalink">
<%= link_to 'Permalink', event_url(event.id) %>
</span>
</h2>
<ul>
<li class="eventdate"><%= time_summary(event) %></li>
<li class="eventlocation"><%= event.location || "unknown location" %></li>
<li class="eventlocation">
<%= link_to_event_location(event) %>
</li>
</ul>
<div class="eventdescription">
<%= linkify(event.description).html_safe %>
<%= auto_link(event.description).html_safe %>
</div>

</div>
</div>

<script>
$(document).ready(function() {
show_map("event_map_<%=event.id%>", <%=event.lat || 'null'%>, <%=event.lon || 'null'%>);
});
</script>
<% unless no_map %>
<script>
$(document).ready(function() {
show_map("event_map_<%=event.id%>", <%=event.lat || 'null'%>, <%=event.lon || 'null'%>);
});
</script>
<% end %>

18 changes: 11 additions & 7 deletions app/views/events/index.html.erb
@@ -1,5 +1,10 @@
<h1>Events in Appsterdam</h1>

<h1>
Events in Appsterdam
<span style="font-size: 18px; vertical-align: middle; font-family: 'Open Sans', helvetica, arial;">
<%= link_to "Current week", events_url %> |
<%= link_to "Week of #{@next_monday}", events_per_week_url(@next_monday) %>
</span>
</h1>

<% @events_per_day.sort_by {|k,v| k}.each do |date, events| %>
<h2 class="timeline"><%= timeline_header(date) %></h2>
Expand All @@ -9,11 +14,10 @@
<div class="grid_12 timelinespacer">&nbsp;</div>
<% end %>


<script type="text/javascript"
<script
src="http://maps.google.com/maps/api/js?sensor=true">
</script>
<script type="text/javascript">
<script>
function show_map(dom_id, lat, lon) {
var coordinates = new google.maps.LatLng(lat, lon);
var options = {
Expand All @@ -26,7 +30,7 @@
var map = new google.maps.Map(document.getElementById(dom_id), options);

var image = '/images/eventlocation.gif';
var beachMarker = new google.maps.Marker({
new google.maps.Marker({
position: coordinates,
map: map,
icon: image
Expand All @@ -46,7 +50,7 @@
map.setMapTypeId('grayscale');

$('#'+dom_id).mouseover(function () {
map.setOptions({disableDefaultUI: false});
map.setOptions({disableDefaultUI: false, mapTypeControl: false});
map.setMapTypeId(google.maps.MapTypeId.ROADMAP);
});

Expand Down
1 change: 1 addition & 0 deletions app/views/events/show.html.erb
@@ -0,0 +1 @@
<%= render @event %>
@@ -1,6 +1,6 @@
# get your meetup API key here: http://www.meetup.com/meetup_api/key/
Appsterdam::Application.meetup_api_options = {
:events_url => "https://api.meetup.com/2/events",
:key => "<<< YOUR API KEY >>>",
:key => ENV["MEETUP_KEY"],
:group_name => "Appsterdam"
}
4 changes: 0 additions & 4 deletions config/initializers/twitter.example.rb

This file was deleted.

4 changes: 4 additions & 0 deletions config/initializers/twitter.rb
@@ -0,0 +1,4 @@
Appsterdam::Application.twitter_options = {
:consumer_key => ENV["TWITTER_CONSUMER_KEY"],
:consumer_secret => ENV["TWITTER_CONSUMER_SECRET"]
}
4 changes: 4 additions & 0 deletions config/routes.rb
Expand Up @@ -7,6 +7,10 @@
match '/articles/mine' => 'articles#index', :as => :my_articles, :show => :mine

resources :articles

match '/events/:from_date/:to_date' => 'events#index', :as => :filter_events
match '/events/:from_date' => 'events#index', :as => :events_per_week, :constraints => { :from_date => /\d{4}-\d{2}-\d{2}/ }

resources :classifieds
resources :commands
resources :members do
Expand Down
15 changes: 6 additions & 9 deletions lib/meetup.rb
Expand Up @@ -19,19 +19,16 @@ def get_events

@log.info "got #{events.size} Meetup events"
events
end

private
end

def get_events_json
response = open meetup_api_url
response = open self.meetup_api_url
data = JSON.parse(response.read)
data['results']

rescue URI::InvalidURIError
@log.fatal "invalid meetup api url: #{self.meetup_api_url}"
rescue OpenURI::HTTPError
@log.fatal "unable to retreive meetup data (http error) from url #{self.meetup_api_url}"
rescue URI::InvalidURIError
@log.fatal "invalid meetup api url: #{self.meetup_api_url}"
rescue OpenURI::HTTPError
@log.fatal "unable to retreive meetup data (http error) from url #{self.meetup_api_url}"
end

def meetup_api_url
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/cron.rake
@@ -0,0 +1,5 @@
desc "This task is called by the Heroku cron add-on"
task :cron => :environment do
log = Logger.new(STDOUT)
Event.sync_events false, log
end

0 comments on commit 0dc2ecf

Please sign in to comment.