pvdgm-bs-client 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: 451d6b508d2f48e034c94f0a47978bf9add343eb
4
+ data.tar.gz: 3af5ab2ece0adcc1918da092efd172af141cc4db
5
+ SHA512:
6
+ metadata.gz: 13fbadb4b8b10073114118ce5cbfe9a192159953f61fa1bb5570b7ca4ad9ae0c1955ad13c3a99f91c3afd287b5000cdb4b711701fc6edf6f7322c4d5061fb38c
7
+ data.tar.gz: c88e545d560287f2db3f9870f3ca01534d2d0d564b4d03304e63707371d23ac6c19b93021243d4341b5d7471c4ad87744e8fae4780ad212b0450a4b2cc9ca05d
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ gem "activesupport", ">= 3.2.14"
5
+ gem 'rest-client'
6
+ gem 'terminal-table'
7
+ gem 'highline'
8
+
9
+ # Add dependencies to develop your gem here.
10
+ # Include everything needed to run rake, tests, features, etc.
11
+ group :development do
12
+ gem "rspec"
13
+ gem "rdoc", "~> 3.12"
14
+ gem "bundler", "~> 1.0"
15
+ gem "jeweler", "~> 1.8.7"
16
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.2.14)
5
+ i18n (~> 0.6, >= 0.6.4)
6
+ multi_json (~> 1.0)
7
+ addressable (2.3.5)
8
+ builder (3.2.2)
9
+ diff-lcs (1.2.4)
10
+ faraday (0.8.8)
11
+ multipart-post (~> 1.2.0)
12
+ git (1.2.6)
13
+ github_api (0.10.1)
14
+ addressable
15
+ faraday (~> 0.8.1)
16
+ hashie (>= 1.2)
17
+ multi_json (~> 1.4)
18
+ nokogiri (~> 1.5.2)
19
+ oauth2
20
+ hashie (2.0.5)
21
+ highline (1.6.19)
22
+ httpauth (0.2.0)
23
+ i18n (0.6.5)
24
+ jeweler (1.8.7)
25
+ builder
26
+ bundler (~> 1.0)
27
+ git (>= 1.2.5)
28
+ github_api (= 0.10.1)
29
+ highline (>= 1.6.15)
30
+ nokogiri (= 1.5.10)
31
+ rake
32
+ rdoc
33
+ json (1.8.0)
34
+ jwt (0.1.8)
35
+ multi_json (>= 1.5)
36
+ mime-types (1.25)
37
+ multi_json (1.8.0)
38
+ multi_xml (0.5.5)
39
+ multipart-post (1.2.0)
40
+ nokogiri (1.5.10)
41
+ oauth2 (0.9.2)
42
+ faraday (~> 0.8)
43
+ httpauth (~> 0.2)
44
+ jwt (~> 0.1.4)
45
+ multi_json (~> 1.0)
46
+ multi_xml (~> 0.5)
47
+ rack (~> 1.2)
48
+ rack (1.5.2)
49
+ rake (10.1.0)
50
+ rdoc (3.12.2)
51
+ json (~> 1.4)
52
+ rest-client (1.6.7)
53
+ mime-types (>= 1.16)
54
+ rspec (2.14.1)
55
+ rspec-core (~> 2.14.0)
56
+ rspec-expectations (~> 2.14.0)
57
+ rspec-mocks (~> 2.14.0)
58
+ rspec-core (2.14.5)
59
+ rspec-expectations (2.14.3)
60
+ diff-lcs (>= 1.1.3, < 2.0)
61
+ rspec-mocks (2.14.3)
62
+ terminal-table (1.4.5)
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ activesupport (>= 3.2.14)
69
+ bundler (~> 1.0)
70
+ highline
71
+ jeweler (~> 1.8.7)
72
+ rdoc (~> 3.12)
73
+ rest-client
74
+ rspec
75
+ terminal-table
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Dave
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = pvdgm-bs-client
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to pvdgm-bs-client
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2013 Dave. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,50 @@
1
+ # -*- ruby -*-
2
+ # encoding: utf-8
3
+
4
+ require 'rubygems'
5
+ require 'bundler'
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+ require 'rake'
14
+
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
18
+ gem.name = "pvdgm-bs-client"
19
+ gem.homepage = "http://github.com/j0hnds/pvdgm-bs-client"
20
+ gem.license = "MIT"
21
+ gem.summary = %Q{Command-line client tools to use the beanstalk api.}
22
+ gem.description = %Q{This gem provides command-line tools to use the beanstalk api.}
23
+ gem.email = "dave.sieh@providigm.com"
24
+ gem.authors = ["Dave Sieh"]
25
+ # dependencies defined in Gemfile
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rdoc/task'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "pvdgm-bs-client #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/bin/beanstalk_api ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'pvdgm-bs-client'
5
+
6
+ bso = BeanstalkApiOptions.new
7
+
8
+ bso.invoke_command
@@ -0,0 +1,15 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext/object'
3
+ require 'base64'
4
+ require 'pvdgm-bs-client/beanstalk_api_options'
5
+ require 'pvdgm-bs-client/base_resource'
6
+
7
+ # Load all the prompters
8
+ Dir.glob(File.join(File.dirname(__FILE__), 'pvdgm-bs-client/prompters/*.rb')).each do | prompter_path |
9
+ require File.join('pvdgm-bs-client/prompters', File.basename(prompter_path))
10
+ end
11
+
12
+ # Load all the resources
13
+ Dir.glob(File.join(File.dirname(__FILE__), 'pvdgm-bs-client/resources/*.rb')).each do | resource_path |
14
+ require File.join('pvdgm-bs-client/resources', File.basename(resource_path))
15
+ end
@@ -0,0 +1,103 @@
1
+ require 'time'
2
+ require 'rest-client'
3
+ require 'json'
4
+ require 'highline'
5
+ require 'terminal-table'
6
+
7
+ class BaseResource
8
+
9
+ attr_reader :options, :prompter
10
+
11
+ def initialize(opts)
12
+ @options = opts
13
+ @prompter = HighLine.new
14
+ end
15
+
16
+ private
17
+
18
+ def invoke_rest(is_json=true)
19
+ begin
20
+ response = yield
21
+ begin
22
+ is_json ? JSON.parse(response) : response
23
+ rescue Exception => ex
24
+ puts "Error parsing response: #{ex.message[0..100]}"
25
+ if options[:trace]
26
+ puts "Full Error message: #{ex.message}"
27
+ puts "\t#{ex.backtrace.join("\n\t")}"
28
+ end
29
+ exit 1
30
+ end
31
+ rescue RestClient::Exception => ex
32
+ puts "Response: #{ex.response.code} - #{ex.response.description}"
33
+ if [ 403, 404 ].include?(ex.response.code)
34
+ if is_json
35
+ begin
36
+ hash = JSON.parse(ex.response.http_body)
37
+ if hash.has_key?('error')
38
+ puts(hash['error'])
39
+ elsif hash.has_key?('validation_error')
40
+ puts "Validation errors:"
41
+ hash['validation_error'].each_pair do | key, value |
42
+ puts " #{key}"
43
+ puts " #{value.join("\n ")}"
44
+ end
45
+ else
46
+ puts(hash.inspect)
47
+ end
48
+ rescue Exception => json_ex
49
+ puts "Unable to parse body for error: #{ex.response.http_body[0..50]}"
50
+ end
51
+ end
52
+ end
53
+ exit 1
54
+ rescue SystemExit
55
+ raise
56
+ rescue Exception => ex
57
+ # General unknown exception
58
+ puts "Error invoking RestClient: #{ex.message}"
59
+ puts "\t#{ex.backtrace.join("\n\t")}" if options[:trace]
60
+ exit 1
61
+ end
62
+ end
63
+
64
+ def get(uri, is_json=true)
65
+ invoke_rest(is_json) do
66
+ result = RestClient.get(build_url(uri),
67
+ authentication_headers)
68
+ end
69
+ end
70
+
71
+ def delete(uri, is_json=true)
72
+ invoke_rest(is_json) do
73
+ result = RestClient.delete(build_url(uri),
74
+ authentication_headers)
75
+ end
76
+ end
77
+
78
+ def post(uri, parameters, is_json=true)
79
+ invoke_rest(is_json) do
80
+ result = RestClient.post(build_url(uri),
81
+ parameters,
82
+ authentication_headers)
83
+ end
84
+ end
85
+
86
+ def put(uri, parameters, is_json=true)
87
+ invoke_rest(is_json) do
88
+ result = RestClient.put(build_url(uri),
89
+ parameters,
90
+ authentication_headers)
91
+ end
92
+ end
93
+
94
+ def build_url(uri)
95
+ "#{options[:use_ssl] ? 'https' : 'http'}://#{options[:server]}/#{uri}"
96
+ end
97
+
98
+ def authentication_headers
99
+ {
100
+ 'Authorization' => "Token token=\"#{options[:api_token]}\""
101
+ }
102
+ end
103
+ end
@@ -0,0 +1,136 @@
1
+ class BeanstalkApiOptions
2
+
3
+ DEFAULT_SERVER = 'www.abaqis.com'
4
+ LOCAL_SERVER = 'localhost:3000'
5
+ UAT_SERVER = 'uat.abaqis.com'
6
+ DEFAULT_USE_SSL = true
7
+ DEFAULT_RESOURCE = 'statistics'
8
+ DEFAULT_COMMAND = 'list'
9
+ DEFAULT_TOKEN = ENV['API_TOKEN']
10
+
11
+ def initialize
12
+ @options = { all_stats: false}
13
+
14
+ @optparse = OptionParser.new do | opts |
15
+ # Set a banner, displayed at the top of the help screen
16
+ opts.banner = "Usage: beanstalk_api [ options ] [ resource [ command ] ]"
17
+
18
+ opts.on '-h', '--help', 'Display the help message' do
19
+ puts opts
20
+ puts
21
+ print_valid_resources
22
+ puts
23
+ exit
24
+ end
25
+
26
+ @options[:server] = DEFAULT_SERVER
27
+ opts.on '-s', '--server SERVER', "Specify the abaqis server to hit (default #{DEFAULT_SERVER})" do | server |
28
+ @options[:server] = server
29
+ end
30
+
31
+ @options[:use_ssl] = DEFAULT_USE_SSL
32
+ opts.on '-n', '--no-ssl', "If specified, SSL will NOT be used for the call to the server" do
33
+ @options[:use_ssl] = false
34
+ end
35
+
36
+ opts.on '--local', 'Set the host to the localhost and non-ssl as a testing convenience' do
37
+ @options[:server] = LOCAL_SERVER
38
+ @options[:use_ssl] = false
39
+ end
40
+
41
+ opts.on '--uat', 'Set the host to the uat machine and ssl as a testing convenience' do
42
+ @options[:server] = UAT_SERVER
43
+ @options[:use_ssl] = true
44
+ end
45
+
46
+ opts.on '-l', '--list', 'List the valid commands' do
47
+ print_valid_resources
48
+ exit
49
+ end
50
+
51
+ @options[:api_token] = DEFAULT_TOKEN
52
+ opts.on '-t', '--token TOKEN', 'The API security token to use (defaults to ENV["API_TOKEN"])' do | token |
53
+ @options[:api_token] = token
54
+ end
55
+
56
+ opts.on '--tube TUBE_NAME', 'The name of the tube to use for the query' do | tube_name |
57
+ @options[:tube] = tube_name
58
+ end
59
+
60
+ opts.on '--all', 'Indicates that all statistics values should be show' do
61
+ @options[:all_stats] = true
62
+ end
63
+
64
+ end
65
+ end
66
+
67
+ def parse_options!(argv=ARGV)
68
+ @optparse.parse!(argv)
69
+
70
+ resource = argv.shift || DEFAULT_RESOURCE
71
+ command = argv.shift || DEFAULT_COMMAND
72
+
73
+ # Check to make sure we have a valid resource
74
+ unless valid_resource?(resource)
75
+ puts @optparse
76
+ puts
77
+ puts "Invalid resource: #{resource}"
78
+ puts
79
+ print_valid_resources
80
+ exit
81
+ end
82
+
83
+ # Must have been a valid resource; do we have a valid command
84
+ unless valid_command?(resource, command)
85
+ puts @optparse
86
+ puts
87
+ puts "Invalid command (#{command}) for resource (#{resource})"
88
+ puts
89
+ print_valid_resources
90
+ exit
91
+ end
92
+
93
+ # Check the API token
94
+ if @options[:api_token].nil?
95
+ puts @optparse
96
+ puts
97
+ puts "Must specify an API token either via the environment variable 'API_TOKEN' or via the -t option"
98
+ puts
99
+ print_valid_resources
100
+ exit
101
+ end
102
+
103
+ [ @options, resource, command ]
104
+ end
105
+
106
+ def invoke_command
107
+ options, resource, command = parse_options!
108
+
109
+ eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").new(options).send(command.to_sym)
110
+ end
111
+
112
+ private
113
+
114
+ def valid_command?(resource, command)
115
+ return false if resource.blank? || command.blank?
116
+ eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").instance_methods(false).include?(command.to_sym)
117
+ end
118
+
119
+ def valid_resource?(resource)
120
+ Resources.constants.map { | const | ActiveSupport::Inflector.underscore(const.to_s) }.include?(resource)
121
+ end
122
+
123
+ def print_valid_resources
124
+ puts "Valid Resources:"
125
+ Resources.constants.each do | const |
126
+ resource_string = ActiveSupport::Inflector.underscore(const.to_s)
127
+ puts " - #{resource_string}"
128
+ puts " Commands: #{print_valid_commands(resource_string)} Default: list"
129
+ end
130
+ end
131
+
132
+ def print_valid_commands(resource)
133
+ eval("Resources::#{ActiveSupport::Inflector.camelize(resource)}").instance_methods(false).map { | command | command.to_s }.join(", ")
134
+ end
135
+
136
+ end