Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple Crime Upload #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/crimes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def show
# POST /crimes.json
def create
@crime = Crime.new(crime_params)
logger.info(crime_params)

if @crime.save
render json: @crime, status: :created, location: @crime
Expand All @@ -27,6 +28,12 @@ def create
end
end

def bulk
crimes_params.each do |i|
Crime.create(i[:crime])
end
end

# PATCH/PUT /crimes/1
# PATCH/PUT /crimes/1.json
def update
Expand Down Expand Up @@ -54,3 +61,7 @@ def crime_params
params.require(:crime).permit(:code, :name, :lat, :lng, :rawDateFormat, :reportNumber, :incidentTime, :street)
end

def crimes_params
params.require(:crimes)
end

53 changes: 5 additions & 48 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,7 @@
Ancbkn::Application.routes.draw do
resources :crimes, except: [:new, :edit]
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
resources :crimes, except: [:new, :edit] do
collection do
post 'bulk'
end
end
end
37 changes: 37 additions & 0 deletions scripts/crimes_small.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"crimes": [
{
"__type": "Crime:#CrimeMap.CrimeBO",
"m_crimeCode": "DST",
"m_crimeType": "DISTURBANCE",
"m_latitude": 61.20428755,
"m_longitude": -149.79617608,
"m_reportDate": "/Date(1334649600000-0800)/",
"m_reportNo": "120017394",
"m_reportTimeString": "12:22 A.M",
"m_streetName": "Reka Drive"
},
{
"__type": "Crime:#CrimeMap.CrimeBO",
"m_crimeCode": "DST",
"m_crimeType": "DISTURBANCE",
"m_latitude": 61.2175657,
"m_longitude": -149.87816242,
"m_reportDate": "/Date(1334649600000-0800)/",
"m_reportNo": "120017395",
"m_reportTimeString": "12:18 A.M",
"m_streetName": "East 5th Avenue"
},
{
"__type": "Crime:#CrimeMap.CrimeBO",
"m_crimeCode": "RB",
"m_crimeType": "ROBBERY",
"m_latitude": 61.20971336,
"m_longitude": -149.8289412,
"m_reportDate": "/Date(1334649600000-0800)/",
"m_reportNo": "120017396",
"m_reportTimeString": "12:28 A.M",
"m_streetName": "Debarr Road"
}
]
}
53 changes: 39 additions & 14 deletions scripts/load_data.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@

require 'json'
require 'net/http'

$root_url = "http://ancbkn.herokuapp.com"

def post_to_prod(crime)
uri = URI.parse("http://ancbkn.herokuapp.com/crimes")
post_params = build_crime(crime)

uri = URI.parse($root_url + "/crimes")
start_request(uri, post_params)
end

def bulk_post_to_prod(crimes)
crimes_array = []

crimes.each { |crime| crimes_array << build_crime(crime) }

post_params = {
:crimes => crimes_array
}

uri = URI.parse($root_url + "/crimes/bulk")
start_request(uri, post_params)
end

def build_crime(crime)
dateFormat = crime["m_reportDate"]
start_index = dateFormat.index("(") + 1
end_index = dateFormat.index("-") - 1
rawDateFormat = Time.at(dateFormat[start_index..end_index].to_i/1000)
puts "*"*10

puts "*"*10
puts rawDateFormat
post_params = {
:crime => {

param = {
:crime => {
:code => crime["m_crimeCode"],
:name => crime["m_crimeType"],
:lat => crime["m_latitude"],
Expand All @@ -20,24 +42,27 @@ def post_to_prod(crime)
:reportNumber => crime["m_reportNo"],
:incidentTime => crime["m_reportTimeString"],
:street => crime["m_streetName"]
}
}
}
}
end

def start_request(uri, post_params)
# Convert the parameters into JSON and set the content type as application/json
req = Net::HTTP::Post.new(uri.path)
req = Net::HTTP::Post.new(uri.path)
req.body = JSON.generate(post_params)
req["Content-Type"] = "application/json"

http = Net::HTTP.new(uri.host, uri.port)
response = http.start {|htt| htt.request(req)}
puts response.body
#puts response.body
end

topObject = JSON.parse(File.read("scripts/crimes.json"));
topObject = JSON.parse(File.read("scripts/crimes_small.json"));
crimes = topObject ["crimes"]

crimes.each do |c|
post_to_prod(c)
end
bulk_post_to_prod(crimes)

#crimes.each do |c|
# post_to_prod(c)
#end