elektra 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 48a0342870563bdb0bad25319cb83a6e297ba931
4
+ data.tar.gz: 0167c431016a7450929f3d7ee2346c0c397c07d0
5
+ SHA512:
6
+ metadata.gz: e61563ac12e1e3976d50b6e0f6616f45af56f3e7718e02f123762bb03164331cf355ffb710f58364da83cc001ada0afad249c1dad3017385b2ae35001755b345
7
+ data.tar.gz: ed0a32786340b1a1344dee46f5d36ca0bc5574a822e8b798b16577614f733a853ecb7f94f4cc683e681b4f0c11db354b235d1f4d70171bdb1c886c5ec2e3c8d0
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ elektra-0.1.0.gem
11
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ ruby "2.2.3"
4
+ # Specify your gem's dependencies in elektra.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ikem Okonkwo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Elektra
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/elektra`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'elektra'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install elektra
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/elektra. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "elektra"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/elektra.gemspec ADDED
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'elektra/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "elektra"
8
+ spec.version = Elektra::VERSION
9
+ spec.authors = ["Ikem Okonkwo"]
10
+ spec.email = ["ikem.okonkwo@andela.com"]
11
+
12
+ spec.summary = %q{A ruby microframework for bulding small web application}
13
+ spec.description = %q{A framework inspired by sinatra}
14
+ spec.homepage = "https://github.com/andela-iokonkwo/elektra"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.10"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec"
33
+ spec.add_development_dependency "webmock"
34
+ spec.add_development_dependency "rack"
35
+ spec.add_development_dependency "pry"
36
+
37
+ spec.add_runtime_dependency "slim"
38
+
39
+
40
+ end
@@ -0,0 +1,105 @@
1
+ require "rack"
2
+ require "pry"
3
+ require_relative "config"
4
+ require_relative "filters"
5
+ require_relative "helpers"
6
+ require_relative "redirect"
7
+ require_relative "render"
8
+ require_relative "routes"
9
+
10
+
11
+ module Elektra
12
+ class Base
13
+ attr_reader :response, :request
14
+
15
+ %w(params body).each do |method_name|
16
+ define_method(method_name) do
17
+ @request.send(method_name)
18
+ end
19
+ end
20
+
21
+ %w(status header).each do |method_name|
22
+ define_method(method_name) do |*args|
23
+ @response.send("#{method_name}=", *args)
24
+ end
25
+ end
26
+
27
+ def halt(*response)
28
+ response = response.first if response.length == 1
29
+ update_response_with response
30
+ throw :halt
31
+ end
32
+
33
+ def call(env)
34
+ @request = Rack::Request.new(env)
35
+ @response = Rack::Response.new
36
+ generate_response_for_request
37
+ @response.finish
38
+ end
39
+
40
+
41
+
42
+ def path_and_verb
43
+ [@request.path_info, @request.request_method.downcase.to_sym]
44
+ end
45
+
46
+ def generate_response_for_request
47
+ catch :halt do
48
+ execute_block_result = execute_block_and_before_filters
49
+ if execute_block_result
50
+ update_response_with execute_block_result
51
+ execute_after_filters
52
+ return
53
+ end
54
+ halt 404
55
+ end
56
+ end
57
+
58
+ def update_response_with(execute_block_result)
59
+ if execute_block_result.is_a? String
60
+ @response.write execute_block_result
61
+ elsif execute_block_result.is_a? Fixnum
62
+ @response.status = execute_block_result
63
+ elsif execute_block_result.length == 3
64
+ @response.status = execute_block_result[0]
65
+ execute_block_result[1].each { |key, value| @response[key] = value }
66
+ @response.body = execute_block_result[2]
67
+ elsif execute_block_result.length == 2
68
+ @response.status = execute_block_result[0]
69
+ @response.body = execute_block_result[1]
70
+ elsif execute_block_result.respond_to?(:each)
71
+ @response.body = execute_block_result
72
+ end
73
+ end
74
+
75
+ def execute_block_and_before_filters
76
+ block_to_execute = get_block_for_request
77
+
78
+ # require "pry"; binding.pry
79
+ self.class.filter_collection[:before].each do |before_filter|
80
+ instance_eval(&before_filter)
81
+ end
82
+ instance_eval(&block_to_execute)
83
+ end
84
+
85
+ def execute_after_filters
86
+ self.class.filter_collection[:after].each do |after_filter|
87
+ instance_eval(&after_filter)
88
+ end
89
+ end
90
+
91
+
92
+ def get_block_for_request
93
+ path, verb = path_and_verb
94
+ self.class.endpoints[verb].each do |route, block|
95
+ pattern, placeholders = route
96
+ if path =~ route[0]
97
+ placeholders.each_with_index do |placeholder, index|
98
+ @request.update_param(placeholder, eval("$#{index + 1}"))
99
+ end
100
+ return block
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,12 @@
1
+ module Elektra
2
+ class Base
3
+ @@configuration = { public_folder: 'public',
4
+ views_folder: 'views',
5
+ layout: 'layout' }
6
+ class << self
7
+ def set(key, value)
8
+ @@configuration[key] = value
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ module Elektra
2
+ class Base
3
+ class << self
4
+ %w{before after}.each do |filter|
5
+ define_method(filter) do |&block|
6
+ filter_collection[filter.to_sym] << block
7
+ end
8
+ end
9
+
10
+ def filter_collection
11
+ @filters ||= Hash.new { |hash, key| hash[key] = [] }
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,24 @@
1
+ module Elektra
2
+ class Base
3
+ class << self
4
+
5
+ def helpers(*args)
6
+ if block_given?
7
+ command = Proc.new { yield }
8
+ class_eval(&command)
9
+ helpers_collection << command
10
+ else
11
+ args.each do |helper|
12
+ command = "include #{ helper}"
13
+ class_eval(command)
14
+ helpers_collection << command
15
+ end
16
+ end
17
+ end
18
+
19
+ def helpers_collection
20
+ @helpers ||= []
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module Elektra
2
+ class Base
3
+ def redirect(*response)
4
+ redirect_path = response.shift
5
+ @response.redirect(redirect_path)
6
+ update_response_with response
7
+ throw :halt
8
+ end
9
+
10
+ def to(path)
11
+ port = ":#{@request.port}" unless @request.port == 80
12
+ "#{@request.host}#{port}#{path}"
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ require 'slim'
2
+ module Elektra
3
+ class Base
4
+ def render(template, layout: true)
5
+ filename = File.join(@@configuration[:views_folder], "#{template}.slim")
6
+ @response["Content-Type"] = "text/html"
7
+ scope = Object.new
8
+ instance_variable_to_view.each { |key, value| scope.instance_variable_set(key, value) }
9
+ compiled_template = Slim::Template.new(filename).render(scope)
10
+ compiled_template = Slim::Template.new("#{@@configuration[:views_folder]}/#{@@configuration[:layout]}.slim").
11
+ render { compiled_template } if layout
12
+ compiled_template
13
+ end
14
+
15
+ def instance_variable_to_view
16
+ var_to_be_passed = instance_variables - [:@request, :@response]
17
+ var_to_be_passed .map { |name| [name, instance_variable_get(name)] }
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,33 @@
1
+
2
+ module Elektra
3
+ class Base
4
+ attr_reader :response, :request
5
+
6
+ class << self
7
+
8
+ def self.route_methods(*methods)
9
+ Array(methods).each do |method_name|
10
+ define_method(method_name) do |*args, &block|
11
+ pattern = args[0]
12
+ endpoints[method_name] << [get_path(pattern), block]
13
+ end
14
+ end
15
+ end
16
+
17
+ route_methods :get, :patch, :put, :post, :delete, :head
18
+
19
+ def endpoints
20
+ @endpoints ||= Hash.new { |hash, key| hash[key] = [] }
21
+ end
22
+
23
+ def get_path(pattern)
24
+ placeholders = []
25
+ pattern.gsub!(/(:\w+)/) do |match|
26
+ placeholders << $1[1..-1]
27
+ "([^/?#]+)"
28
+ end
29
+ [%r{^#{pattern}$}, placeholders]
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Elektra
2
+ VERSION = "0.1.0"
3
+ end
data/lib/elektra.rb ADDED
@@ -0,0 +1,24 @@
1
+ require "elektra/base"
2
+ require "elektra/version"
3
+
4
+ module Elektra
5
+
6
+ module Delegator
7
+
8
+ methods_to_define = Base.methods - Object.methods
9
+
10
+ def self.delegate(methods, to:)
11
+ puts methods
12
+ methods.each do |method_name|
13
+ define_method(method_name) do |*args, &block|
14
+ to.send(method_name, *args, &block)
15
+ end
16
+ end
17
+ end
18
+
19
+ delegate methods_to_define, to: Base
20
+ Application = Base.new
21
+ end
22
+ end
23
+
24
+ include Elektra::Delegator
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elektra
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ikem Okonkwo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: slim
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: A framework inspired by sinatra
112
+ email:
113
+ - ikem.okonkwo@andela.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - elektra.gemspec
129
+ - lib/elektra.rb
130
+ - lib/elektra/base.rb
131
+ - lib/elektra/config.rb
132
+ - lib/elektra/filters.rb
133
+ - lib/elektra/helpers.rb
134
+ - lib/elektra/redirect.rb
135
+ - lib/elektra/render.rb
136
+ - lib/elektra/routes.rb
137
+ - lib/elektra/version.rb
138
+ homepage: https://github.com/andela-iokonkwo/elektra
139
+ licenses:
140
+ - MIT
141
+ metadata:
142
+ allowed_push_host: https://rubygems.org
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.4.5.1
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: A ruby microframework for bulding small web application
163
+ test_files: []