sinatra-rax 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE +21 -0
  4. data/README.md +159 -0
  5. data/bin/rax +97 -0
  6. data/sinatra-rax.gemspec +14 -0
  7. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe9300a8a392c80645f184a87f6e8b33bf03a4d157fc6184a2bdee9afb3aa634
4
+ data.tar.gz: da243894f05a87facdc62231677c9aab96dbeba5e114296221ef9605fd2ef66e
5
+ SHA512:
6
+ metadata.gz: b367a0a7a5bf7b03a65316864eec97086d32584a202dbde7efdb888ff8db175d6590cbb3ced21bacb20ccf900d9c53913dfca1c8fbd2ec397f18dd814e70aca8
7
+ data.tar.gz: c640bef8191fe0b93307812b7b2e72358d4cb7ed0be9af7b6a130f6a11e14484af5f4353840fea0acf2e7283b7785de32d2cb801aaf1ba20a215b962f45d488b
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Steve Shreeve
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,159 @@
1
+ # sinatra-rax
2
+
3
+ `sinatra-rax` is a minimal and modern Ruby/IRB console for Sinatra apps.
4
+
5
+ ## Goals
6
+
7
+ 1. Call `app.get '/'` to trigger a request
8
+ 2. Easily call Sinatra helpers, such as `app.current_weather`
9
+ 3. Set ivars like `app.set(name: 'bob', age: 25)`
10
+ 4. Use templates like `app.erb :test` (using `name` from above)
11
+ 5. Set ivars like `@name='mike'`, then `app.erb :test, scope: self`
12
+ 6. Normal access to database calls, like `Sponsor[2]` to show a record
13
+ 7. Call `routes` to show list sorted by route
14
+ 8. Call `props` to show application settings
15
+
16
+
17
+ ## Examples
18
+
19
+ ```ruby
20
+ project/api> rax
21
+
22
+ development(main):001:0> app # an application instance (Sinatra::Application.new!)
23
+ => #<Sinatra::Application:0x0000000108b1ae50
24
+ @app=Sinatra::Application,
25
+ @default_layout=:layout,
26
+ @pinned_response=nil,
27
+ @preferred_extension=nil,
28
+ @request=#<Sinatra::Request:0x0000000108b1a838 @env={}, @params=nil>,
29
+ @response=
30
+ #<Sinatra::Response:0x0000000108addbb8
31
+ @block=nil,
32
+ @body=[],
33
+ @buffered=true,
34
+ @headers={},
35
+ @length=0,
36
+ @status=200,
37
+ ...
38
+ @template_cache=#<Tilt::Cache:0x0000000108b1aae0 @cache={}>>
39
+
40
+ development(main):002:0> app.app # the application class
41
+ => Sinatra::Application
42
+
43
+ development(main):003:0> app.get '/ping' # mock a request
44
+
45
+ {
46
+ "pong": true
47
+ }
48
+
49
+ => #<Rack::MockResponse:0x00000001088f94c8
50
+ @block=nil,
51
+ @body=["{\n \"pong\": true\n}"],
52
+ @buffered=true,
53
+ @cookies={},
54
+ @errors="",
55
+ @headers={"Content-Type"=>"application/json", "Content-Length"=>"18"},
56
+ @length=18,
57
+ @original_headers={"Content-Type"=>"application/json", "Content-Length"=>"18"},
58
+ @status=200,
59
+ ...
60
+
61
+ development(main):004:0> app.current_weather # call a helper method
62
+ => "sunny and warm"
63
+
64
+ development(main):005:0> app.set(name: 'bob', age: 25); app.instance_variables # set instance vars
65
+ =>
66
+ [:@default_layout,
67
+ :@preferred_extension,
68
+ :@app,
69
+ :@template_cache,
70
+ :@pinned_response,
71
+ :@request,
72
+ :@response,
73
+ :@_rack_test_sessions,
74
+ :@_rack_test_current_session,
75
+ :@name,
76
+ :@age]
77
+
78
+ development(main):006:0> app.erb "Hello, <%= @name %> (age <%= @age %>)" # render a template
79
+ => "Hello, bob (age 25)"
80
+
81
+ development(main):007:0> app.set(name: 'joe', age: 50).erb("Hello, <%= @name %> (age <%= @age %>)") # one step
82
+ => "Hello, joe (age 50)"
83
+
84
+ development(main):008:0> @name = "waldo"; @age = 19; app.erb "Hello, <%= @name %> (age <%= @age %>)", scope: self # scoping
85
+ => "Hello, waldo (age 19)"
86
+
87
+ development(main):009:0> Sponsor[2] # direct access to db models
88
+ => #<Sponsor:0x00000001092bad40
89
+ id: 2,
90
+ slug: "tesla",
91
+ name: "Tesla",
92
+ link: "https://tesla.com",
93
+ email: "info@tesla.com",
94
+ logo: "tesla-logo.png",
95
+ title: "Tesla",
96
+ heading: "Accelerating sustainable transport and electric technology",
97
+ subheading: nil,
98
+ created_at: 2023-01-12 05:06:35.751006 UTC,
99
+ updated_at: 2023-01-12 05:06:35.751006 UTC>
100
+
101
+ development(main):010:0> routes # show sorted routes and methods
102
+ get "/"
103
+ post "/access"
104
+ post "/signin"
105
+ post "/signin/assignpin"
106
+ post "/signin/changepin"
107
+ post "/signin/checkpin"
108
+ delete "/users"
109
+ get "/users"
110
+ get "/users/:id"
111
+ => nil
112
+
113
+ development(main):011:0> props # show app settings
114
+ absolute_redirects true
115
+ add_charset ["application/javascript", "application/xml", "application/xhtml+xml", /^text\//]
116
+ app_file "/Users/shreeve/project/api/api.rb"
117
+ bind "localhost"
118
+ default_content_type "text/html"
119
+ default_encoding "utf-8"
120
+ dump_errors true
121
+ empty_path_info nil
122
+ environment :development
123
+ handler_name nil
124
+ lock false
125
+ logging false
126
+ method_override true
127
+ mustermann_opts {}
128
+ port 4567
129
+ prefixed_redirects false
130
+ protection {:reaction=>:deny, :except=>[:json_csrf, :frame_options, :xss_header, :http_origin]}
131
+ public nil
132
+ public_dir "/Users/shreeve/project/api/public"
133
+ public_folder "/Users/shreeve/project/api/public"
134
+ quiet false
135
+ raise_errors false
136
+ reload_templates true
137
+ reloader true
138
+ root "/Users/shreeve/project/api"
139
+ run false
140
+ running_server nil
141
+ server ["thin", "falcon", "puma", "HTTP", "webrick"]
142
+ session_secret "--secret--"
143
+ session_store Rack::Protection::EncryptedCookie
144
+ sessions false
145
+ show_exceptions false
146
+ static false
147
+ static_cache_control false
148
+ strict_paths false
149
+ threaded true
150
+ traps true
151
+ use_code false
152
+ views "/Users/shreeve/project/api/views"
153
+ x_cascade true
154
+ => nil
155
+ ```
156
+
157
+ ## License
158
+
159
+ This software is licensed under terms of the MIT License.
data/bin/rax ADDED
@@ -0,0 +1,97 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # ==============================================================================
4
+ # rax - A minimal and modern Ruby/IRB console for Sinatra apps
5
+ #
6
+ # Author: Steve Shreeve (steve.shreeve@gmail.com)
7
+ # Date: Jan 15, 2023
8
+ # ==============================================================================
9
+ # NOTE: Here are the goals...
10
+ #
11
+ # 1. Call `app.get '/'` to trigger a request
12
+ # 2. Easily call Sinatra helpers, such as `app.current_weather`
13
+ # 3. Set ivars like `app.set(name: 'bob', age: 25)`
14
+ # 4. Use templates like `app.erb :test` (using `name` from above)
15
+ # 5. Set ivars like `@name='mike'`, then `app.erb :test, scope: self`
16
+ # 6. Normal access to database calls, like `Sponsor[2]` to show a record
17
+ # 7. Call `routes` to show list sorted by route
18
+ # 8. Call `props` to show application settings
19
+ # ==============================================================================
20
+
21
+ STDOUT.sync = true
22
+
23
+ require 'bundler/setup'
24
+ require 'irb'
25
+ require 'rack/test'
26
+
27
+ IRB::ExtendCommandBundle::HushAliasWarning = true
28
+
29
+ class Rax
30
+ attr_reader :env, :app
31
+
32
+ def initialize(file=nil)
33
+ @env = ENV['APP_ENV'] ||= ENV['RACK_ENV'] || 'development'
34
+ @app = Rack::Builder.parse_file(file || ENV['RACK_CONFIG'] || 'config.ru')[0]
35
+ end
36
+
37
+ def self.instance
38
+ @instance ||= new
39
+ end
40
+
41
+ def self.app
42
+ instance.app
43
+ end
44
+ end
45
+
46
+ def env
47
+ @env ||= Rax.instance.env
48
+ end
49
+
50
+ def app
51
+ @app ||= begin
52
+ app = Rax.app.new!
53
+ app.app = Rax.app
54
+ app.extend Rack::Test::Methods
55
+ app.request = Sinatra::Request.new({})
56
+ app.response = Sinatra::Response.new
57
+ app
58
+ end
59
+ end
60
+
61
+ def app!
62
+ @app = nil
63
+ app
64
+ end
65
+
66
+ def app.set(**vars)
67
+ vars.each {|k,v| instance_variable_set "@#{k}", v }
68
+ self
69
+ end
70
+
71
+ def display(list)
72
+ wide = list.map {|e| e[0].length }.max
73
+ list.each {|a, b| puts "%-*s %s" % [wide, a, b.inspect] }
74
+ nil
75
+ end
76
+
77
+ def routes
78
+ list = Rax.app.routes.except 'HEAD'
79
+ list = list.inject([]) do |list, (meth, hits)|
80
+ list += hits.map {|vals| [meth.downcase, vals[0].to_s] }
81
+ end
82
+ list = list.sort_by {|a, b| [b, a]}
83
+ display list
84
+ end
85
+
86
+ def props
87
+ skip = %w[ inline_templates methodoverride ]
88
+ list = app.methods(false) | Rax.app.methods(false) | Sinatra::Base.methods(false)
89
+ list = list.filter_map {|meth| meth[/^(.*?)=$/, 1] }.sort - skip
90
+ list = list.map {|meth| [meth, (Rax.app.send(meth) rescue $!.inspect)] }
91
+ display list
92
+ end
93
+
94
+ # launch IRB
95
+ IRB.setup nil
96
+ IRB.conf[:IRB_NAME] = "\e[36m#{env}\e[0m"
97
+ IRB::Irb.new.run IRB.conf
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "sinatra-rax"
5
+ s.version = "1.0"
6
+ s.author = "Steve Shreeve"
7
+ s.email = "steve.shreeve@gmail.com"
8
+ s.summary = "A minimal and modern Ruby/IRB console for Sinatra apps"
9
+ s.description = "Provides a simple console to interact with Sinatra apps"
10
+ s.homepage = "https://github.com/shreeve/sinatra-rax"
11
+ s.license = "MIT"
12
+ s.files = `git ls-files`.split("\n") - %w[.gitignore]
13
+ s.executables = `cd bin && git ls-files .`.split("\n")
14
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-rax
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Steve Shreeve
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Provides a simple console to interact with Sinatra apps
14
+ email: steve.shreeve@gmail.com
15
+ executables:
16
+ - rax
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - LICENSE
22
+ - README.md
23
+ - bin/rax
24
+ - sinatra-rax.gemspec
25
+ homepage: https://github.com/shreeve/sinatra-rax
26
+ licenses:
27
+ - MIT
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.4.3
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: A minimal and modern Ruby/IRB console for Sinatra apps
48
+ test_files: []