atd 0.1.0.TEST.GITLAB1 → 0.2.0.TEST.GITLAB1

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: 7632cff0dea940480d94ff9edf270c40cb867d84
4
- data.tar.gz: 51bb58c9651e635c8ed24d15a5f1ae1f7af3915f
3
+ metadata.gz: 5aee51c24722e0377807e99f47cbe847ceab08db
4
+ data.tar.gz: 7ce85f825e084f24fdcebd4bfe1ae835af651959
5
5
  SHA512:
6
- metadata.gz: b47eb2b7b87ba4fa7b62f73a32da06c2e239b0aed06f63812fed6d3c94596a093a483be6b8594f236ef1d33059bcbf22916cf20e095d747b6a73f0b16feb3b82
7
- data.tar.gz: 42c78f99c567e0b102b0b2138696a9de72291ac15295fd400ef49533535a73c7c1ceb69dffe332ae3dafc482235eb21695da1929ac90a84c960d2e641346a4bc
6
+ metadata.gz: cea8be538401bbc9e7c466928afc3bb8357b8d5b9feababd544373c91630bf52e6ad77f220e9a6606f463322505b511ed7bbff51d60bfa86bb83d78bf588c004
7
+ data.tar.gz: 9a6f3bd0188a78d9706c473d4446992ab2f758d18a16d6524f088e4057ddedf76ed09c3d7ab2b92da418dd33e06b53a7987d82bf5073f8c85ea50b0b9896d107
data/CHANGELOG CHANGED
@@ -1,5 +1,14 @@
1
1
  Repository Version:
2
- - Nothing different
2
+
3
+ v0.2.0
4
+ - Changed git workflow, now with 2 branches.
5
+ - Fixed some broken compiler issues
6
+ - Allowed routes to skip precompilation with precompile: false
7
+ - Changed :methods to :respond_to
8
+ - Allowed :ignore [http_methods] syntax
9
+ - Added documentation for routes.rb
10
+ - Updated README, added modification of instances feature
11
+ - Minor restructuring
3
12
 
4
13
  v0.1.0:
5
14
  - Created CHANGELOG!
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  [![build status][build status badge]][build status link] [![coverage report][coverage report badge]][coverage report link] [![gem version][gem version badge]][gem version link] [![documentation coverage][documentation coverage badge]][documentation coverage link]
2
2
 
3
- [documentation coverage badge]: https://img.shields.io/badge/YARD%20coverage-77.50%-yellow.svg?style=flat-square
3
+ [documentation coverage badge]: https://img.shields.io/badge/YARD%20coverage-93.02%-green.svg?style=flat-square
4
4
  [documentation coverage link]: http://izwick-schachter.gitlab.io/atd/YARD/coverage
5
5
 
6
6
  [build status badge]: https://gitlab.com/izwick-schachter/atd/badges/master/build.svg?style=flat-square
@@ -34,21 +34,23 @@ And then execute:
34
34
 
35
35
  $ bundle
36
36
 
37
- Or install it yourself as:
37
+ Or install it from the repository with:
38
38
 
39
39
  $ git clone https://gitlab.com/izwick-schachter/atd.git
40
40
  $ cd atd
41
41
  $ bundle exec rake install
42
42
 
43
+ This will also allow you to use the current development version (USE WITH CAUTION), and to use that you can `git checkout development` and then `bundle exec rake install`.
44
+
43
45
  ## Usage
44
46
 
45
47
  ### Setup
46
48
 
47
49
  Setup is as easy as `require "atd"` at the top of whatever file you need it in
48
50
 
49
- ### Routing
51
+ ## Routing
50
52
 
51
- #### Basic Routing
53
+ ### Basic Routing
52
54
 
53
55
  On the lowest level it can be used serve files or any strings when a path is requested (and optionally when a specific HTTP method is used). Here is a basic route that will serve `Hello World` when `/` is sent any HTTP request (`GET`, `POST`, `PUT`, `PATCH`, `DELETE`):
54
56
 
@@ -100,7 +102,38 @@ Or, a simpler way would be with `:ignore`:
100
102
  request "/", "Hello World", ignore: :delete
101
103
  ```
102
104
 
103
- #### Advanced Routing
105
+ ### Advanced Routing
106
+
107
+ #### DefaultApp
108
+
109
+ `DefaultApp` is just another app which extends `ATD::App`, it is the app which doesn't have to be written to in `class DefaultApp`, any routes that are created in `main` will be added to `DefaultApp`, well... by default!
110
+
111
+ #### Inheritance
112
+
113
+ Whenever you create a route, it is given to an App Class (which inherits from `ATD::App`). This isn't apparent when you create a route in `main`, but even when you do that the route is added to `DefaultApp`. If you are using Apps, than when you create a route in the App Class, that route is given to that Class. When you start the server, it then creates an instance of the App Class and starts it as a rack app. But that is not what the purpose of Apps are.
114
+
115
+ The intention of Apps are to allow you to use one App Class as a template class, from which you can create many different apps. An App Class is not a rack app all by itself. Every instance of an App Class is a rack app. But the rack app doesn't actually start until `start` is called. This means that you can create an instance of an App Class (an App), and then you can modify it before starting it. So for example, you can have an app which is impacted by an instance variable:
116
+
117
+ ```ruby
118
+ class MyApp < ATD::App
119
+ attr_accessor :my_name
120
+ request "/", "Hi! This is my_name's App!" do
121
+ @http[:output] = @http[:output].gsub("my_name", @my_name)
122
+ end
123
+ end
124
+ ```
125
+
126
+ Which you can then create an instance of and modify the instance variable:
127
+
128
+ ```ruby
129
+ app = MyApp.new
130
+ app.my_name = "Fredrick"
131
+ app.start
132
+ ```
133
+
134
+ Then when you try to access the website, it will respond to `/` with `Hi! This is Fredrick's App!`.
135
+
136
+ #### Blocks
104
137
 
105
138
  You can also add blocks to the basic routing methods to execute code when they are reached, and customize the response to the incoming request. Here is an example that will return the HTTP method used to request `/`:
106
139
 
@@ -225,4 +258,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
225
258
 
226
259
  ## Contributing
227
260
 
228
- Bug reports and pull requests are welcome on GitLab at https://gitlab.com/izwick-schachter/atd/issues. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
261
+ Bug reports and pull requests are welcome on GitLab at https://gitlab.com/izwick-schachter/atd/issues. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
data/atd.gemspec CHANGED
@@ -11,14 +11,14 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = 'The assistant technical director of your website. It does the dirty work so you can see the big picture.'
13
13
  spec.homepage = "https://rubygems.org/gems/atd"
14
- spec.license = "MIT"
14
+ spec.license = "MIT"
15
15
 
16
16
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
17
  # to allow pushing to a single host or delete this section to allow pushing to any host.
18
18
  raise "RubyGems 2.0 or newer is required to protect against public gem pushes." unless spec.respond_to?(:metadata)
19
19
  spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
20
 
21
- spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }#+"test/assets/test.html"
21
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
22
  spec.bindir = "exe"
23
23
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ["lib"]
data/lib/atd.rb CHANGED
@@ -69,7 +69,7 @@ module ATD
69
69
  define_method(method) do |*args, &block|
70
70
  @method = [method] if @method.length == 5
71
71
  @method += [method]
72
- @method += args.last[:methods] if args.last.is_a?(Hash) && !args.last[:methods].nil?
72
+ @method += args.last[:respond_to] if args.last.is_a?(Hash) && !args.last[:respond_to].nil?
73
73
  @method.uniq!
74
74
  parse_args(*args, &block)
75
75
  end
@@ -99,15 +99,16 @@ module ATD
99
99
  @path = args.shift if @path.nil?
100
100
  @output = args.shift if @output.nil?
101
101
  @args = Array(@args).concat(args) - [nil] unless args.nil?
102
+ @method -= Array(args.last[:ignore]) if args.last.is_a?(Hash) && !args.last[:ignore].nil?
102
103
  self
103
104
  end
104
105
  end
105
106
 
106
107
  # A template {App} that all Apps extend. When a new App is created with {ATD.new ATD.new} it extends this class.
107
108
  class App
109
+ attr_accessor :http
108
110
  class << self
109
111
  attr_accessor :routes # An array of instances of {ATD::Route} that belong to this {App}.
110
- attr_accessor :http
111
112
 
112
113
  # Generates an instance of {ATD::Route}.
113
114
  # Passes all arguments and the block to {Route.new the constructor} and sets the app where it was called from.
@@ -129,7 +130,7 @@ module ATD
129
130
  # @param [Class] server The server that you would like to use.
130
131
  # @param [Fixnum] port The port you would like the server to run on.
131
132
  def start(server = WEBrick, port = 3150)
132
- Rack::Server.start(app: self.new, server: server, Port: port)
133
+ Rack::Server.start(app: new, server: server, Port: port)
133
134
  end
134
135
  end
135
136
 
@@ -152,16 +153,29 @@ module ATD
152
153
  # }
153
154
  # @param [Array] routes An array of instances of {ATD::Route}.
154
155
  def initialize(routes = [])
155
- routes += self.class.routes
156
156
  @routes = {}
157
- routes.each do |route|
158
- filename = ATD::Compilation.pre_parse(route)
157
+ Array(routes + self.class.routes).each do |route|
158
+ filename = ATD::Compilation.pre_parse(route) if route.args.last.nil? || route.args.last[:precompile].nil? || route.args.last[:precompile]
159
159
  route_hash = route.to_h
160
- route_hash[route.path][route.method][:filename] = filename
160
+ current_route = route_hash[route.path][route.method]
161
+ current_route[:filename] = filename
162
+ block = current_route[:block]
163
+ current_route[:block] = define_singleton_method(block.object_id.to_s.tr("0-9", "a-j").to_sym, &block) unless block.nil?
161
164
  @routes = @routes.to_h.deep_merge(route_hash)
162
165
  end
163
166
  end
164
167
 
168
+ def request(*args, &block)
169
+ route = ATD::Route.new(*args, &block)
170
+ filename = ATD::Compilation.pre_parse(route) if route.args.last.nil? || route.args.last[:precompile].nil? || route.args.last[:precompile]
171
+ route_hash = route.to_h
172
+ route_hash[route.path][route.method][:filename] = filename
173
+ @routes = @routes.to_h.deep_merge(route_hash)
174
+ route
175
+ end
176
+ alias req request
177
+ alias r request
178
+
165
179
  # Starts the rack server
166
180
  # @param [Class] server The server that you would like to use.
167
181
  # @param [Fixnum] port The port you would like the server to run on.
@@ -173,6 +187,7 @@ module ATD
173
187
  # It will return status code 200 and whatever output corresponds the that route if it exists, and if it doesn't
174
188
  # it will return status code 404 and the message "Error 404"
175
189
  def call(env)
190
+ @http = nil
176
191
  route = route(env)
177
192
  return error(404) if route.nil?
178
193
  route[:output] = ATD::Compilation.compile(route[:filename], route[:output])
@@ -190,12 +205,12 @@ module ATD
190
205
  end
191
206
 
192
207
  def run_block(block)
193
- block.call
194
- [self.class.http[:status_code].to_i, Hash(self.class.http[:headers]), Array(self.class.http[:output])]
208
+ method(block).call
209
+ [@http[:status_code].to_i, Hash(@http[:headers]), Array(@http[:output])]
195
210
  end
196
211
 
197
212
  def http(additional_params)
198
- self.class.http = { status_code: 200, headers: {} }.merge(additional_params)
213
+ @http = { status_code: 200, headers: {} }.merge(additional_params)
199
214
  end
200
215
 
201
216
  def error(number)
data/lib/atd/routes.rb CHANGED
@@ -1,21 +1,31 @@
1
1
 
2
2
  module ATD
3
+ # This module holds everything related to the compilation of routes.
3
4
  module Compilation
5
+ # A module designed to hold all the precompilation methods
4
6
  module Precompiler
5
7
  extend self
8
+
9
+ # Lists all filestypes that have defined precompiler methods
6
10
  def filetypes
7
11
  instance_methods(true) - [:filetypes]
8
12
  end
9
13
  end
14
+
15
+ # A module designed to hold all the compilation methods
10
16
  module Compiler
11
17
  extend self
18
+
19
+ # Lists all file extenstions which have defined compiler methods
12
20
  def filetypes
13
21
  instance_methods(true) - [:filetypes]
14
22
  end
15
23
  end
16
24
 
25
+ # This method is responsible for live compilation.
17
26
  def self.compile(name, contents)
18
- return contents if name.nil?
27
+ return contents if name.nil? && !contents.is_a?(File)
28
+ name = File.basename(contents) if contents.is_a?(File)
19
29
  name = name.split(".")
20
30
  extensions = name - [name.first]
21
31
  extensions.each do |extension|
@@ -24,14 +34,15 @@ module ATD
24
34
  contents
25
35
  end
26
36
 
37
+ # This method is responsible for precompilation
27
38
  def self.pre_parse(route)
28
39
  output = route.output
29
40
  name = output
30
41
  return nil if output.nil?
31
42
  if output.include?(".") || output.is_a?(File)
32
- file = read_file(output)
33
- return nil if file.nil?
34
43
  name = (output.is_a?(File) ? File.basename(output) : output)
44
+ file = read_file(output)
45
+ return name if file.nil?
35
46
  file = precompile(name, file)
36
47
  route.output = file
37
48
  end
data/lib/atd/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module ATD
2
- VERSION = "0.1.0.TEST.GITLAB1".freeze # The current version number of {ATD}.
2
+ VERSION = "0.2.0.TEST.GITLAB1".freeze # The current version number of {ATD}.
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atd
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.TEST.GITLAB1
4
+ version: 0.2.0.TEST.GITLAB1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ACecretMaster
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-08 00:00:00.000000000 Z
11
+ date: 2016-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack