bwcli 1.0.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: 74d73712b88433fc3573ed6361018686b95e8e18
4
+ data.tar.gz: 3705e7faae6ff8da83a0e92e85cc3dadfcc7293b
5
+ SHA512:
6
+ metadata.gz: ba7a0d48b232ff53b3631fb9228b6a5ce5baf975459888ca21e44e1a1290f2b088a54fca2d8069a004eb84abfece61b783b1c3a4bf0104c6bc315f9bb91cc5ae
7
+ data.tar.gz: caefe5b3737721cb1819932fe56c213dd852c3290d77a026f20353dca64da1e135e077d1564e35beb0313b9c204ec09c70ebc58f847f7ed0a84e00e255b48699
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+ .DS_Store
15
+
16
+ Gemfile.lock
17
+ report.html
18
+
19
+ # YARD artifacts
20
+ .yardoc
21
+ _yardoc
22
+ doc/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'bundler', '~> 1.3.5'
4
+ gem "coveralls", "~> 0.6.7", require: false
5
+
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Jonathan Chrisp
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # bwcli
2
+
3
+ __PLEASE NOTE THAT THIS PROJECT IS NOT OFFICIALLY SUPPORTED BY BRANDWATCH__
4
+
5
+ A command line interface to interact with the Brandwatch API
6
+
7
+
data/bin/bwcli ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bwcli'
3
+ BWCLI::BW.start ARGV
data/bwcli.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'bwcli'
3
+ s.version = '1.0.0'
4
+ s.date = '2013-08-15'
5
+ s.summary = 'Brandwatch CLI'
6
+ s.description = 'A CLI interface to interact with the Brandwatch v2 API'
7
+ s.author = 'Jonathan Chrisp'
8
+ s.email = 'jonathan@brandwatch.com'
9
+ s.license = 'MIT'
10
+ s.homepage = 'https://github.com/jonathanchrisp/bwcli'
11
+ s.required_ruby_version = ">= 1.9.2"
12
+
13
+ s.add_development_dependency 'rspec', '~> 2.13.0'
14
+ s.add_development_dependency 'pry', '~> 0.9.12.2'
15
+
16
+ s.add_runtime_dependency 'bwapi', '~> 1.0.0'
17
+ s.add_runtime_dependency 'thor', '~> 0.18.1'
18
+ s.add_runtime_dependency 'awesome_print', '~> 1.1.0'
19
+ s.add_runtime_dependency 'hashie', '~> 2.0.5'
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.test_files = `git ls-files -- spec/*`.split("\n")
24
+ s.require_paths = ['lib']
25
+ end
Binary file
@@ -0,0 +1,66 @@
1
+ require 'bwcli/configuration'
2
+
3
+ module BWCLI
4
+
5
+ # Config subcommand
6
+ class Config < Thor
7
+
8
+ no_tasks do
9
+ def configuration
10
+ @configuration ||= BWCLI::Configuration.new
11
+ end
12
+ alias :conf :configuration
13
+ end
14
+
15
+ desc "user", "Returns the current user"
16
+ def user
17
+ conf.list_current_user
18
+ end
19
+
20
+ desc "login", "Authenticate current user"
21
+ def login
22
+ conf.oauth
23
+ end
24
+
25
+ desc "list", "List users"
26
+ def list
27
+ conf.list_users
28
+ end
29
+
30
+ desc "add", "Add a new user"
31
+ method_option :e, :banner => 'Environment', :required => true
32
+ method_option :u, :banner => 'Username', :required => true
33
+ method_option :p, :banner => 'Password', :required => true
34
+ def add
35
+ conf.add_user options[:e], options[:u], options[:p]
36
+
37
+ # Set as current user if no current user exists
38
+ return conf.set_current_user options[:e], options[:u] unless conf.current_user_exists?
39
+
40
+ # If current user exists check to switch
41
+ conf.set_current_user options[:e], options[:u] if yes? "Would you like set this user as the current user?"
42
+ end
43
+
44
+ desc "switch", "Switch to user"
45
+ method_option :u, :banner => 'Username', :required => true
46
+ method_option :e, :banner => 'Environment', :required => true
47
+ def switch
48
+ conf.set_current_user options[:e], options[:u]
49
+ end
50
+
51
+ desc "remove", "Remove a new user"
52
+ method_option :u, :banner => 'Username', :required => true
53
+ method_option :e, :banner => 'Environment', :required => true
54
+ def remove
55
+ conf.remove_user options[:u], options[:e]
56
+ end
57
+
58
+ desc "reset", "Reset the conf file"
59
+ def reset
60
+ conf.reset if yes? "Are you sure you want to reset your config?"
61
+ end
62
+
63
+ include Thor::Actions
64
+
65
+ end
66
+ end
@@ -0,0 +1,186 @@
1
+ require 'yaml'
2
+
3
+ module BWCLI
4
+
5
+ # Configuration class to maintain config
6
+ class Configuration
7
+
8
+ # Initialises the configuration class
9
+ def initialize
10
+ @config_file = File.join(File.expand_path("~"), ".bwcli")
11
+ @config = Hashie::Mash.new(YAML.load_file @config_file)
12
+ rescue Errno::ENOENT
13
+ abort "You dont have a .bwcli file!".red.underline
14
+ end
15
+
16
+ # Add user to the config hash
17
+ #
18
+ # @param [String] env the environment
19
+ # @param [String] username the username of the user
20
+ # @param [String] pwd the password of the user
21
+ def add_user env, username, pwd
22
+ config[env] = {} unless env_exists? env
23
+ abort "The #{username} already exists!".yellow if user_exists? env, username
24
+ config[env][username] = { 'access_token' => '', 'password' => pwd }
25
+ write_config
26
+ end
27
+
28
+ # Returns the api endpoint for the current user
29
+ #
30
+ # @return [String] the api endpoint
31
+ def api_endpoint
32
+ case current_user.environment
33
+ when 'int', 'integration'
34
+ 'http://newapi.int.brandwatch.com'
35
+ when 'rel', 'release', 'stage'
36
+ 'http://newapi.rel.brandwatch.com'
37
+ else
38
+ 'http://newapi.brandwatch.com'
39
+ end
40
+ end
41
+
42
+ # Creates and returns bwapi instance
43
+ #
44
+ # @return [Object] bwapi the bwapi instance
45
+ def bwapi
46
+ abort "You have no current user set!".yellow unless current_user_exists?
47
+ abort "There is no access token set for the current user".yellow if config.current_user.access_token.nil?
48
+ abort "There is no environment set for the current user".yellow if config.current_user.environment.nil?
49
+
50
+ return @bwapi ||= BWAPI::Client.new(:username => current_user.username, :password => current_user.password, :api_endpoint => api_endpoint)
51
+ end
52
+
53
+ # Returns the config
54
+ #
55
+ # @return [Hash] config the current config
56
+ def config
57
+ @config
58
+ end
59
+
60
+ # Returns the current user set in config hash
61
+ #
62
+ # @return [Hash] returns the current user
63
+ def current_user
64
+ abort "You have no current user set!".yellow unless current_user_exists?
65
+ config.current_user
66
+ end
67
+
68
+ # Check whether the current user exists in the config hash
69
+ #
70
+ # @return [Boolean]
71
+ def current_user_exists?
72
+ return false if config.current_user.nil?
73
+ return true
74
+ end
75
+
76
+ # Check whether a env exists in the config hash
77
+ #
78
+ # @param [String] env the environment
79
+ # @return [Boolean]
80
+ def env_exists? env
81
+ return false if config.nil?
82
+ config.has_key? env
83
+ end
84
+
85
+ # List all users within the config hash
86
+ def list_users
87
+ abort "You have no users within your config file!".yellow if config.empty?
88
+ puts "\nUser Configuration".yellow
89
+ config.each do |k, v|
90
+ next if k == 'current_user'
91
+ puts "\nEnvironment: #{k}".yellow
92
+ print_hash_values v
93
+ end
94
+
95
+ list_current_user if current_user_exists?
96
+ end
97
+
98
+ # List the current user within the config hash
99
+ def list_current_user
100
+ abort "You have no current user set!".yellow unless current_user_exists?
101
+ puts "\nCurrent User:".yellow
102
+ config.current_user.each do |k,v|
103
+ puts "#{k.yellow}: #{v}\n" unless v.nil?
104
+ end
105
+ end
106
+
107
+ # Authenticates current user
108
+ def oauth
109
+ if current_user.access_token.nil? || current_user.access_token.empty?
110
+ abort "Unable to login as #{current_user.username}".red unless bwapi.login
111
+ set_access_token bwapi.access_token
112
+ else
113
+ bwapi.access_token = current_user.access_token
114
+ abort "Unable to login as #{current_user.username}".red unless bwapi.login
115
+ end
116
+
117
+ return bwapi
118
+ end
119
+
120
+ # Print out hash values
121
+ def print_hash_values hash
122
+ puts hash.yellow unless hash.is_a? Hash
123
+ hash.each do |k, v|
124
+ if v.is_a? Hash
125
+ puts "User: #{k}:".yellow
126
+ print_hash_values v
127
+ else
128
+ puts " - #{k}: #{v}"
129
+ end
130
+ end
131
+ end
132
+
133
+ # Resets the config to an empty hash
134
+ def reset
135
+ @config = {}
136
+ write_config
137
+ end
138
+
139
+ # Set current user
140
+ #
141
+ # @param [String] env the environment
142
+ # @param [String] username the username of the user
143
+ def set_current_user env, username
144
+ abort "The #{username} doesn't exist! Please add!".yellow unless user_exists? env, username
145
+ config.current_user = { 'username' => username, 'environment' => env, 'password' => config[env][username].password, 'access_token' => config[env][username].access_token }
146
+ write_config
147
+ end
148
+
149
+ # Sets access token for current user
150
+ #
151
+ # @param [String] token the access token to store in config hash
152
+ def set_access_token token
153
+ current_user.access_token = token
154
+ config[current_user.environment].access_token = token
155
+ write_config
156
+ end
157
+
158
+ # Remove user
159
+ #
160
+ # @param [String] username the username of the user
161
+ # @param [String] env the environment
162
+ def remove_user username, env
163
+ abort "You have no users within your config file!".yellow if config.empty?
164
+ abort "This user is currently set as the current user! Aborting!".yellow if current_user.username == username && current_user.env == env
165
+ abort "The #{username} doesn't exist!".yellow unless user_exists? env, username
166
+ config[env].delete username
167
+ write_config
168
+ end
169
+
170
+ # Check whether a user exists in the config hash
171
+ #
172
+ # @param [String] env the environment
173
+ # @param [String] username the username of the user
174
+ # @return [Boolean]
175
+ def user_exists? env, username
176
+ return false if config.nil? || config.empty?
177
+ return true if config[env].has_key? username
178
+ end
179
+
180
+ # Writes the config to file
181
+ def write_config
182
+ File.open(@config_file, "w"){|f| YAML.dump(config, f)}
183
+ end
184
+
185
+ end
186
+ end
data/lib/bwcli/me.rb ADDED
@@ -0,0 +1,21 @@
1
+ require 'bwcli/configuration'
2
+
3
+ module BWCLI
4
+
5
+ # Me subcommand
6
+ class Me < Thor
7
+
8
+ no_tasks do
9
+ def configuration
10
+ @configuration ||= BWCLI::Configuration.new
11
+ end
12
+ end
13
+
14
+ desc "me", "Users credentials"
15
+ def me
16
+ ap configuration.oauth.me
17
+ end
18
+ default_task :me
19
+
20
+ end
21
+ end
@@ -0,0 +1,26 @@
1
+ require 'bwcli/configuration'
2
+
3
+ module BWCLI
4
+
5
+ # Projects subcommand
6
+ class Projects < Thor
7
+
8
+ no_tasks do
9
+ def configuration
10
+ @configuration ||= BWCLI::Configuration.new
11
+ end
12
+ end
13
+
14
+ desc "projects", "Users projects"
15
+ method_option :i, :banner => 'project id', :required => false
16
+ def projects
17
+ if options[:i].nil?
18
+ ap configuration.oauth.projects
19
+ else
20
+ configuration.oauth.project options[:i]
21
+ end
22
+ end
23
+ default_task :projects
24
+
25
+ end
26
+ end
data/lib/bwcli.rb ADDED
@@ -0,0 +1,33 @@
1
+ require 'thor'
2
+ require 'hashie'
3
+ require 'awesome_print'
4
+ require 'bwapi'
5
+ require 'bwcli/configuration'
6
+ require 'bwcli/config'
7
+ require 'bwcli/me'
8
+ require 'bwcli/projects'
9
+
10
+ module BWCLI
11
+
12
+ # BWCLI command
13
+ class BW < Thor
14
+
15
+ no_tasks do
16
+ def configuration
17
+ @configuration ||= BWCLI::Configuration.new
18
+ end
19
+ end
20
+
21
+ desc "config", "User configuration"
22
+ subcommand "config", Config
23
+
24
+ desc "me", "Me endpoint"
25
+ subcommand "me", Me
26
+
27
+ desc "projects", "Project endpoints"
28
+ subcommand "projects", Projects
29
+
30
+ include Thor::Actions
31
+
32
+ end
33
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bwcli
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Chrisp
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.13.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.13.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.12.2
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.12.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bwapi
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.18.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.18.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: awesome_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: 1.1.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: 1.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: hashie
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 2.0.5
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 2.0.5
97
+ description: A CLI interface to interact with the Brandwatch v2 API
98
+ email: jonathan@brandwatch.com
99
+ executables:
100
+ - bwcli
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE
107
+ - README.md
108
+ - bin/bwcli
109
+ - bwcli.gemspec
110
+ - lib/bwcli.rb
111
+ - lib/bwcli/.DS_Store
112
+ - lib/bwcli/config.rb
113
+ - lib/bwcli/configuration.rb
114
+ - lib/bwcli/me.rb
115
+ - lib/bwcli/projects.rb
116
+ homepage: https://github.com/jonathanchrisp/bwcli
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: 1.9.2
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.5
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Brandwatch CLI
140
+ test_files: []
141
+ has_rdoc: