sinatra_bicyclist 0.1.0

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: 6dd623dcfeee2a86c6f472b1801b2c07f9e2c9f0
4
+ data.tar.gz: 64954cb95a7eeb1493fc5b649585eff62100b3b1
5
+ SHA512:
6
+ metadata.gz: 3d50bfcc4d50c04ab38d240144d64703ee8c02d1befdccf4c83980bbf041fcc24002cb566fd455844857a59c5e23f3f3dbe844baba713fc30480d0cea1d9bb96
7
+ data.tar.gz: 33e8eddc21a9ae711176fff87920184e2d5cfce469001a46d696eda5dd417fa4cb9cefdb461a8389848889e56bd40e95e463eb48154dd5598da0db9954aa722f
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .*.swp
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ .idea
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - rbx-2
data/CHANGELOG.md ADDED
@@ -0,0 +1,12 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ ## 0.1.0 - 2014-09-30
5
+ ### Added
6
+ - support for more cycle groups
7
+ now you can specify `routes_to_cycle_through` as a hash
8
+ then it picks one of the keys and starts cycling all values of given key
9
+ - a "Change Log"
10
+
11
+ ### Changed
12
+ - Changed project name to sinatra_bicyclist
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sinatra_cyclist.gemspec
4
+ gemspec
5
+
6
+
7
+ group :development do
8
+ gem 'pry-byebug', platforms: [:mri_20, :mri_21]
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Michael Lavrisha
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,124 @@
1
+ # SinatraBicyclist
2
+
3
+ This is a fork of SinatraCyclist. It adds functionality to have more than one cycling loops.
4
+ So you can have `/_cycle/apps` and `/_cycle/ops` to loop through different pages.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'sinatra_bicyclist'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install sinatra_bicyclist
19
+
20
+ Installation into your code depends on how you are using Sinatra.
21
+
22
+ ### Dashing
23
+ If you are using [dashing](https://github.com/Shopify/dashing) update your `config.ru` to look something like:
24
+
25
+ ```ruby
26
+ require "sinatra/cyclist"
27
+ require 'dashing'
28
+
29
+ configure do
30
+ set :auth_token, 'YOUR_AUTH_TOKEN'
31
+
32
+ helpers do
33
+ def protected!
34
+ # Put any authentication code you want in here.
35
+ # This method is run before accessing any resource.
36
+ end
37
+ end
38
+ end
39
+
40
+ map Sinatra::Application.assets_prefix do
41
+ run Sinatra::Application.sprockets
42
+ end
43
+
44
+ set :routes_to_cycle_through, [:dashboard_1, :dashboard_2]
45
+
46
+ run Sinatra::Application
47
+ ```
48
+
49
+ * Require `sinatra_cyclist` before `dashing` otherwise you will see this error:
50
+
51
+ > No such file or directory - sample/dashboards/_cycle.erb
52
+
53
+ * Set the `routes_to_cycle_through` before running the application.
54
+
55
+ ### Classic Applications
56
+ Require the gem and specify the routes you want to cycle through.
57
+
58
+ ```ruby
59
+ require "sinatra"
60
+ require "sinatra/cyclist"
61
+
62
+ set :routes_to_cycle_through, [:page_1, :page_2]
63
+
64
+ get "/page_1" do
65
+ "Page 1"
66
+ end
67
+
68
+ get "/page_2" do
69
+ "Page 2"
70
+ end
71
+ ```
72
+
73
+ ### Modular Applications
74
+ Require the gem, explicitly register the extension, and specify the routes.
75
+ ```ruby
76
+ require "sinatra/base"
77
+ require "sinatra/cyclist"
78
+
79
+ class MyApp < Sinatra::Base
80
+ register Sinatra::Cyclist
81
+
82
+ set :routes_to_cycle_through, [:page_1, :page_2]
83
+
84
+ get "/page_1" do
85
+ "Page 1"
86
+ end
87
+
88
+ get "/page_2" do
89
+ "Page 2"
90
+ end
91
+ end
92
+ ```
93
+
94
+ ## Usage
95
+ Now visit `/_cycle` to start cycling!
96
+
97
+ You can also specify a duration (in seconds) in the params to the cycle action
98
+
99
+ ```
100
+ http://sinatra_app.com/_cycle?duration=10
101
+ ```
102
+
103
+ ## Advanced Usage
104
+
105
+ If you want to have different cycles for different dashboards you can define:
106
+
107
+ ```
108
+ set :routes_to_cycle_through, {
109
+ applications: [:dash1, :dash2, :dash3],
110
+ operations: [:dash4, :dash5, :dash6]
111
+ }
112
+ ```
113
+
114
+ Then visiting `/_cycle/applications` will go just through dashboards dash1, dash2 and dash3.
115
+
116
+ ## Contributing
117
+
118
+ 1. Fork it
119
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
120
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
121
+ 4. Push to the branch (`git push origin my-new-feature`)
122
+ 5. Create new Pull Request
123
+
124
+ ![](http://24.media.tumblr.com/tumblr_lhgo817Gnw1qf6o97o1_500.jpg)
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ begin
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec)
7
+ task default: :spec
8
+ rescue LoadError
9
+ end
@@ -0,0 +1,12 @@
1
+ require "sinatra"
2
+ require "sinatra/cyclist"
3
+
4
+ set :routes_to_cycle_through, [:page_1, :page_2]
5
+
6
+ get "/page_1" do
7
+ "Page 1"
8
+ end
9
+
10
+ get "/page_2" do
11
+ "Page 2"
12
+ end
@@ -0,0 +1,2 @@
1
+ require "./modular_application"
2
+ run MyApp
@@ -0,0 +1,16 @@
1
+ require "sinatra/base"
2
+ require "sinatra/cyclist"
3
+
4
+ class MyApp < Sinatra::Base
5
+ register Sinatra::Cyclist
6
+
7
+ set :routes_to_cycle_through, [:page_1, :page_2]
8
+
9
+ get "/page_1" do
10
+ "Page 1"
11
+ end
12
+
13
+ get "/page_2" do
14
+ "Page 2"
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ require 'sinatra/base'
2
+ require_relative 'bicyclist/version'
3
+ require_relative 'bicyclist/cycler'
4
+
5
+ module Sinatra
6
+ module Bicyclist
7
+ def self.registered(app)
8
+ app.enable :sessions
9
+ app.set :routes_to_cycle_through, {}
10
+ app.set :cycle_duration, 3
11
+ app.set :cycler, Sinatra::Bicyclist::Cycler.new(app.settings)
12
+
13
+ app.get '/_cycle' do
14
+ session[:_cycle_duration] = params[:duration] if params.has_key?('duration')
15
+
16
+ page = settings.cycler.page(session)
17
+ redirect settings.cycler.group ? "/_cycle/#{page}" : "/#{page}"
18
+ end
19
+
20
+ app.get '/_cycle/:group' do
21
+ session[:_cycle_duration] = params[:duration] if params.has_key?('duration')
22
+
23
+ page = settings.cycler.page(session, params[:group])
24
+ redirect "/#{page}"
25
+ end
26
+
27
+ app.before do
28
+ session[:_cycle_duration] ||= settings.cycle_duration
29
+
30
+ if group = session.delete(:_cycle)
31
+ headers["Refresh"] = "#{session[:_cycle_duration]}; url=/_cycle/#{group}"
32
+ session[:_last_cycle] = group
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ register Bicyclist
39
+ end
@@ -0,0 +1,58 @@
1
+ module Sinatra
2
+ module Bicyclist
3
+ class Cycler
4
+ attr_accessor :duration
5
+
6
+ def initialize(settings)
7
+ @settings = settings
8
+ end
9
+
10
+ def routes
11
+ @settings.routes_to_cycle_through
12
+ end
13
+
14
+ def has_groups?
15
+ Hash === @settings.routes_to_cycle_through
16
+ end
17
+
18
+ def pages(group = nil)
19
+ return routes[group.to_sym] if has_group?(group)
20
+
21
+ case routes
22
+ when Array then routes
23
+ when Hash then routes.keys
24
+ end
25
+ end
26
+
27
+ def has_group?(name)
28
+ name && has_groups? && pages.map(&:to_s).include?(name.to_s)
29
+ end
30
+
31
+ def group
32
+ has_groups?
33
+ end
34
+
35
+ def page_index(session)
36
+ page_index = session[:_cycle_page_index] || -1
37
+ session[:_cycle_page_index] = page_index + 1
38
+ end
39
+
40
+ def random_route(session, group)
41
+ number_of_routes = pages(group).length
42
+ pages(group)[page_index(session) % number_of_routes]
43
+ end
44
+
45
+ def reset_index(session)
46
+ session[:_cycle_page_index] = -1
47
+ end
48
+
49
+ def page(session, group = nil)
50
+ reset_index(session) if group.to_s != session[:_last_cycle].to_s
51
+ page = random_route(session, group)
52
+ session[:_cycle] = group || (has_groups? && page)
53
+ page
54
+ end
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module Bicyclist
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sinatra/bicyclist/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Michael Lavrisha", 'Michal Cichra']
6
+ gem.email = ["michael.lavrisha@gmail.com", 'michal.cichra@gmail.com']
7
+ gem.description = %q{Cycle through pages at a regular interval}
8
+ gem.summary = %q{Sinatra can ride a bicycle over and over and over again}
9
+ gem.homepage = "https://github.com/mikz/sinatra_bicyclist"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "sinatra_bicyclist"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Sinatra::Bicyclist::VERSION
17
+
18
+ gem.add_dependency "sinatra"
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+ gem.add_development_dependency 'rack-test'
23
+ end
@@ -0,0 +1,26 @@
1
+ require 'sinatra'
2
+ require 'sinatra/bicyclist'
3
+
4
+ set :routes_to_cycle_through, {
5
+ one: [:foo],
6
+ two: [:bar, :baz]
7
+ }
8
+
9
+ RSpec.describe Sinatra::Application do
10
+
11
+ context '_cycle' do
12
+ before { get '/_cycle' }
13
+ subject(:response) { last_response }
14
+
15
+ it { expect(response.status).to eq(302) }
16
+ it { expect(response.headers).to include('Location' => 'http://example.org/_cycle/one') }
17
+
18
+
19
+ context 'two' do
20
+ before { get '/_cycle/two' }
21
+
22
+ it { expect(response.status).to eq(302) }
23
+ it { expect(response.headers).to include('Location' => 'http://example.org/bar') }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,90 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
4
+ # file to always be loaded, without a need to explicitly require it in any files.
5
+ #
6
+ # Given that it is always loaded, you are encouraged to keep this file as
7
+ # light-weight as possible. Requiring heavyweight dependencies from this file
8
+ # will add to the boot time of your test suite on EVERY test run, even for an
9
+ # individual file that may not need all of that loaded. Instead, consider making
10
+ # a separate helper file that requires the additional dependencies and performs
11
+ # the additional setup, and require it from the spec files that actually need it.
12
+ #
13
+ # The `.rspec` file also contains a few flags that are not defaults but that
14
+ # users commonly want.
15
+ #
16
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
17
+
18
+ require 'rack/test'
19
+
20
+ require_relative 'support/rack'
21
+
22
+ RSpec.configure do |config|
23
+ # rspec-expectations config goes here. You can use an alternate
24
+ # assertion/expectation library such as wrong or the stdlib/minitest
25
+ # assertions if you prefer.
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ config.include RackSupport
47
+
48
+ # The settings below are suggested to provide a good initial experience
49
+ # with RSpec, but feel free to customize to your heart's content.
50
+ # These two settings work together to allow you to limit a spec run
51
+ # to individual examples or groups you care about by tagging them with
52
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
53
+ # get run.
54
+ config.filter_run :focus
55
+ config.run_all_when_everything_filtered = true
56
+
57
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
58
+ # For more details, see:
59
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
60
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
61
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
62
+ config.disable_monkey_patching!
63
+
64
+ # Many RSpec users commonly either run the entire suite or an individual
65
+ # file, and it's useful to allow more verbose output when running an
66
+ # individual spec file.
67
+ if config.files_to_run.one?
68
+ # Use the documentation formatter for detailed output,
69
+ # unless a formatter has already been configured
70
+ # (e.g. via a command-line flag).
71
+ config.default_formatter = 'doc'
72
+ end
73
+
74
+ # Print the 10 slowest examples and example groups at the
75
+ # end of the spec run, to help surface which specs are running
76
+ # particularly slow.
77
+ config.profile_examples = 10
78
+
79
+ # Run specs in random order to surface order dependencies. If you find an
80
+ # order dependency and want to debug it, you can fix the order by providing
81
+ # the seed, which is printed after each run.
82
+ # --seed 1234
83
+ config.order = :random
84
+
85
+ # Seed global randomization in this process using the `--seed` CLI option.
86
+ # Setting this allows you to use `--seed` to deterministically reproduce
87
+ # test failures related to randomization by passing the same `--seed` value
88
+ # as the one that triggered the failure.
89
+ Kernel.srand config.seed
90
+ end
@@ -0,0 +1,7 @@
1
+ module RackSupport
2
+ include Rack::Test::Methods
3
+
4
+ def app
5
+ Sinatra::Application
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra_bicyclist
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Lavrisha
8
+ - Michal Cichra
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-09-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sinatra
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rack-test
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ description: Cycle through pages at a regular interval
71
+ email:
72
+ - michael.lavrisha@gmail.com
73
+ - michal.cichra@gmail.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CHANGELOG.md
82
+ - Gemfile
83
+ - LICENSE
84
+ - README.md
85
+ - Rakefile
86
+ - examples/classic_application.rb
87
+ - examples/config.ru
88
+ - examples/modular_application.rb
89
+ - lib/sinatra/bicyclist.rb
90
+ - lib/sinatra/bicyclist/cycler.rb
91
+ - lib/sinatra/bicyclist/version.rb
92
+ - sinatra_cyclist.gemspec
93
+ - spec/byciclist_spec.rb
94
+ - spec/spec_helper.rb
95
+ - spec/support/rack.rb
96
+ homepage: https://github.com/mikz/sinatra_bicyclist
97
+ licenses: []
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.2.2
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Sinatra can ride a bicycle over and over and over again
119
+ test_files:
120
+ - spec/byciclist_spec.rb
121
+ - spec/spec_helper.rb
122
+ - spec/support/rack.rb