Skip to content

Commit 1ef18c3

Browse files
Reorganize to introduce database support.
1 parent 4b8b953 commit 1ef18c3

21 files changed

+104
-79
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
DATABASE_URL=postgres://localhost/qwubble_development
12
GCM_API_KEY=AIzaSyAaL2sp05G2zA7mk55cFzKuK2zbLF-5b6s
23
GCM_TEST_REGISTRATION_ID=APA91bFIRWEnsebu4pycVmDbBG1qswBsbI0KbZSDp6fZ5yOvFZAUIPCQEdtOgsVxuh9D-J2BEUaQOcXRsjxT8DJIdjoVkjM634hgAyVUR-PtHxEQW7krNHtcPAwcIrBzATCefPmXXxQxGfVjO4z8JFbwnWRl-_e_cZBWHrv_zSGl4JOXLD60K0o

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
DATABASE_URL=postgres://localhost/qwubble_development
12
GCM_API_KEY=
23
GCM_TEST_REGISTRATION_ID=

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ruby "2.1.1"
44
gem "dotenv"
55
gem "em-gcm", github: "SpotIM/em-gcm"
66
gem "em-http-request"
7+
gem "em-pg-sequel"
78
gem "goliath"
89
gem "grape"
910

Gemfile.lock

+9
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ GEM
3737
em-socksify (>= 0.3)
3838
eventmachine (>= 1.0.3)
3939
http_parser.rb (>= 0.6.0.beta.2)
40+
em-pg-client (0.3.3)
41+
eventmachine (~> 1.0.0)
42+
pg (>= 0.17.0)
43+
em-pg-sequel (0.0.4)
44+
em-pg-client (>= 0.3.0)
45+
sequel
4046
em-socksify (0.3.0)
4147
eventmachine (>= 1.0.0.beta.4)
4248
em-synchrony (1.0.3)
@@ -76,6 +82,7 @@ GEM
7682
minitest (5.3.3)
7783
multi_json (1.9.2)
7884
multi_xml (0.5.5)
85+
pg (0.17.1)
7986
rack (1.5.2)
8087
rack-accept (0.4.5)
8188
rack (>= 0.4)
@@ -97,6 +104,7 @@ GEM
97104
rspec-expectations (2.14.5)
98105
diff-lcs (>= 1.1.3, < 2.0)
99106
rspec-mocks (2.14.6)
107+
sequel (4.9.0)
100108
thread_safe (0.3.3)
101109
tzinfo (1.1.0)
102110
thread_safe (~> 0.1)
@@ -113,6 +121,7 @@ DEPENDENCIES
113121
dotenv
114122
em-gcm!
115123
em-http-request
124+
em-pg-sequel
116125
goliath
117126
grape
118127
rack-test

api/ping.rb

-8
This file was deleted.

api/registrations.rb

-18
This file was deleted.

app.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require 'dotenv'
2-
Dotenv.load
1+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
32

4-
require File.expand_path("../config/environment", __FILE__)
3+
require "qwubble"

app/api.rb

-8
This file was deleted.

app/qwubble_app.rb

-10
This file was deleted.

config/application.rb

-15
This file was deleted.

config/boot.rb

-2
This file was deleted.

config/environment.rb

-3
This file was deleted.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Sequel.migration do
2+
up do
3+
create_table(:registrations) do
4+
primary_key :id
5+
String :registration_id, null: false
6+
end
7+
end
8+
9+
down do
10+
drop_table(:registrations)
11+
end
12+
end

lib/qwubble.rb

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'dotenv'; Dotenv.load
2+
require "em-pg-sequel"
3+
require "em-synchrony"
4+
require "goliath"
5+
require "grape"
6+
require "sequel"
7+
8+
DB = ::Sequel.connect(
9+
ENV["DATABASE_URL"],
10+
pool_class: :em_synchrony)
11+
12+
Bundler.require :default
13+
14+
require "qwubble/resources/ping"
15+
require "qwubble/resources/registrations"
16+
require "qwubble/goliath"
17+
require "qwubble/grape"

lib/qwubble/goliath.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Qwubble
2+
class Goliath < Goliath::API
3+
use ::Goliath::Rack::Params
4+
use ::Goliath::Rack::Render
5+
6+
def response(env)
7+
Qwubble::Grape.call(env)
8+
end
9+
end
10+
end

lib/qwubble/grape.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Qwubble
2+
class Grape < Grape::API
3+
prefix 'api'
4+
format :json
5+
mount ::Qwubble::Resources::Ping
6+
mount ::Qwubble::Resources::Registrations
7+
end
8+
end

lib/qwubble/resources/ping.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Qwubble
2+
module Resources
3+
class Ping < ::Grape::API
4+
format :json
5+
get '/ping' do
6+
one = DB.execute("select 1")
7+
{ ping: "pong" }
8+
end
9+
end
10+
end
11+
end
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module Qwubble
2+
module Resources
3+
class Registrations < ::Grape::API
4+
format :json
5+
6+
resource :registrations do
7+
8+
desc "Register a device."
9+
params do
10+
requires :registration_id, type: String, desc: "The device's registration id."
11+
end
12+
post do
13+
{ registration_id: params[:registration_id] }
14+
end
15+
16+
end
17+
18+
end
19+
end
20+
end

spec/api/ping_spec.rb spec/resources/ping_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
require 'spec_helper'
22

3-
describe Qwubble::API do
4-
it "ping" do
5-
with_api Qwubble::App do
3+
describe Qwubble::Resources::Ping do
4+
it "pings" do
5+
with_api Qwubble::Goliath do
66
get_request(path: "/api/ping") do |async|
77
async.response.should == { ping: "pong" }.to_json
88
end
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
require 'spec_helper'
22

3-
describe Qwubble::Registrations do
3+
describe Qwubble::Resources::Registrations do
44
it "registers a device" do
5-
with_api Qwubble::App do
5+
with_api Qwubble::Goliath do
66
post_request(path: "/api/registrations", body: { registration_id: "josh is awesome" }) do |async|
77
async.response.should == { registration_id: "josh is awesome" }.to_json
88
end
99
end
1010
end
1111
end
12-

spec/spec_helper.rb

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
require 'rubygems'
2-
1+
ENV["DATABASE_URL"] ||= 'postgres://localhost/qwubble_test'
32
ENV["RACK_ENV"] ||= 'test'
43

5-
require 'rack/test'
4+
require 'rubygems'
5+
require 'bundler/setup'
6+
require 'goliath/test_helper'
7+
require 'qwubble'
68

7-
require File.expand_path("../../config/environment", __FILE__)
9+
Bundler.require :default, :test
810

911
RSpec.configure do |config|
1012
config.mock_with :rspec
1113
config.expect_with :rspec
1214
end
1315

14-
require 'goliath/test_helper'
1516
RSpec.configure do |c|
1617
c.include Goliath::TestHelper, :example_group => {
17-
:file_path => /spec\/api/
18+
:file_path => /spec\/resources/
1819
}
1920
end

0 commit comments

Comments
 (0)