Skip to content

Commit

Permalink
added support for gelocation in feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
iobridge committed Mar 27, 2011
1 parent 93a51bd commit 7e964af
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 23 deletions.
40 changes: 35 additions & 5 deletions app/controllers/channels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def index

def show
@channel = Channel.find(params[:id]) if params[:id]
@domain = domain

# if owner of channel
get_channel_data if current_user and @channel.user_id == current_user.id
Expand All @@ -28,6 +29,10 @@ def update
@channel.update_attributes(params[:channel])
@channel.name = "#{t(:channel_default_name)} #{@channel.id}" if params[:channel][:name].empty?
@channel.save

# save tags
@channel.save_tags(params[:tags][:name])

redirect_to channel_path(@channel.id) and return
end

Expand Down Expand Up @@ -59,6 +64,20 @@ def create
redirect_to edit_channel_path(@channel.id)
end

# clear all data from a channel
def clear
channel = Channel.find(params[:id])
# make sure channel belongs to current user
check_permissions(channel)

# do the delete
channel.feeds.each do |f|
f.delete
end

redirect_to channels_path
end

def destroy
@channel = Channel.find(params[:id])
# make sure channel belongs to current user
Expand All @@ -73,13 +92,16 @@ def destroy
def post_data
status = '0'
feed = Feed.new

api_key = ApiKey.find_by_api_key(get_userkey)

# if write persmission, allow post
if (api_key && api_key.write_flag)
channel = Channel.find(api_key.channel_id)

# rate limit posts
render :text => '0' and return if (Time.now < channel.updated_at + 14.seconds)

# update entry_id for channel and feed
entry_id = channel.last_entry_id.nil? ? 1 : channel.last_entry_id + 1
channel.last_entry_id = entry_id
Expand All @@ -88,15 +110,18 @@ def post_data
# try to get created_at datetime if appropriate
if params[:created_at]
begin
@feed.created_at = DateTime.parse(params[:created_at])
feed.created_at = DateTime.parse(params[:created_at])
# if invalid datetime, don't do anything--rails will set created_at
rescue
end
end

# strip line feeds from end of parameters
# modify parameters
params.each do |key, value|
params[key] = value.sub(/\\n$/, '').sub(/\\r$/, '')
# strip line feeds from end of parameters
params[key] = value.sub(/\\n$/, '').sub(/\\r$/, '') if value
# use ip address if found
params[key] = request.remote_addr if value.upcase == 'IP_ADDRESS'
end

# set feed details
Expand All @@ -111,6 +136,11 @@ def post_data
feed.field7 = params[:field7] if params[:field7]
feed.field8 = params[:field8] if params[:field8]
feed.status = params[:status] if params[:status]
feed.latitude = params[:lat] if params[:lat]
feed.latitude = params[:latitude] if params[:latitude]
feed.longitude = params[:long] if params[:long]
feed.longitude = params[:longitude] if params[:longitude]
feed.elevation = params[:elevation] if params[:elevation]

if channel.save && feed.save
status = entry_id
Expand All @@ -122,4 +152,4 @@ def post_data
render :text => status
end

end
end
12 changes: 11 additions & 1 deletion app/controllers/feed_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class FeedController < ApplicationController
require 'csv'
layout 'application', :except => :index

def index
channel = Channel.find(params[:channel_id])
Expand All @@ -8,6 +9,7 @@ def index

# check for access
if @success

# create options hash
channel_options = { :only => channel_select_data(channel) }
select_options = feed_select_data(channel)
Expand All @@ -17,7 +19,7 @@ def index
:all,
:conditions => { :channel_id => channel.id, :created_at => get_date_range(params) },
:select => select_options,
:order => 'created_at'
:order => 'created_at desc'
)

# if a feed has data
Expand Down Expand Up @@ -159,6 +161,13 @@ def feed_select_data(channel)
only += [:field8] unless channel.field8.blank? or (params[:field_id] and params[:field_id] != '8')
only += [:status] if params[:status] and params[:status].upcase == 'TRUE'

# add geolocation data if necessary
if params[:location] and params[:location].upcase == 'TRUE'
only += [:latitude]
only += [:longitude]
only += [:elevation]
end

return only
end

Expand All @@ -171,6 +180,7 @@ def timeparam_valid?(timeparam)
return false
end
end

# slice feed into timescales
def feeds_into_timescales(feeds)
# convert timescale (minutes) into seconds
Expand Down
46 changes: 29 additions & 17 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
t.string "description"
t.decimal "latitude", :precision => 15, :scale => 10
t.decimal "longitude", :precision => 15, :scale => 10
t.text "field1"
t.text "field2"
t.text "field3"
t.text "field4"
t.text "field5"
t.text "field6"
t.text "field7"
t.text "field8"
t.string "field1"
t.string "field2"
t.string "field3"
t.string "field4"
t.string "field5"
t.string "field6"
t.string "field7"
t.string "field8"
t.integer "scale1"
t.integer "scale2"
t.integer "scale3"
Expand All @@ -52,26 +52,38 @@
t.string "elevation"
t.integer "last_entry_id"
t.boolean "public_flag", :default => false
t.string "options1"
t.string "options2"
t.string "options3"
t.string "options4"
t.string "options5"
t.string "options6"
t.string "options7"
t.string "options8"
end


create_table "feeds", :force => true do |t|
t.integer "channel_id"
t.text "raw_data"
t.text "field1"
t.text "field2"
t.text "field3"
t.text "field4"
t.text "field5"
t.text "field6"
t.text "field7"
t.text "field8"
t.string "field1"
t.string "field2"
t.string "field3"
t.string "field4"
t.string "field5"
t.string "field6"
t.string "field7"
t.string "field8"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "entry_id"
t.string "status"
t.decimal "latitude", :precision => 15, :scale => 10
t.decimal "longitude", :precision => 15, :scale => 10
t.string "elevation"
end

add_index "feeds", ["channel_id"], :name => "index_feeds_on_channel_id"
add_index "feeds", ["channel_id", "created_at"], :name => "index_feeds_on_channel_id_and_created_at"

create_table "users", :force => true do |t|
t.string "login", :null => false
Expand Down

0 comments on commit 7e964af

Please sign in to comment.