-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.idea/ | ||
.rspec | ||
.gitignore | ||
*.sqlite3 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
source 'http://rubygems.org' | ||
|
||
# service | ||
gem 'activerecord' | ||
gem 'sqlite3' | ||
gem 'sinatra' | ||
|
||
# client | ||
gem 'json' | ||
gem 'typhoeus' | ||
|
||
# testing | ||
gem 'rspec' | ||
gem 'rack-test' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
GEM | ||
remote: http://rubygems.org/ | ||
specs: | ||
activemodel (4.2.6) | ||
activesupport (= 4.2.6) | ||
builder (~> 3.1) | ||
activerecord (4.2.6) | ||
activemodel (= 4.2.6) | ||
activesupport (= 4.2.6) | ||
arel (~> 6.0) | ||
activesupport (4.2.6) | ||
i18n (~> 0.7) | ||
json (~> 1.7, >= 1.7.7) | ||
minitest (~> 5.1) | ||
thread_safe (~> 0.3, >= 0.3.4) | ||
tzinfo (~> 1.1) | ||
arel (6.0.3) | ||
builder (3.2.2) | ||
diff-lcs (1.2.5) | ||
ethon (0.9.0) | ||
ffi (>= 1.3.0) | ||
ffi (1.9.10) | ||
i18n (0.7.0) | ||
json (1.8.3) | ||
minitest (5.8.4) | ||
rack (1.6.4) | ||
rack-protection (1.5.3) | ||
rack | ||
rack-test (0.6.3) | ||
rack (>= 1.0) | ||
rspec (3.4.0) | ||
rspec-core (~> 3.4.0) | ||
rspec-expectations (~> 3.4.0) | ||
rspec-mocks (~> 3.4.0) | ||
rspec-core (3.4.4) | ||
rspec-support (~> 3.4.0) | ||
rspec-expectations (3.4.0) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.4.0) | ||
rspec-mocks (3.4.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.4.0) | ||
rspec-support (3.4.1) | ||
sinatra (1.4.7) | ||
rack (~> 1.5) | ||
rack-protection (~> 1.4) | ||
tilt (>= 1.3, < 3) | ||
sqlite3 (1.3.11) | ||
thread_safe (0.3.5) | ||
tilt (2.0.2) | ||
typhoeus (1.0.2) | ||
ethon (>= 0.9.0) | ||
tzinfo (1.2.2) | ||
thread_safe (~> 0.1) | ||
|
||
PLATFORMS | ||
ruby | ||
|
||
DEPENDENCIES | ||
activerecord | ||
json | ||
rack-test | ||
rspec | ||
sinatra | ||
sqlite3 | ||
typhoeus | ||
|
||
BUNDLED WITH | ||
1.11.2 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'rubygems' | ||
require 'active_record' | ||
require 'yaml' | ||
require 'logger' | ||
|
||
desc 'Load the environment' | ||
task :environment do | ||
env = ENV['SINATRA_ENV'] || 'development' | ||
databases = YAML.load_file('config/database.yml') | ||
ActiveRecord::Base.establish_connection(databases[env]) | ||
end | ||
|
||
namespace :db do | ||
desc 'Migrate the database' | ||
task(migrate: :environment) do | ||
ActiveRecord::Base.logger = Logger.new(STDOUT) | ||
ActiveRecord::Migration.verbose = true | ||
ActiveRecord::Migrator.migrate('db/migrate') | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
development: | ||
adapter: sqlite3 | ||
database: db/development.sqlite3 | ||
pool: 5 | ||
timeout: 5000 | ||
|
||
test: | ||
adapter: sqlite3 | ||
database: db/test.sqlite3 | ||
pool: 5 | ||
timeout: 5000 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateEmailTemplates < ActiveRecord::Migration | ||
def change | ||
create_table :email_templates do |t| | ||
t.string :title | ||
t.text :body | ||
|
||
t.timestamps null: false | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class EmailTemplate < ActiveRecord::Base | ||
validates_presence_of :title, :body | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# service.rb - contains the entire service | ||
require 'active_record' | ||
require 'sinatra' | ||
require_relative 'models/email_template' | ||
require 'logger' | ||
|
||
# setting up a logger. levels: DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN | ||
log = Logger.new(STDOUT) | ||
log.level == Logger::DEBUG | ||
|
||
# setting up the environment | ||
env_index = ARGV.index("-e") | ||
env_arg = ARGV[env_index + 1] if env_index | ||
env = env_arg || ENV['SINATRA_ENV'] || 'development' | ||
log.debug "env: #{env}" | ||
|
||
# connecting to database | ||
use ActiveRecord::ConnectionAdapters::ConnectionManagement # close connection to the DDBB properly | ||
databases = YAML.load_file('config/database.yml') | ||
ActiveRecord::Base.establish_connection(databases[env]) | ||
log.debug "#{databases[env]['database']} database connection established." | ||
|
||
# # create a fixture data for test env only | ||
# if env == 'test' | ||
# EmailTemplate.delete_all | ||
# EmailTemplate.create(id: 1, title: 'Fixture data title', body: 'Fixture data body') | ||
# log.debug 'fixture data created in test database.' | ||
# end | ||
|
||
# HTTP entry points | ||
|
||
# get an email template by id | ||
get '/api/v1/templates/:id' do | ||
email_template = EmailTemplate.find_by(id: params[:id]) | ||
if email_template | ||
email_template.to_json | ||
else | ||
error 404, {error: 'Template not found'}.to_json | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
ENV['SINATRA_ENV'] = 'test' | ||
|
||
require File.dirname(__FILE__) + '/../service' | ||
require 'rspec' | ||
require 'rack/test' | ||
|
||
RSpec.configure do |conf| | ||
conf.include Rack::Test::Methods | ||
end | ||
|
||
def app | ||
Sinatra::Application | ||
end | ||
|
||
describe 'service' do | ||
before(:each) do | ||
EmailTemplate.delete_all | ||
end | ||
|
||
describe 'GET on api/v1/templates/:id' do | ||
before(:each) do | ||
EmailTemplate.create( | ||
id: 130516, | ||
title: 'Test title template', | ||
body: 'Test body for email template' | ||
) | ||
end | ||
|
||
it 'returns an email template by id' do | ||
get '/api/v1/templates/130516' | ||
expect(last_response).to be_ok | ||
attributes = JSON.parse(last_response.body) | ||
expect(attributes['id']).to eq(130516) | ||
end | ||
|
||
it 'returns an email template with title' do | ||
get '/api/v1/templates/130516' | ||
expect(last_response).to be_ok | ||
attributes = JSON.parse(last_response.body) | ||
expect(attributes['title']).to eq('Test title template') | ||
end | ||
|
||
it 'returns an email template with body' do | ||
get '/api/v1/templates/130516' | ||
expect(last_response).to be_ok | ||
attributes = JSON.parse(last_response.body) | ||
expect(attributes['body']).to eq('Test body for email template') | ||
end | ||
|
||
it "returns 404 for an email template that does'n exist" do | ||
get '/api/v1/templates/foo' | ||
expect(last_response.status).to be 404 | ||
end | ||
end | ||
end |