richard-cli 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,34 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ # .ruby-version
31
+ # .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in richard-cli.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Adam Kerr
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,30 @@
1
+ # Richard::Cli
2
+
3
+ Command line interface for accessing richard.
4
+
5
+ ## Installation
6
+
7
+ Install it yourself as:
8
+
9
+ $ gem install richard-cli
10
+
11
+ ## Usage
12
+
13
+ Usage:
14
+ richard command [user_id]
15
+
16
+ Commands:
17
+ list - List current queue
18
+ enqueue - Add yourself to the queue
19
+ cancel - Cancel yourself from the queue if you are in nit
20
+ run - Signal to the queue if you are running (only works if you are at the head of the queue)
21
+ finish - Signal to the queue if you are finished (only works if you are at the head of the queue)
22
+ force_release - Force releases another user. The user_id must be provided.
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( https://github.com/[my-github-username]/richard-cli/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/richard ADDED
@@ -0,0 +1,54 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'richard'
4
+
5
+
6
+ def print_usage
7
+ puts <<-EOS
8
+ Usage:
9
+ richard command [user_id]
10
+
11
+ Commands:
12
+ list - List current queue
13
+ enqueue - Add yourself to the queue
14
+ cancel - Cancel yourself from the queue if you are in nit
15
+ run - Signal to the queue if you are running (only works if you are at the head of the queue)
16
+ finish - Signal to the queue if you are finished (only works if you are at the head of the queue)
17
+ force_release - Force releases another user. The user_id must be provided.
18
+ EOS
19
+ end
20
+
21
+
22
+
23
+ config = Richard::ConfigRepository.find_or_create
24
+ server = Richard::Server.new(config.url, config.api_key)
25
+
26
+ case ARGV[0]
27
+ when 'list'
28
+ server.list
29
+
30
+ when 'enqueue'
31
+ server.enqueue
32
+
33
+ when 'cancel'
34
+ server.cancel
35
+
36
+ when 'run'
37
+ server.run
38
+
39
+ when 'finish'
40
+ server.finish
41
+
42
+ when 'force_release'
43
+ user_id = ARGV[1].to_i
44
+
45
+ if user_id
46
+ server.force_release(user_id)
47
+ else
48
+ print_usage
49
+ end
50
+
51
+ else
52
+ print_usage
53
+
54
+ end
@@ -0,0 +1,9 @@
1
+ require "richard/cli/version"
2
+
3
+ module Richard
4
+ module Cli
5
+ def self.hello_world
6
+ puts "Hello"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Richard
2
+ class Config
3
+ attr_reader :api_key, :url
4
+
5
+ # DEFAULT_URL = "http://richard.nulogy.nu/"
6
+ DEFAULT_URL = 'http://richard.vcap.me:3030'
7
+ CONFIG_FILE = ENV['HOME'] + "/.richard"
8
+
9
+ def initialize(url, api_key)
10
+ @url = url
11
+ @api_key = api_key
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,42 @@
1
+ require 'yaml'
2
+ require 'highline/import'
3
+
4
+ module Richard
5
+ module ConfigRepository
6
+ extend self
7
+
8
+ def find_or_create
9
+ load || prompt
10
+ end
11
+
12
+ def prompt
13
+ puts "Could not find a valid config file. Creating one at #{Config::CONFIG_FILE}"
14
+
15
+ url = ask "URL for Richard: " do |q|
16
+ q.default = Richard::Config::DEFAULT_URL
17
+ end
18
+
19
+ api_key = ask "API Key: " do |q|
20
+ q.validate = /\w+/
21
+ end
22
+
23
+ config = Config.new(url, api_key)
24
+ save(config)
25
+
26
+ return config
27
+ end
28
+
29
+
30
+ def load
31
+ return YAML.load_file(Config::CONFIG_FILE)
32
+ rescue Errno::ENOENT => e
33
+ return false
34
+ end
35
+
36
+ def save(config)
37
+ File.open(Config::CONFIG_FILE, 'w') do |file|
38
+ file.write(YAML.dump(config))
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,41 @@
1
+ require 'json'
2
+
3
+ module Richard
4
+ class QueuedUser
5
+ attr_reader :user_id, :email, :name
6
+ attr_reader :status
7
+
8
+ def initialize(options)
9
+ @user_id = options[:user_id]
10
+ @name = options[:name]
11
+ @email = options[:email]
12
+ @status = options[:status]
13
+ end
14
+
15
+ def to_s
16
+ [
17
+ user_id,
18
+ name,
19
+ email,
20
+ status
21
+ ].join(", ")
22
+ end
23
+ end
24
+
25
+ module QueuedUserDeserializer
26
+ extend self
27
+
28
+ def from_json(parsed_json)
29
+ parsed_json['content'].map do |obj|
30
+ obj = obj['queue_transaction']
31
+
32
+ Richard::QueuedUser.new(
33
+ user_id: obj['user']['id'],
34
+ name: obj['user']['name'],
35
+ email: obj['user']['email'],
36
+ status: obj['status']
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,35 @@
1
+ require 'terminal-table'
2
+
3
+ module Richard
4
+ module QueuedUserFormatter
5
+ extend self
6
+
7
+ def table_format(users)
8
+ # return Terminal::Table.new(rows: rows)
9
+ Terminal::Table.new do |table|
10
+ table << row_header
11
+ table.add_separator
12
+
13
+ users.each { |user| table << user_to_row(user) }
14
+ end
15
+ end
16
+
17
+ def row_header
18
+ [
19
+ "User Id",
20
+ "Name",
21
+ "Email",
22
+ "Status"
23
+ ]
24
+ end
25
+
26
+ def user_to_row(user)
27
+ return [
28
+ user.user_id,
29
+ user.name,
30
+ user.email,
31
+ user.status
32
+ ]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Richard
2
+ VERSION = "1.0.0"
3
+ end
data/lib/richard.rb ADDED
@@ -0,0 +1,75 @@
1
+ require "richard/version"
2
+ require "richard/config"
3
+ require "richard/config_repository"
4
+ require "richard/queued_user"
5
+ require "richard/queued_user_formatter"
6
+
7
+ require "rest-client"
8
+
9
+ module Richard
10
+ class Server
11
+ def initialize(base_url, api_key)
12
+ @base_url = base_url
13
+ @api_key = api_key
14
+ end
15
+
16
+ def enqueue
17
+ response = send_command("/queue/enqueue.json")
18
+ print_result(response)
19
+ end
20
+
21
+ def cancel
22
+ response = send_command("/queue/cancel.json")
23
+ print_result(response)
24
+ end
25
+
26
+ def run
27
+ response = send_command("/queue/run.json")
28
+ print_result(response)
29
+ end
30
+
31
+ def finish
32
+ response = send_command("/queue/finish.json")
33
+ print_result(response)
34
+ end
35
+
36
+ def force_release(user_id)
37
+ response = send_command("/queue/force_release/#{user_id}.json")
38
+ print_result(response)
39
+ end
40
+
41
+ def list
42
+ print_result(RestClient.get(@base_url + "/?format=json&api_key=#{@api_key}"))
43
+ end
44
+
45
+ private
46
+
47
+ def send_command(url)
48
+ RestClient.post(@base_url + url, api_key: @api_key)
49
+ rescue RestClient::UnprocessableEntity => e
50
+ e.response
51
+ end
52
+
53
+ def print_result(result)
54
+ json = JSON.parse(result)
55
+
56
+ users = QueuedUserDeserializer.from_json(json)
57
+
58
+ puts QueuedUserFormatter.table_format(users)
59
+ puts format_estimated_wait(json)
60
+ puts format_flash(json['flash']) unless json['flash'].empty?
61
+ end
62
+
63
+ def format_estimated_wait(response)
64
+ "Estimated Wait: #{response['estimated_wait_time']}"
65
+ end
66
+
67
+ def format_flash(flash)
68
+ if flash['error']
69
+ "\nError: " + flash['error']
70
+ end
71
+ end
72
+ end
73
+
74
+
75
+ end
@@ -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 'richard/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "richard-cli"
8
+ spec.version = Richard::VERSION
9
+ spec.authors = ["Adam Kerr"]
10
+ spec.email = ["adamk@nulogy.com"]
11
+ spec.summary = %q{A command line interface for richard}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.add_dependency "json"
25
+ spec.add_dependency "rest-client"
26
+ spec.add_dependency "highline"
27
+ spec.add_dependency "terminal-table"
28
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: richard-cli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Kerr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2015-01-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.6'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.6'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '10.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '10.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rest-client
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: highline
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :runtime
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: terminal-table
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :runtime
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: ''
111
+ email:
112
+ - adamk@nulogy.com
113
+ executables:
114
+ - richard
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - bin/richard
124
+ - lib/richard.rb
125
+ - lib/richard/cli.rb
126
+ - lib/richard/config.rb
127
+ - lib/richard/config_repository.rb
128
+ - lib/richard/queued_user.rb
129
+ - lib/richard/queued_user_formatter.rb
130
+ - lib/richard/version.rb
131
+ - richard-cli.gemspec
132
+ homepage: ''
133
+ licenses:
134
+ - MIT
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ none: false
141
+ requirements:
142
+ - - ! '>='
143
+ - !ruby/object:Gem::Version
144
+ version: '0'
145
+ segments:
146
+ - 0
147
+ hash: 3065960232028393740
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ segments:
155
+ - 0
156
+ hash: 3065960232028393740
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 1.8.23.2
160
+ signing_key:
161
+ specification_version: 3
162
+ summary: A command line interface for richard
163
+ test_files: []