forked from rubyonjets/jets
-
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
15 changed files
with
212 additions
and
24 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,12 +1,22 @@ | ||
guard "rspec" do | ||
watch(%r{^spec/.+_spec\.rb$}) | ||
watch(%r{^lib/(.+)\.rb$}) { "spec/lam_spec.rb" } | ||
watch(%r{^lib/lam/(.+)\.rb$}) { "spec/lam_spec.rb" } | ||
watch("spec/spec_helper.rb") { "spec/lam_spec.rb" } | ||
watch(%r{^lib/lam/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
end | ||
|
||
guard "bundler" do | ||
guard "bundler", cmd: "bundle" do | ||
watch("Gemfile") | ||
watch(/^.+\.gemspec/) | ||
end | ||
|
||
guard :rspec, cmd: "bundle exec rspec" do | ||
require "guard/rspec/dsl" | ||
dsl = Guard::RSpec::Dsl.new(self) | ||
|
||
# RSpec files | ||
rspec = dsl.rspec | ||
watch(rspec.spec_helper) { rspec.spec_dir } | ||
watch(rspec.spec_support) { rspec.spec_dir } | ||
watch(rspec.spec_files) | ||
|
||
# Ruby files | ||
ruby = dsl.ruby | ||
puts "ruby.lib_files #{ruby.lib_files.inspect}" | ||
dsl.watch_spec_files_for(ruby.lib_files) | ||
|
||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
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
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,37 @@ | ||
module Lam | ||
class BaseController | ||
def initialize(event, context) | ||
@event, @context = event, context | ||
end | ||
|
||
private | ||
def render(options={}) | ||
# render json: {"mytestdata": "value1"}, status: 200, headers: {...} | ||
if options.has_key?(:json) | ||
# Transform the structure to Lambda Proxy structure | ||
# {statusCode: ..., body: ..., headers: } | ||
status = options.delete(:status) | ||
body = options.delete(:json) | ||
result = options.merge( | ||
statusCode: status, | ||
body: body | ||
) | ||
# render text: "text" | ||
elsif options.has_key?(:text) | ||
result = options.delete(:text) | ||
else | ||
raise "Unsupported render option. Only :text and :json supported. options #{options.inspect}" | ||
end | ||
|
||
result | ||
end | ||
|
||
# API Gateway LAMBDA_PROXY wraps the event in its own structure. | ||
# We unwrap the "body" before sending it back | ||
# For regular Lambda function calls, no need to unwrap but need to | ||
# transform it to a string with JSON.dump. | ||
def normalize_event_body(event) | ||
body = event.has_key?("body") ? event["body"] : JSON.dump(event) | ||
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
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
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 'json' | ||
require_relative 'infer' | ||
|
||
# Global overrides for Lambda processing | ||
$stdout.sync = true | ||
# This might seem weird but we want puts to write to stderr which is set in | ||
# the node shim to write to stderr. This directs the output to Lambda logs. | ||
# Printing to stdout can managle up the payload returned from Lambda function. | ||
# This is not desired if you want to return say a json payload to API Gateway | ||
# eventually. | ||
def puts(text) | ||
$stderr.puts(text) | ||
end | ||
|
||
class Lam::Process::BaseProcessor | ||
attr_reader :event, :context, :handler | ||
def initialize(event, context, handler) | ||
@event, @context, @handler = event, context, handler | ||
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,34 @@ | ||
require_relative "base_processor" | ||
|
||
class Lam::Process | ||
class ControllerProcessor < Lam::Process::BaseProcessor | ||
def run | ||
# Use the handler value (ie: posts.create) to infer the user's business | ||
# code to require and run. | ||
infer = Infer.new(handler) | ||
path = infer.controller[:path] | ||
code = infer.controller[:code] | ||
|
||
begin | ||
require path # require "app/controllers/posts_controller.rb" | ||
# Puts the return value of user's code to stdout because this is | ||
# what eventually gets used by API Gateway. | ||
# Explicitly using $stdout since puts redirected to $stderr. | ||
result = instance_eval(code, path) # result = PostsController.new(event, context).create | ||
|
||
# JSON.dump is pretty robust. If it cannot dump the structure into a | ||
# json string, it just dumps it to a plain text string. | ||
$stdout.puts JSON.dump(result) # only place where we write to stdout. | ||
rescue Exception => e | ||
# Customize error message slightly so nodejs shim can process the | ||
# returned error message. | ||
# The "RubyError: " is a marker that the javascript shim scans for. | ||
$stderr.puts("RubyError: #{e.class}: #{e.message}") # js needs this as the first line | ||
backtrace = e.backtrace.map {|l| " #{l}" } | ||
$stderr.puts(backtrace) | ||
# $stderr.puts("END OF RUBY OUTPUT") | ||
exit 1 # instead of re-raising to control the error backtrace output | ||
end | ||
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,53 @@ | ||
class Lam::Process::Infer | ||
def initialize(handler) | ||
@handler = handler | ||
@project_root = ENV['PROJECT_ROOT'] || '.' | ||
end | ||
|
||
# Infers the path and method from the handler. Example: | ||
# | ||
# InferCode.new("handlers/functions/posts.create").function | ||
# => {path: "app/functions/posts.rb", code: "create(event, context)"} | ||
# | ||
# Summary: | ||
# | ||
# Input: | ||
# handler: handlers/functions/posts.create | ||
# Output: | ||
# path: app/functions/posts.rb | ||
# code: create(event, context) # code to instance_eval | ||
# | ||
# Returns: {path: path, code: code} | ||
def function | ||
path, meth = @handler.split('.') | ||
path = "#{@project_root}/" + path.sub("handlers", "app") + ".rb" | ||
code = "#{meth}(event, context)" | ||
{path: path, code: code} | ||
end | ||
|
||
# Infers the path and method from the handler. Example: | ||
# | ||
# InferCode.new("handlers/controllers/posts.create").controller | ||
# => {path: "controllers/posts_controller.rb", code: "create"} | ||
# | ||
# Summary: | ||
# | ||
# Input: | ||
# handler: handlers/controllers/posts.create | ||
# Output: | ||
# path: app/controllers/posts_controller.rb | ||
# code: create # code to instance_eval | ||
# | ||
# Returns: {path: path, code: code} | ||
def controller | ||
handler_path, meth = @handler.split('.') | ||
|
||
path = "#{@project_root}/" + handler_path.sub("handlers", "app") + "_controller.rb" | ||
|
||
controller_name = handler_path.sub(%r{.*handlers/controllers/}, "") + "_controller" # posts_controller | ||
controller_class = controller_name.split('_').collect(&:capitalize).join # PostsController | ||
code = "#{controller_class}.new(event, context).#{meth}" # PostsController.new(event, context).create | ||
|
||
{path: path, code: code} | ||
end | ||
end |
2 changes: 2 additions & 0 deletions
2
spec/fixtures/project/app/controllers/application_controller.rb
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,2 @@ | ||
class ApplicationController < Lam::BaseController | ||
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,8 @@ | ||
class PostsController < Lam::BaseController | ||
def create | ||
# render text: "test2" # more consistent for web controllers | ||
|
||
# render returns Lamba Proxy struture for web requests | ||
render json: {"mytestdata": "value2"}, status: 200 | ||
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
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,29 @@ | ||
require "spec_helper" | ||
|
||
|
||
describe Lam::Process::Infer do | ||
before(:all) do | ||
# @args = "--from Tung" | ||
end | ||
|
||
describe "lam" do | ||
let(:infer) { Lam::Process::Infer.new(handle) } | ||
|
||
context("controller") do | ||
let(:handle) { "handlers/controllers/posts.create" } | ||
it "should infer path and code" do | ||
expect(infer.controller[:path]).to include "app/controllers/posts_controller.rb" | ||
expect(infer.controller[:code]).to eq "PostsController.new(event, context).create" | ||
end | ||
end | ||
|
||
context("function") do | ||
let(:handle) { "handlers/functions/posts.create" } | ||
it "should infer path and code" do | ||
expect(infer.function[:path]).to include "app/functions/posts.rb" | ||
expect(infer.function[:code]).to eq "create(event, context)" | ||
end | ||
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 |
---|---|---|
|
@@ -14,4 +14,3 @@ | |
end | ||
end | ||
end | ||
|
File renamed without changes.
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,4 +1,5 @@ | ||
ENV["TEST"] = "1" | ||
ENV["PROJECT_ROOT"] = "./spec/fixtures/project" | ||
|
||
require "simplecov" | ||
SimpleCov.start | ||
|