This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"_id": "supplier-one", | |
"name": "Supplier One" | |
}, | |
{ | |
"_id": "supplier-two", | |
"name": "Supplier Two" | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/locations' do | |
lat = params.fetch(:lat, 0).to_f | |
lng = params.fetch(:lng, 0).to_f | |
distance = params[:distance].to_i * 1.61 * 1000 | |
query = { | |
loc: { | |
:$near => { | |
:$geometry => { | |
type: 'Point', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/suppliers' do | |
Supplier.all.to_json | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App < Sinatra::Base | |
configure do | |
# Let's connect to MongoDB | |
begin | |
mongo_db = Mongo::Connection.new.db('data-test') | |
set :mongo_db, mongo_db | |
rescue Mongo::ConnectionFailure | |
set :mongo_db, {} | |
end | |
Mongoid.load!('mongoid.yml') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace 'import' do | |
task 'locations' do | |
locations = App.mongo_db.collection('locations') | |
locations.insert( | |
number: 1, | |
sid: 'supplier-one', | |
loc: [8, 10] | |
) | |
locations.insert( | |
number: 2, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Location | |
include Mongoid::Document | |
index(loc: '2dsphere') | |
field(:number, type: Integer) | |
# supplier id | |
field(:sid, type: String) | |
# coordinates | |
field(:loc, type: Array) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Supplier | |
include Mongoid::Document | |
field(:name, type: String) | |
field(:_id, default: -> { name.parameterize if self }) | |
end |
NewerOlder