jetson 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9f300ae4a79ccc92520c1787c974684bf8dff1d8
4
+ data.tar.gz: 6687fdda7e64f86c5014cb093c2dd84df8aace31
5
+ SHA512:
6
+ metadata.gz: 8fd8267d28aa2f55cac3c2c1634b966e6b9c6a8edc3623bee36c51ef7577da24b6b0a709e87182c48f35c65972d19929bd695fb98374723ddc122fa878bff8a4
7
+ data.tar.gz: a1c930a4fe73bab70150187fa46b8ca6a570d6ff0bdda27d18230d529ed3be62433de1509c90d02d1734b5ee1b665b50233708803dadd0cf17e98d8bbdad73eb
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jetson.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 blahed
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.
@@ -0,0 +1,98 @@
1
+ # Jetson
2
+
3
+ Talk to JSON APIs in a convient ruby based console or use the DSL to make calls in your own code.
4
+
5
+ Jetson provides a simple wrapper for working with JSON APIs – it's built for working with JSON in a friendly and convient way and tries to simplify it to the point of not having to remember complex command line switches or configuration options.
6
+
7
+ *NOTE: Jetson is built to work with JSON APIs, if you need to work with APIs that aren't JSON based, this isn't the tool for you... sorry :(*
8
+
9
+ ## Installation
10
+
11
+ ```shell
12
+ $ gem install jetson
13
+ ```
14
+
15
+ ### Usage
16
+
17
+ You can work with Jetson in two different ways, the console or the DSL. The DSL actually powers the console, so you'll have a very similar experience regardless of what you choose to use based on your needs. The console is an IRB based environment for working with your API, and the DSL provides a set of methods that can be included in your own code or scripts.
18
+
19
+ ## Console / REPL
20
+
21
+ To start the console, just run the jetson command with a hostname:
22
+
23
+ ```shell
24
+ $ jetson localhost:3000
25
+ localhost:3000>
26
+ ```
27
+
28
+ The console is actually powered by IRB, so you can do anything you'd do in IRB within the console... and command history works. HTTP verbs (get, post, put, patch, delete) are available as methods in the console:
29
+
30
+ ```ruby
31
+ # GET request
32
+ localhost:3000> get '/foo'
33
+ # GET request with params
34
+ localhost:3000> get '/foo', {some: 'param'}
35
+ # POST with json payload
36
+ localhost:3000> post '/bar', {something: 'jsony'}
37
+ ```
38
+
39
+ #### Cookies
40
+
41
+ Each console starts with a session (sorta like a browser), so any requests that set cookies will store those cookies and send them with subsequent requests. If you need to clear the cookies/session you can use the `clear :cookies` method.
42
+
43
+ #### Headers
44
+
45
+ Every request will have the `Content-Type` and `Accept` header set to `application/json`. If you need to add additional headers you can set them by manipulating the `headers` attribute:
46
+
47
+ ```ruby
48
+ localhost:3000> headers[:some_header] = 'some value'
49
+ ```
50
+
51
+ Headers set using the `headers` attribute will persist and be applied to every request made after, you can clear headers using the `clear :headers` method.
52
+
53
+
54
+ If you need to change headers on a per request basis, you can add a headers hash as the last argument to the request method:
55
+
56
+ ```ruby
57
+ localhost:3000> post '/bar', {something: 'jsony'}, {some_header: 'some value'}
58
+ ```
59
+
60
+
61
+ ## DSL
62
+
63
+ The Jetson DSL is handy for writing scripts, once you include the DSL you'll get access to all of the methods that console has. The one difference is that you'll need to call your http methods inside a `session` block to set the host name. The session block also persists headers and cookies on a global level.
64
+
65
+ ```ruby
66
+ require 'jetson/dsl'
67
+
68
+ extend Jetson::DSL
69
+
70
+ session 'http://localhost:4567' do
71
+ set :verbose, true
72
+ set :headers, {foo: 'bar'}
73
+ set :cookies, {wee: 'hah'}
74
+
75
+ get '/foo'
76
+
77
+ res = get! ('/')
78
+
79
+ puts res.status
80
+ end
81
+
82
+ ```
83
+
84
+
85
+ #### Setting headers and cookies
86
+
87
+ While working within a `session` block you can use the `set` command to set headers or cookies. Both will be applied to ever request.
88
+
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/jetson.
93
+
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
98
+
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/dsl ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'jetson/dsl'
5
+
6
+ extend Jetson::DSL
7
+
8
+ session 'http://localhost:4567' do
9
+ set :verbose, true
10
+ set :headers, {foo: 'bar'}
11
+ set :cookies, {wee: 'hah'}
12
+
13
+ get '/cookies'
14
+ get! '/'
15
+ # users = request!(:get, 'https://user:password@httpbin.org/hidden-basic-auth/user/password')
16
+ end
@@ -0,0 +1,26 @@
1
+ require 'pp'
2
+ require 'sinatra'
3
+ require 'sinatra/cookies'
4
+ require 'sinatra/json'
5
+
6
+ get '/' do
7
+ pp request.env.select {|k, _| k[0] =~ /\A[A-Z]/ }
8
+ json cookies
9
+ end
10
+
11
+ post '/api' do
12
+ pp request.env.select {|k, _| k[0] =~ /\A[A-Z]/ }
13
+ puts cookies
14
+ puts request.body.read
15
+ json headers
16
+ end
17
+
18
+ get '/cookies' do
19
+ pp request.env.select {|k, _| k[0] =~ /\A[A-Z]/ }
20
+ cookies[:foo] = 'bar'
21
+ json cookies
22
+ end
23
+
24
+ get '/boo' do
25
+ asdf
26
+ end
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'jetson'
5
+
6
+ Jetson::Console.start
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jetson/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jetson"
8
+ spec.version = Jetson::VERSION
9
+ spec.authors = ["blahed"]
10
+ spec.email = ["trvsdnn@gmail.com"]
11
+
12
+ spec.summary = "Talk to JSON APIs in a convient ruby based console or use the DSL to make calls in your own code."
13
+ spec.description = "Jetson provides a simple wrapper for working with JSON APIs – it's built for working with JSON in a friendly and convient way and tries to simplify it to the point of not having to remember complex command line switches or configuration options."
14
+ spec.homepage = "https://github.com/blahed/jetson"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "rest-client", "~> 1.8.0"
23
+ spec.add_dependency "json_color", "~> 0.1.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "minitest"
28
+ end
@@ -0,0 +1,8 @@
1
+ require 'json'
2
+
3
+ require 'jetson/dsl'
4
+ require 'jetson/console'
5
+ require 'jetson/version'
6
+
7
+ module Jetson
8
+ end
@@ -0,0 +1,51 @@
1
+ module Jetson
2
+ class Commands
3
+ extend Jetson::DSL
4
+
5
+ def self.command(name, description = nil, &block)
6
+ @commands ||= {}
7
+ @descriptions ||= {}
8
+ @descriptions[name] = description
9
+ @commands[name] = block
10
+ end
11
+
12
+ command :get, 'Perform a GET request' do |url, params|
13
+ params = eval(params) unless params.nil?
14
+
15
+ request(:get, url, params)
16
+ end
17
+
18
+ command :post, 'Perform a POST request' do |url, params|
19
+ params = eval(params) unless params.nil?
20
+
21
+ request(:post, url, params)
22
+ end
23
+
24
+ command :put, 'Perform a PUT request' do |url, params|
25
+ params = eval(params) unless params.nil?
26
+
27
+ request(:put, url, params)
28
+ end
29
+
30
+ command :patch, 'Perform a PATCH request' do |url, params|
31
+ params = eval(params) unless params.nil?
32
+
33
+ request(:patch, url, params)
34
+ end
35
+
36
+ command :delete, 'Perform a DELETE request' do |url, params|
37
+ params = eval(params) unless params.nil?
38
+
39
+ request(:delete, url, params)
40
+ end
41
+
42
+ command :help, 'Show this list' do
43
+ puts @descriptions.map { |name, desc| "#{name} - #{desc}" }.join("\n")
44
+ end
45
+
46
+ def self.run(command, url, params)
47
+ @commands[command].call(url, params)
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,83 @@
1
+ require 'irb'
2
+ require 'irb/completion'
3
+ require 'irb/ext/save-history'
4
+
5
+ module Jetson
6
+ class Console
7
+ class Context
8
+ include Jetson::DSL
9
+
10
+ def initialize(host, *args)
11
+ @host = normalize_host(host)
12
+ @verbose = true
13
+ @cookies = {}
14
+ @headers = {}
15
+ end
16
+
17
+ def help
18
+
19
+ end
20
+
21
+ def to_s
22
+ @host.sub(/\Ahttps?:\/\//, '')
23
+ end
24
+ end
25
+
26
+ def self.start
27
+ new.start
28
+ end
29
+
30
+ def start
31
+ host, *args = ARGV
32
+
33
+ ARGV.clear
34
+ IRB.setup nil
35
+
36
+ IRB.conf[:PROMPT] = {}
37
+ IRB.conf[:IRB_NAME] = 'jetson'
38
+ IRB.conf[:PROMPT][:JETSON] = {
39
+ :PROMPT_I => "\033[1m%m>\033[0m ",
40
+ :PROMPT_N => nil,
41
+ :PROMPT_S => nil,
42
+ :PROMPT_C => nil,
43
+ :RETURN => "%s\n"
44
+ }
45
+ IRB.conf[:PROMPT_MODE] = :JETSON
46
+ IRB.conf[:ECHO] = false
47
+ IRB.conf[:RC] = false
48
+ IRB.conf[:READLINE] = true
49
+ IRB.conf[:SAVE_HISTORY] = 1000
50
+ IRB.conf[:HISTORY_FILE] = '~/.jetson'
51
+
52
+ begin
53
+ dev_null = File.open('/dev/null', "w")
54
+ $stdout = dev_null
55
+ irb = IRB::Irb.new(IRB::WorkSpace.new(Context.new(host, *args)))
56
+ ensure
57
+ dev_null.close()
58
+ $stdout = STDOUT
59
+ end
60
+
61
+
62
+ IRB.conf[:MAIN_CONTEXT] = irb.context
63
+
64
+ trap(:SIGINT) do
65
+ IRB.irb.signal_handle
66
+ end
67
+
68
+ begin
69
+ catch(:IRB_EXIT) do
70
+ irb.eval_input
71
+ end
72
+ ensure
73
+ IRB.irb_at_exit
74
+ end
75
+ end
76
+
77
+ private
78
+
79
+ def normalize_host
80
+
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,105 @@
1
+ require 'json'
2
+ require 'uri'
3
+
4
+ require 'json_color'
5
+ require 'rest-client'
6
+
7
+ module Jetson
8
+ module DSL
9
+
10
+ def set(option, value)
11
+ instance_variable_set("@#{option}", value)
12
+ end
13
+
14
+ def session(host, &block)
15
+ @host = normalize_host(host)
16
+ @verbose = false
17
+ @cookies = {}
18
+ @headers = {}
19
+
20
+ self.instance_eval &block
21
+ end
22
+
23
+ def clear(resource = :all)
24
+ case resource
25
+ when :all
26
+ @cookies = {}
27
+ @headers = {}
28
+ when :headers
29
+ @headers = {}
30
+ when :cookies
31
+ @cookies = {}
32
+ end
33
+ end
34
+
35
+ def request(method, url, *args)
36
+ response = request!(method, url, *args)
37
+ json = parse_json(response.body) if response.headers[:content_type] =~ /json/
38
+
39
+ print_request(response, json) if @verbose
40
+
41
+ json
42
+ rescue RestClient::InternalServerError => err
43
+ puts "\033[1m#{method.to_s.upcase}\033[0m #{url} \033[31m#{err.response.code}\033[0m"
44
+ end
45
+
46
+ def request!(method, url, params = {}, headers = {})
47
+ options = default_options
48
+ url = URI.join(@host, url).to_s
49
+
50
+ options.merge!(headers)
51
+
52
+ if method == :get
53
+ options.merge!({params: params})
54
+ res = RestClient.send(method, url, options)
55
+ else
56
+ res = RestClient.send(method, url, params.to_json, options)
57
+ end
58
+
59
+ @cookies.merge!(res.cookies)
60
+
61
+ res
62
+ end
63
+
64
+ [:get, :post, :put, :patch, :delete].each do |name|
65
+ define_method(name) { |url, *args| request(name, url, *args) }
66
+ define_method("#{name}!".to_sym) { |url, *args| request!(name, url,*args) }
67
+ alias_method name.upcase, name
68
+ alias_method "#{name.upcase}!".to_sym, "#{name}!".to_sym
69
+ end
70
+
71
+ private
72
+
73
+ def normalize_host(host)
74
+ host =~ /\Ahttp/ ? host : "http://#{host}"
75
+ end
76
+
77
+ def parse_json(body)
78
+ JSON.parse(body)
79
+ rescue JSON::ParserError => err
80
+ puts "\033[31mUnable to parse JSON\033[0m"
81
+ puts err.backtrace
82
+ nil
83
+ end
84
+
85
+ def default_options
86
+ options = { content_type: :json, accept: :json }.merge(@headers)
87
+ options[:cookies] = @cookies unless @cookies.nil?
88
+
89
+ options
90
+ end
91
+
92
+ def print_request(response, json)
93
+ request = response.request
94
+ url = request.url.sub(@host, '')
95
+
96
+ puts "\033[1m#{request.method.to_s.upcase}\033[0m #{url} \033[32m#{response.code}\033[0m"
97
+ puts "\033[4mResponse\033[0m"
98
+ if json
99
+ puts JsonColor.colorize(JSON.pretty_generate(body))
100
+ else
101
+ puts response.body[0..100] + '...'
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module Jetson
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jetson
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - blahed
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: json_color
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
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
+ description: Jetson provides a simple wrapper for working with JSON APIs – it's built
84
+ for working with JSON in a friendly and convient way and tries to simplify it to
85
+ the point of not having to remember complex command line switches or configuration
86
+ options.
87
+ email:
88
+ - trvsdnn@gmail.com
89
+ executables:
90
+ - jetson
91
+ extensions: []
92
+ extra_rdoc_files: []
93
+ files:
94
+ - ".gitignore"
95
+ - ".travis.yml"
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/dsl
101
+ - bin/server
102
+ - exe/jetson
103
+ - jetson.gemspec
104
+ - lib/jetson.rb
105
+ - lib/jetson/commands.rb
106
+ - lib/jetson/console.rb
107
+ - lib/jetson/dsl.rb
108
+ - lib/jetson/version.rb
109
+ homepage: https://github.com/blahed/jetson
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.6.7
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Talk to JSON APIs in a convient ruby based console or use the DSL to make
133
+ calls in your own code.
134
+ test_files: []