cynic 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 921909ae4a5f456d4a8a6ab259cabd6db7cba467
4
+ data.tar.gz: 57097bc3f55fb85f001eb6708b4fa7f6cf230191
5
+ SHA512:
6
+ metadata.gz: fa68271e395072d4f6cfa50ff873fb76c2a13997e245f47762842770b8e11935267b6fb9aeeaec0192a90690893d4f84481454f68c9d7cdd1c86550faab25b27
7
+ data.tar.gz: f73f4f287c5db2b37e109e33d8baf4dd116a4776dbca4281d4995b5456d3521eb70a704bdbfbbb7498920ef012f32c4963e43640ecfffbc21f5ca625899b421e
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cynic.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Bookis Smuin
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Cynic
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cynic'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cynic
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/cynic ADDED
@@ -0,0 +1,111 @@
1
+ #!/usr/bin/env ruby
2
+ require "fileutils"
3
+ require "cynic"
4
+
5
+ module Cynic
6
+ class Generator
7
+ attr_accessor :name, :options, :app_name
8
+
9
+ def initialize(args)
10
+ @name, @options = args[1], args[2..-1]
11
+ @app_name = @name.capitalize
12
+ end
13
+
14
+
15
+ def make_file(name, &block)
16
+ File.open("#{@name}/#{name}", "w") do |file|
17
+ block.call(file)
18
+ end
19
+ end
20
+
21
+ def create_routes
22
+ make_file("config/routes.rb") do |file|
23
+ file.write <<-HEREDOC
24
+ Cynic.application.routing.define do |map|
25
+ # map.get "/", to: [HomeController.new, :index]
26
+ end
27
+ HEREDOC
28
+ end
29
+
30
+ end
31
+
32
+ def create_gemfile
33
+ make_file("Gemfile") do |file|
34
+ file.write <<-HEREDOC
35
+ source 'https://rubygems.org'
36
+
37
+ gem "cynic", "#{Cynic::VERSION}"
38
+ HEREDOC
39
+ end
40
+
41
+ end
42
+
43
+ def create_application
44
+ make_file("config/application.rb") do |file|
45
+ file.write <<-HEREDOC
46
+ require 'cynic'
47
+ Dir.glob(["./app/**/*.rb"]).each {|file| require file }
48
+
49
+ module #{@app_name}
50
+ class Application < Cynic::App
51
+ # Your code here
52
+ end
53
+ end
54
+
55
+
56
+
57
+ HEREDOC
58
+ end
59
+ end
60
+
61
+ def create_config
62
+ Dir.mkdir([self.name, "config"].join("/"))
63
+ make_file("config.ru") do |file|
64
+ file.write <<-HEREDOC
65
+ require './config/application'
66
+ #{self.name} = #{@app_name}::Application.initialize!
67
+ run #{self.name}
68
+ HEREDOC
69
+ end
70
+ end
71
+
72
+ def create_layout
73
+ Dir.mkdir([self.name, "app", "views", "layouts"].join("/"))
74
+ make_file("app/views/layouts/application.html.erb") do |file|
75
+ file.write <<-HEREDOC
76
+ <!DOCTYPE html>
77
+ <head>
78
+ <meta http-equiv="Content-type" content="text/html; charset=utf-8">
79
+ <title></title>
80
+ </head>
81
+
82
+ <body>
83
+ <%= yield %>
84
+ </body>
85
+ HEREDOC
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ case ARGV[0]
92
+ when "new"
93
+ generator = Cynic::Generator.new(ARGV)
94
+ Dir.mkdir(generator.name)
95
+ Dir.mkdir([generator.name, "app"].join("/"))
96
+ Dir.mkdir([generator.name, "app", "controllers"].join("/"))
97
+ Dir.mkdir([generator.name, "app", "views"].join("/"))
98
+ Dir.mkdir([generator.name, "app", "models"].join("/"))
99
+ Dir.mkdir([generator.name, "public"].join("/"))
100
+ Dir.mkdir([generator.name, "public", "stylesheets"].join("/"))
101
+ Dir.mkdir([generator.name, "public", "javascripts"].join("/"))
102
+ Dir.mkdir([generator.name, "public", "images"].join("/"))
103
+ generator.create_config
104
+ generator.create_application
105
+ generator.create_gemfile
106
+ generator.create_routes
107
+ generator.create_layout
108
+ when "server"
109
+ `shotgun -p 4545 -s puma`
110
+ end
111
+
data/cynic.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cynic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cynic"
8
+ spec.version = Cynic::VERSION
9
+ spec.authors = ["Bookis Smuin"]
10
+ spec.email = ["bookis.smuin@gmail.com"]
11
+ spec.description = %q{A Lightweight Rack-based framework}
12
+ spec.summary = %q{Cynic provides a framework with a few tools built on the ideas of rails.}
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_runtime_dependency "rack"
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
data/lib/cynic/app.rb ADDED
@@ -0,0 +1,45 @@
1
+ module Cynic
2
+ class App
3
+ attr_reader :routing
4
+
5
+ def self.initialize!
6
+ Cynic.application = self.new
7
+ require './config/routes'
8
+ builder = Rack::Builder.new
9
+ builder.use Rack::Static, :urls => ["/javascripts", "/stylesheets", "/images"], :root => "public"
10
+ builder.run Cynic.application
11
+ builder
12
+ end
13
+
14
+ def initialize
15
+ Cynic.application = self
16
+ @routing = Routing.new
17
+ end
18
+
19
+ def call(env)
20
+ @env = env
21
+ send_response
22
+ end
23
+
24
+ def routing_to_request
25
+ routing.go_to @env["REQUEST_METHOD"].downcase.to_sym, @env["REQUEST_PATH"]
26
+ end
27
+
28
+ def send_response
29
+ [status, headers, response]
30
+ end
31
+
32
+ def response
33
+ Response.new(self.routing_to_request)
34
+ end
35
+
36
+ def status
37
+ 200
38
+ end
39
+
40
+ def headers
41
+ {"CONTENT-TYPE" => "text/html"}
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ module Cynic
2
+ class Configuration
3
+ attr_accessor :environment
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ module Cynic
2
+ class Controller
3
+ def render(action)
4
+ @action = action
5
+ Renderer.new(full_path).body
6
+ end
7
+
8
+ def path
9
+ self.class.to_s.split("::").join("/").downcase.gsub(/controller$/i, "")
10
+ end
11
+
12
+ def full_path
13
+ path + "/#{@action}.html.erb"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,29 @@
1
+ module Cynic
2
+ class Renderer
3
+ def initialize(path)
4
+ @full_path = path_prefix + path
5
+ @layout = path_prefix + "layouts/application.html.erb"
6
+ end
7
+
8
+ def body
9
+ layout { ERB.new(File.read(@full_path)).result }
10
+ end
11
+
12
+ def layout
13
+ ERB.new(layout_file || "<%= yield %>").result(binding)
14
+ end
15
+
16
+ def layout_file
17
+ layout_exists? ? File.read(@layout) : "<%= yield %>"
18
+ end
19
+
20
+ def path_prefix
21
+ "app/views/"
22
+ end
23
+
24
+ def layout_exists?
25
+ File.exists? @layout
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ module Cynic
2
+ class Response
3
+ attr_reader :body
4
+ def initialize(body)
5
+ @body = body
6
+ end
7
+ def each(&block)
8
+ block.call @body
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ module Cynic
2
+ class Routing
3
+ class Error < StandardError;end
4
+
5
+ def initialize()
6
+ @routings = {get: {}, post: {}, patch: {}, delete: {}}
7
+ end
8
+
9
+ def define(&block)
10
+ block.call(self).to_s
11
+ self
12
+ end
13
+
14
+ def go_to(request_method, request_path)
15
+ klass, action = @routings[request_method.to_sym][request_path]
16
+ klass.send(action)
17
+ rescue TypeError
18
+ raise Error, "undefined routing #{request_method.upcase} '#{request_path}'"
19
+ end
20
+
21
+ def get(path, options={})
22
+ @routings[:get][path] = options[:to]
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Cynic
2
+ VERSION = "0.0.2"
3
+ end
data/lib/cynic.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "cynic/version"
2
+ require 'cynic/configuration'
3
+
4
+ module Cynic
5
+ class << self
6
+ attr_accessor :configuration, :application
7
+
8
+ def configure
9
+ @configuration = Configuration.new
10
+ yield(configuration) if block_given?
11
+ end
12
+
13
+ def application
14
+ @application ||= App.new
15
+ end
16
+ end
17
+ end
18
+
19
+ require 'cynic/app'
20
+ require 'cynic/response'
21
+ require 'cynic/routing'
22
+ require 'cynic/controller'
23
+ require 'cynic/renderer'
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cynic::App do
4
+ let(:env) { {"REQUEST_METHOD" => "GET", "REQUEST_PATH" => "/cynic"} }
5
+ let(:cynic) { Cynic.application }
6
+ before { require "support/routes" }
7
+
8
+ describe "#routing" do
9
+ it "returns a routing object" do
10
+ expect(cynic.routing).to be_an_instance_of Cynic::Routing
11
+ end
12
+ end
13
+
14
+ describe "#call" do
15
+ before do
16
+ Cynic::Controller.any_instance.stub(:index).and_return("This is erb hello")
17
+ end
18
+
19
+ it "responds to call" do
20
+ expect(cynic).to respond_to(:call)
21
+ end
22
+
23
+ it "returns an array" do
24
+ expect(cynic.call(env)).to be_an_instance_of Array
25
+ end
26
+
27
+ describe "the returned array" do
28
+ it "0 returns 200" do
29
+ expect(cynic.call(env)[0]).to eq 200
30
+ end
31
+ it "1 returns a Hash" do
32
+ expect(cynic.call(env)[1]).to be_an_instance_of Hash
33
+ end
34
+ it "2 returns something that responds to `each`" do
35
+ expect(cynic.call(env)[2]).to respond_to :each
36
+ end
37
+
38
+ it "has a response object with a body" do
39
+ expect(cynic.call(env)[2].body).to eq "This is erb hello"
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+ class CynicController < Cynic::Controller;end
3
+ describe Cynic::Controller do
4
+ let(:controller) { CynicController.new }
5
+
6
+ describe "#render" do
7
+ it "finds a file" do
8
+ Cynic::Renderer.any_instance.stub(:layout_file).and_return("<%= yield %>")
9
+ File.should_receive(:read).with("app/views/cynic/index.html.erb").once.and_return('This is erb <%= "olleh".reverse %>')
10
+ expect(controller.render(:index)).to eq "This is erb hello"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cynic::Renderer do
4
+ path = "cynic/controller/index.html.erb"
5
+ let(:renderer) { Cynic::Renderer.new(path) }
6
+
7
+ describe "#render" do
8
+ it "finds a file" do
9
+ File.should_receive(:read).with("app/views/" + path).and_return('This is erb <%= "olleh".reverse %>')
10
+ expect(renderer.body).to eq "This is erb hello"
11
+ end
12
+
13
+ it "finds a file with a layout" do
14
+ Cynic::Renderer.any_instance.stub(:layout_file).and_return("Layout: <%= yield %> :file" )
15
+ File.should_receive(:read).with("app/views/" + path).and_return('This is erb <%= "olleh".reverse %>')
16
+ expect(renderer.body).to eq "Layout: This is erb hello :file"
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cynic::Response do
4
+ let(:response) { Cynic::Response.new("This is the body") }
5
+ describe "#each" do
6
+ it "responds" do
7
+ expect(response).to respond_to :each
8
+ end
9
+
10
+ it "responds with the body" do
11
+ expect(response.each {|k| k }).to eq "This is the body"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cynic::Routing do
4
+
5
+ let(:routing) { defined_routing }
6
+
7
+ it "calls the method to be returned" do
8
+ expect(routing.go_to(:get, "/")).to eq ""
9
+ end
10
+
11
+ it "calls the method to be returned" do
12
+ expect(routing.go_to(:get, "/blog")).to be_an_instance_of Float
13
+ end
14
+
15
+ it "returns the index" do
16
+ Cynic::Controller.any_instance.stub(:index).and_return("This is erb hello")
17
+ expect(routing.go_to(:get, "/cynic")).to eq "This is erb hello"
18
+ end
19
+
20
+ it "raises an Routing::Error when the path doesn't exist" do
21
+ expect { routing.go_to(:get, "/im-lost") }.to raise_error Cynic::Routing::Error, "undefined routing GET '/im-lost'"
22
+ end
23
+
24
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cynic do
4
+ describe "configuration" do
5
+ before { Cynic.configure {|config| config.environment = :test } }
6
+ it "has configuration" do
7
+ expect(Cynic.configuration).to be_an_instance_of Cynic::Configuration
8
+ end
9
+
10
+ it "is test env" do
11
+ expect(Cynic.configuration.environment).to eq :test
12
+ end
13
+
14
+ it "can reassign env" do
15
+ Cynic.configuration.environment = :development
16
+ expect(Cynic.configuration.environment).to eq :development
17
+ end
18
+
19
+ context "no block given" do
20
+ it "doesn't bork" do
21
+ expect { Cynic.configure }.to_not raise_error
22
+ end
23
+
24
+ it "still assigns a config object" do
25
+ Cynic.configure
26
+ expect(Cynic.configuration).to be_an_instance_of Cynic::Configuration
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ require 'cynic'
2
+ require 'support/routing'
@@ -0,0 +1,5 @@
1
+ Cynic.application.routing.define do |map|
2
+ map.get "/", to: [String, :new]
3
+ map.get "/blog", to: [self, :rand]
4
+ map.get "/cynic", to: [Cynic::Controller.new, :index]
5
+ end
@@ -0,0 +1,8 @@
1
+ def defined_routing(app=nil)
2
+ routing = app.nil? ? Cynic::Routing.new : app.routing
3
+ routing.define do |map|
4
+ map.get "/", to: [String, :new]
5
+ map.get "/blog", to: [self, :rand]
6
+ map.get "/cynic", to: [Cynic::Controller.new, :index]
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cynic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Bookis Smuin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A Lightweight Rack-based framework
56
+ email:
57
+ - bookis.smuin@gmail.com
58
+ executables:
59
+ - cynic
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/cynic
69
+ - cynic.gemspec
70
+ - lib/cynic.rb
71
+ - lib/cynic/app.rb
72
+ - lib/cynic/configuration.rb
73
+ - lib/cynic/controller.rb
74
+ - lib/cynic/renderer.rb
75
+ - lib/cynic/response.rb
76
+ - lib/cynic/routing.rb
77
+ - lib/cynic/version.rb
78
+ - spec/lib/cynic/app_spec.rb
79
+ - spec/lib/cynic/controller_spec.rb
80
+ - spec/lib/cynic/renderer_spec.rb
81
+ - spec/lib/cynic/response_spec.rb
82
+ - spec/lib/cynic/routing_spec.rb
83
+ - spec/lib/cynic_spec.rb
84
+ - spec/spec_helper.rb
85
+ - spec/support/routes.rb
86
+ - spec/support/routing.rb
87
+ homepage:
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.0.6
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Cynic provides a framework with a few tools built on the ideas of rails.
111
+ test_files:
112
+ - spec/lib/cynic/app_spec.rb
113
+ - spec/lib/cynic/controller_spec.rb
114
+ - spec/lib/cynic/renderer_spec.rb
115
+ - spec/lib/cynic/response_spec.rb
116
+ - spec/lib/cynic/routing_spec.rb
117
+ - spec/lib/cynic_spec.rb
118
+ - spec/spec_helper.rb
119
+ - spec/support/routes.rb
120
+ - spec/support/routing.rb