dolphy 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8fbd167c947e2d4bbd1a1b10a985c9c467cd0b0f
4
- data.tar.gz: 0a3f510552e4e3a79e5cab2d81831f9aa6098fb1
3
+ metadata.gz: 1c96763e76f5fb25eed8c6b47adee4e5d3177a89
4
+ data.tar.gz: 2da58ef0dc58f3de39b0db392cbde6f4a913c9ec
5
5
  SHA512:
6
- metadata.gz: 08ab403c88a8e3c710bc72dbe0816813c4a0b70d759acb80d4e9c8e3c0bd2430ff6ab78dcfa379f5d0086e8d27cef8c34c6db7fcecbd816dd3d06343439bfa44
7
- data.tar.gz: f0e12935f25aa99a629a1d9fb61774445747717bebc84cd2e3972878e34f9a7fb508ea02a068dc6568746d519f8db66080024c9f93cf2cc68cffa306e3d10aba
6
+ metadata.gz: 56e384596a888a55b481a6fd0bdaa51cffcd38b0205ea271875d8eb1f727fed0fb039e21e8830658ecc8cf54a1b8a81a6608d1602b32a6363223fd8b1c1aa424
7
+ data.tar.gz: e062e34a7e2e782db698c5a2dc9ea2c74d47e1ca851761573e3476e0cf38e0e1d3bce21ec144fd977f63ee35c870147d5a43b92ad8f1dd707bbfeb9e60bc2c22
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # Dolphy
2
- [![Build Status](https://magnum.travis-ci.com/majjoha/dolphy.svg?token=G5jfvsY2S8tSb5P53rGq)](https://magnum.travis-ci.com/majjoha/dolphy)
2
+ [![Build Status](https://magnum.travis-ci.com/majjoha/dolphy.svg?token=G5jfvsY2S8tSb5P53rGq&branch=master)](https://magnum.travis-ci.com/majjoha/dolphy)
3
3
  [![Code Climate](https://codeclimate.com/repos/53cb2ea469568031fe01ad3f/badges/a7a6a867bc5744c524d7/gpa.png)](https://codeclimate.com/repos/53cb2ea469568031fe01ad3f/feed)
4
4
 
5
5
  Dolphy is an incredibly small (~200 LOC) web framework written in Ruby and based
@@ -13,7 +13,7 @@ used in production.
13
13
  There are a lot of things I want to do to improve the code and functionality of
14
14
  this project. I try to list these things in the
15
15
  [issues](https://github.com/majjoha/dolphy/issues). Feel free to fork this
16
- repository and contribute if you want to help me implement features or fixing
16
+ repository and contribute if you want to help me implement features or fix
17
17
  bugs.
18
18
 
19
19
  ## Requirements
@@ -38,47 +38,139 @@ Or simply install it yourself as:
38
38
  gem install dolphy
39
39
  ```
40
40
 
41
+ Create a file `app.rb`:
42
+
43
+ ```ruby
44
+ require 'dolphy'
45
+
46
+ Dolphy.app do
47
+ Dolphy.router do
48
+ get '/' do
49
+ "Hello, world!"
50
+ end
51
+ end
52
+ end.serve!
53
+ ```
54
+
55
+ Run the file:
56
+
57
+ ```
58
+ ruby app.rb
59
+ ```
60
+
61
+ Open the browser at [http://localhost:8080](http://localhost:8080).
62
+
41
63
  ## Usage
64
+ * [Setup blocks](#setup-blocks)
65
+ * [Template engines](#template-engines)
66
+ * [Routes](#routes)
67
+
68
+ ## Setup blocks
69
+ A Dolphy application has a couple of default configurations that you can change
70
+ based on your needs through the `setup` block.
71
+
72
+ It uses the `Dolphy::TemplateEngines::ErbEngine` by default, and it uses
73
+ `./views/` to find the view files it needs. These can be configured in this way:
74
+
42
75
  ```ruby
43
76
  require 'dolphy'
44
77
 
45
78
  Dolphy.app do
46
79
  setup do |app|
47
- # It is possible to specify the template engine in the config block. If no
48
- # template engine is specified, it will simply default to ERB.
49
80
  app.settings[:template_engine] = Dolphy::TemplateEngines::HamlEngine
50
-
51
- # You can also specify a view path where Dolphy will look for views. It is
52
- # by default set to `./views/`.
53
81
  app.settings[:view_path] = "./somewhere/else/views/"
82
+ end
54
83
 
55
- # Configurations are available to the rest of the application defined in the
56
- # router block, so we could for instance define a title. Basically, you can
57
- # give the settings hash any key value pair that you might find useful to
58
- # store for later use.
84
+ Dolphy.router do
85
+ get '/' do
86
+ "Hello, world!"
87
+ end
88
+ end
89
+ end.serve!
90
+ ```
91
+
92
+ Since the `Dolphy::Settings` is basically just a hash, we can read from and
93
+ write to, we can also define our own settings to use in the views. We could for
94
+ instance define a title for a page by adding `app.settings[:title] = "Building
95
+ things with Dolphy"` in the `setup` block, which we can access in this way:
96
+
97
+ ```ruby
98
+ Dolphy.app do
99
+ setup do |app|
59
100
  app.settings[:title] = "Building things with Dolphy"
60
101
  end
61
102
 
62
103
  Dolphy.router do
63
104
  get '/' do
64
- render :index, { title: settings[:title], body: "Hello!" }
105
+ render :index, title: settings[:title]
65
106
  end
107
+ end
108
+ end.serve!
109
+ ```
66
110
 
67
- get '/login' do
68
- render :login
69
- end
111
+ ## Template engines
112
+ Dolphy supports Haml and ERB by default through the
113
+ `Dolphy::TemplateEngines::HamlEngine` and `Dolphy::TemplateEngine::ErbEngine`
114
+ (which relies on Tilt under the hood), but we can effortlessly build our own
115
+ template engines and use these in our application.
116
+
117
+ A template engine should have a `.render` method with three arguments: a
118
+ `template_name`, a hash with `locals` and finally a `view_path`.
119
+
120
+ Imagine we want our views to use [Liquid](http://liquidmarkup.org/), then we
121
+ just need to define a new `LiquidEngine` and make it our template engine in the
122
+ `setup` block:
70
123
 
71
- post '/post' do
72
- render :post, body: "hello #{params["message"]["name"]}"
124
+ ```ruby
125
+ class LiquidEngine
126
+ def self.render(template_name, locals = {}, view_path = "./views/")
127
+ path = File.expand_path("#{view_path}#{template_name.to_s}.html", Dir.pwd)
128
+ template = Tilt::LiquidTemplate.new(path)
129
+ template.render(Object.new, locals)
130
+ end
131
+ end
132
+ ```
133
+
134
+ ```ruby
135
+ Dolphy.app do
136
+ setup do |app|
137
+ app.settings[:template_engine] = LiquidEngine
138
+ end
139
+
140
+ Dolphy.router do
141
+ get '/' do
142
+ render :new_page
73
143
  end
144
+ end
145
+ end.serve!
146
+ ```
74
147
 
148
+ ## Routes
149
+ The routing layer should look familiar if you've already worked with other Ruby
150
+ frameworks. We can override routes with variables by adding a new route, and we
151
+ use `:name` to access variables in the routes:
152
+
153
+ ```ruby
154
+ require 'dolphy'
155
+
156
+ Dolphy.app do
157
+ Dolphy.router do
75
158
  get '/hello/:name' do |name|
76
159
  render :hello, name: name
77
160
  end
161
+
162
+ get '/hello/you'
163
+ render :hello_you
164
+ end
78
165
  end
79
166
  end.serve!
80
167
  ```
81
168
 
169
+ In this case, any request to `/hello/bob` will render the `hello` template with
170
+ the `name` variable set to `"bob"`, while any request to `/hello/you` will
171
+ simply just render the `hello_you` template. The opposite order will not work,
172
+ so the ordering is quite important.
173
+
82
174
  ## Inspiration
83
175
  I've been looking in my directions for inspiration. I probably owe some credit
84
176
  to [Camping](http://camping.io), [Cuba](http://cuba.is),
data/lib/dolphy/core.rb CHANGED
@@ -30,9 +30,7 @@ module Dolphy
30
30
  end
31
31
 
32
32
  def render(template_name, locals = {})
33
- Dolphy::TemplateEngine.new(settings[:template_engine],
34
- settings[:view_path]).
35
- render(template_name, locals)
33
+ template_engine.render(template_name, locals)
36
34
  end
37
35
 
38
36
  def redirect_to(path, status = 302)
@@ -54,7 +52,7 @@ module Dolphy
54
52
 
55
53
  router.find_route_for(request).each do |matcher, block|
56
54
  if match = router.find_match_data_for(request, with: matcher)
57
- response.body << block.call(*match.captures)
55
+ response.body = [block.call(*match.captures)]
58
56
  end
59
57
  end
60
58
 
@@ -65,6 +63,13 @@ module Dolphy
65
63
 
66
64
  private
67
65
 
66
+ def template_engine
67
+ @template_engine ||= Dolphy::TemplateEngine.new(
68
+ settings[:template_engine],
69
+ settings[:view_path]
70
+ )
71
+ end
72
+
68
73
  attr_accessor :response, :router, :request
69
74
  end
70
75
  end
@@ -1,3 +1,3 @@
1
1
  module Dolphy
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
data/spec/dolphy_spec.rb CHANGED
@@ -19,6 +19,12 @@ describe Dolphy do
19
19
  expect(page).to have_content "hello world!"
20
20
  end
21
21
 
22
+ it "returns the content of the last defined route" do
23
+ visit '/hello/login'
24
+ expect(page).to have_content "this is a login"
25
+ expect(page).to_not have_content "hello login"
26
+ end
27
+
22
28
  it "returns a page using the input from the matcher" do
23
29
  visit '/hello/davis'
24
30
  expect(page).to have_content "hello davis"
data/spec/spec_helper.rb CHANGED
@@ -32,6 +32,10 @@ app = Dolphy.app do
32
32
  get '/hello/:name' do |name|
33
33
  "hello #{name}"
34
34
  end
35
+
36
+ get '/hello/login' do
37
+ get "this is a login"
38
+ end
35
39
  end
36
40
  end
37
41
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dolphy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathias Jean Johansen