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

[FEATURE] Gemmification #1

Open
wants to merge 4 commits 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile ~/.gitignore_global

settings.yml.erb
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@ rvm:
- 2.2.3
services:
- redis
before_script:
- "cp settings.sample.yml.erb settings.yml.erb"
script: "bundle exec rspec"
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ruby "2.2.3"
gem "rack"
gem "redis"
gem "colorize"
gem "activesupport"

group :test, :development do
gem "rspec"
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (4.1.15)
i18n (~> 0.6, >= 0.6.9)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.1)
tzinfo (~> 1.1)
coderay (1.1.1)
colorize (0.7.7)
diff-lcs (1.2.5)
Expand All @@ -20,11 +26,14 @@ GEM
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
i18n (0.7.0)
json (1.8.3)
listen (3.0.6)
rb-fsevent (>= 0.9.3)
rb-inotify (>= 0.9.7)
lumberjack (1.0.10)
method_source (0.8.2)
minitest (5.8.4)
mock_redis (0.16.1)
nenv (0.3.0)
notiffany (0.0.8)
Expand Down Expand Up @@ -57,11 +66,15 @@ GEM
shellany (0.0.1)
slop (3.6.0)
thor (0.19.1)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)

PLATFORMS
ruby

DEPENDENCIES
activesupport
colorize
guard-rspec
mock_redis
Expand Down
25 changes: 1 addition & 24 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,27 +1,4 @@
require "redis"
Dir["#{File.expand_path("./lib")}/**/**"].each { |filename| require(filename) }

filename = File.expand_path(".", "settings.yml.erb")

unless File.exist?(filename)
system("cp settings.sample.yml.erb #{filename}")
end

settings = Popsicle::Settings.new(filename: filename)
settings.load
settings = settings.fetch("popsicle")
store_settings = settings.fetch("store").fetch("redis")

store = Redis.new(host: store_settings["host"],
port: store_settings["port"],
password: store_settings["password"],
timeout: store_settings["timeout"],
reconnect_attempts: store_settings["reconnect_attempts"],
role: store_settings["role"])

application = Popsicle::Application.new(store: store,
app_name: settings["app_name"],
headers: settings["headers"],
index_key: settings["index_key"])

run application
run Popsicle::Application.new
2 changes: 1 addition & 1 deletion lib/popsicle.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "rack"

require "active_support/configurable"
module Popsicle
end
47 changes: 41 additions & 6 deletions lib/popsicle/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

module Popsicle
class Application
attr_accessor :request, :store, :app_name, :headers, :index_key
include ActiveSupport::Configurable
attr_accessor :request
config_accessor :store
config_accessor :app_name
config_accessor :headers
config_accessor :index_key

def initialize(store:, app_name:, headers:, index_key:)
@store = store
@app_name = app_name
@headers = headers
@index_key = index_key
def initialize
super
apply_config!
end

def call(env)
Expand All @@ -25,6 +28,22 @@ def found_revision
store.get(revision_key).to_s
end

def store
config[:store]
end

def app_name
config[:app_name]
end

def headers
config[:headers]
end

def index_key
config[:index_key]
end

def revision_requested?
request.params.key?(index_key)
end
Expand All @@ -49,5 +68,21 @@ def response(code:, body:)
StringIO.new(body)
]
end

private

def apply_config!
config[:app_name] ||= ENV["POPSICLE_APP_NAME"]
config[:index_key] ||= ENV["POPSICLE_INDEX_KEY"]

return if config[:store]
require "redis"
config[:store] ||= Redis.new(host: ENV["POPSICLE_REDIS_HOST"],
port: ENV["POPSICLE_REDIS_PORT"],
password: ENV["POPSICLE_REDIS_PASSWORD"],
timeout: ENV["POPSICLE_REDIS_TIMEOUT"],
reconnect_attempts: ENV["POPSICLE_REDIS_RECONNECT_ATTEMPTS"],
role: ENV["POPSICLE_REDIS_ROLE"])
end
end
end
23 changes: 0 additions & 23 deletions lib/popsicle/settings.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/popsicle/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Popsicle
VERSION = "0.1.4"
VERSION = "0.2.0"
end
21 changes: 21 additions & 0 deletions popsicle.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# coding: utf-8
gem_name = "popsicle"
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "#{gem_name}/version"

Gem::Specification.new do |s|
s.name = gem_name
s.version = Popsicle::VERSION
s.date = Time.now.to_s
s.authors = ["[email protected]"]
s.summary = "Rack-based web application targeted to serve JavaScript-based client-side applications, like Ember-CLI"
s.description = "A tiny Rack app to serve your Ember-CLI apps. Since it's rack-based you can easily mount this on any other framework and enjoy the fun!"
s.email = "[email protected]"
s.require_paths = ["lib"]

s.files = `git ls-files`.split($/)
s.test_files = files.grep(%r{^(test|spec|features)/})
s.homepage = "https://github.com/mariogintili/popsicle"
s.license = "MIT"
end
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,17 @@ A tiny Rack app to serve your Ember-CLI apps. Since it's rack-based you can easi
# Setup

- Press the deploy to Heroku button :rocket:
- Assign the env vars specified on [`app.json`](https://github.com/mariogintili/popsicle/blob/master/app.json)
- Assign the env vars specified on [`app.json`](https://github.com/mariogintili/popsicle/blob/master/app.json) OR add a custom initializer, like the one specified at the bottom of the file
- Deploy your Ember-CLI application & enjoy.

# Usage with Rails

Since this is a Rack-based application you can [mount it as middleware](http://guides.rubyonrails.org/rails_on_rack.html#configuring-middleware-stack) and setup some redirect rules on top ensuring Popsicle will only be on the set of routes that best suit you.

Don't forget to add an initializer, i.e `config/popsicle.rb`

```ruby
Popsicle::Application do |config|
config.app_name = "name-of-my-js-app"
end
```
13 changes: 0 additions & 13 deletions settings.sample.yml.erb

This file was deleted.

10 changes: 6 additions & 4 deletions spec/acceptance/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
end

def app
@app ||= Popsicle::Application.new(store: app_settings[:store],
app_name: app_settings[:app_name],
headers: app_settings[:headers],
index_key: app_settings[:index_key])
@app ||= Popsicle::Application.new.tap do |instance|
instance.store = app_settings[:store]
instance.app_name = app_settings[:app_name]
instance.headers = app_settings[:headers]
instance.index_key = app_settings[:index_key]
end
end

describe "Making a request to the server" do
Expand Down
23 changes: 17 additions & 6 deletions spec/popsicle/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,28 @@
let(:index_key) { double(:index_key) }

subject do
described_class.new(store: store,
app_name: app_name,
headers: headers,
index_key: index_key)
described_class.new.tap do |instance|
instance.store = store
instance.app_name = app_name
instance.headers = headers
instance.index_key = index_key
end
end

describe "#initialize" do
it "assigns store, app_name, headers, index_key" do
describe "attributes" do
it "store" do
expect(subject.store).to eq(store)
end

it "app_name" do
expect(subject.app_name).to eq(app_name)
end

it "headers" do
expect(subject.headers).to eq(headers)
end

it "index_key" do
expect(subject.index_key).to eq(index_key)
end
end
Expand Down
40 changes: 0 additions & 40 deletions spec/popsicle/settings_spec.rb

This file was deleted.

1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "mock_redis"
require "rack/test"
require "active_support/configurable"
require "pry"

Dir["#{File.expand_path("./lib")}/**/**"].reverse.each do |filename|
Expand Down