Permalink
Browse files

Use a first class geo coordinate.

  • Loading branch information...
dblock committed Sep 24, 2018
1 parent 9fc79da commit 28f309cedd12d970338bfc98582daa521625b43e
@@ -6,6 +6,7 @@ ruby '2.5.1'
gem 'bcrypt'
gem 'bootsnap', '>= 1.1.0', require: false
gem 'email_validator'
gem 'geo_coord', require: 'geo/coord'
gem 'graphql'
gem 'graphql-errors'
gem 'mongoid'
@@ -64,6 +64,7 @@ GEM
faraday_middleware (0.12.2)
faraday (>= 0.7.4, < 1.0)
ffi (1.9.25)
geo_coord (0.1.0)
globalid (0.4.1)
activesupport (>= 4.2.0)
graphlient (0.3.2)
@@ -207,6 +208,7 @@ DEPENDENCIES
email_validator
fabrication
faker
geo_coord
graphlient
graphql
graphql-errors
@@ -4,7 +4,7 @@
input_field :title, types.String
input_field :started, !Types::DateTimeType
input_field :finished, !Types::DateTimeType
input_field :location, Types::InputLocationType
input_field :location, Types::GeoCoordinates

return_field :meeting, Types::MeetingType
return_field :meetingsConnection, Types::MeetingType.connection_type
@@ -14,14 +14,12 @@
user = ctx[:current_user]

if user
data = {}

data[:title] = inputs[:title]
data[:started_at] = inputs[:started]
data[:finished_at] = inputs[:finished]
data[:location] = inputs[:location].to_h if inputs[:location]

meeting = user.meetings.create!(data)
meeting = user.meetings.create!(
title: inputs[:title],
started_at: inputs[:started],
finished_at: inputs[:finished],
location: inputs[:location] ? inputs[:location].to_h : nil
)

range_add = GraphQL::Relay::RangeAdd.new(
parent: user,
@@ -17,6 +17,8 @@
data[:title] = inputs[:title] if inputs.key?(:title)
data[:started_at] = inputs[:started] if inputs.key?(:started)
data[:finished_at] = inputs[:finished] if inputs.key?(:finished)
data[:location] = inputs[:location].to_h if inputs.key?(:location)

meeting.update_attributes!(data)
{ meeting: meeting }
end
@@ -0,0 +1,7 @@
Types::GeoCoordinates = GraphQL::ScalarType.define do
name 'GeoCoordinates'
description 'Geo coordinate, latitude followed by longitude.'

coerce_input ->(value, _ctx) { Geo::Coord.parse(value) }
coerce_result ->(value, _ctx) { value.to_s }
end

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -7,5 +7,5 @@
field :started, Types::DateTimeType, 'The time at which this meeting started.', property: :started_at
field :finished, Types::DateTimeType, 'The time at which this meeting ended.', property: :finished_at
field :user, Types::UserType, 'Owner of this meeting.'
field :location, Types::LocationType, 'Geo-location of meeting.'
field :location, Types::GeoCoordinates, 'Geo-location of meeting.'
end
@@ -0,0 +1,9 @@
module Mongoid
module Geospatial
class Point
def to_s
Geo::Coord.new(y, x).to_s
end
end
end
end
@@ -3,5 +3,5 @@
started_at { Faker::Time.backward(1) }
finished_at { Time.now }
user { Fabricate(:user) }
location { { latitude: 22.3407, longitude: 114.2054 } }
location '50.004444, 36.231389'
end
@@ -12,10 +12,7 @@
title
started
finished
location {
latitude
longitude
}
location
},
meetingEdge {
node {
@@ -30,7 +27,7 @@
let(:title) { Faker::Company.buzzword }
let(:started_at) { Faker::Time.backward(1) }
let(:finished_at) { Time.now }
let(:location) { { latitude: 22.3407, longitude: 114.2054 } }
let(:location) { Geo::Coord.parse('50° 0′ 16″ N, 36° 13′ 53″ E') }

it 'returns an meeting' do
expect do
@@ -39,15 +36,14 @@
title: title,
started: started_at,
finished: finished_at,
location: location
location: location.to_s
}
)
meeting = response.data.create_meeting.meeting
expect(meeting.title).to eq title
expect(DateTime.parse(meeting.started)).to eq started_at.utc.iso8601
expect(DateTime.parse(meeting.finished)).to eq finished_at.utc.iso8601
expect(meeting.location.latitude).to eq location[:latitude]
expect(meeting.location.longitude).to eq location[:longitude]
expect(Geo::Coord.parse(meeting.location)).to eq location

edge = response.data.create_meeting.meeting_edge
expect(edge.node.id).to eq meeting.id
@@ -12,10 +12,7 @@
title
started
finished
location {
latitude
longitude
}
location
}
}
}
@@ -31,7 +28,6 @@
expect(returned_meeting.title).to eq meeting.title
expect(returned_meeting.started).to eq meeting.started_at.utc.iso8601
expect(returned_meeting.finished).to eq meeting.finished_at.utc.iso8601
expect(returned_meeting.location.longitude).to eq meeting.location.to_hsh(:lon, :lat)[:lon]
expect(returned_meeting.location.latitude).to eq meeting.location.to_hsh(:lon, :lat)[:lat]
expect(returned_meeting.location).to eq meeting.location.to_s
end
end

0 comments on commit 28f309c

Please sign in to comment.