cs 0.1.1beta → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: da893437e3bafdedfc45e0ee607d2bf923faa73c
4
- data.tar.gz: 4c63a68ad28063ce2a61968ec71da47c7e41fb94
3
+ metadata.gz: 3c833d41fcb9aa813c7aeda87ad3aac228ace39d
4
+ data.tar.gz: 0e73999b1157e0641241a7fbf1ddb985416e16df
5
5
  SHA512:
6
- metadata.gz: 2ab95c33150cc511f53a057280e83fe35eafcdd61b48acab6ef6bce312511af17c295945e06412b36c98446fc012c60df6aebd7e03017a8cc275e866461bdd36
7
- data.tar.gz: 3973ad261722529b962970e5c956eb921045820cde9ccff0d268824024ffa3cddd487a9bca7faa56f1430320bbd93c8ac45453af135371aa70e1cead6debbd0a
6
+ metadata.gz: b2397a73ae5ba94dab965f2d47a2e4f00d26611fbf1e382761a9488527c105e9c904ae053f4aea8815ba85c0563f3ff5bc2d047da9909ee2145cf93eabc6c093
7
+ data.tar.gz: 53f83903a01000f4690768f40bdc321340071147559d33126cdd6a1ee1f7ca4ac04fdd1c919ab0cd0f941989e79c38f4237a08a105f8dead596eb2ae88fc9f28
@@ -2,8 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 2.0.0
4
4
  - 1.9.3
5
- - 1.9.2
6
5
  - jruby-19mode
7
- - rbx-19mode
6
+ - rbx-2.2.1
8
7
  - jruby-head
9
8
  script: bundle exec rspec spec
data/Gemfile CHANGED
@@ -3,7 +3,15 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in cs.gemspec
4
4
  gemspec
5
5
  gem "time-lord"
6
- gem 'json', '~> 1.7.7'
6
+ gem 'json'
7
+
8
+ #
9
+ # Rubinius does not load ruby stdlib by default
10
+ #
11
+
12
+ platforms :rbx do
13
+ gem 'rubysl'
14
+ end
7
15
 
8
16
  group :test do
9
17
  gem "rake", "~> 10.1.0"
data/README.md CHANGED
@@ -129,6 +129,80 @@ session.dump_to_text("/tmp/output.txt")
129
129
  session.open_in_browser
130
130
  ```
131
131
 
132
+ ## Command Line Executable
133
+
134
+ This gem also contain executables to run from command line. The main executable is `cs`
135
+
136
+ ```bash
137
+ $ cs -h
138
+ Usage: cs <command>
139
+
140
+ -h, --help Show this message
141
+
142
+ Available commands are:
143
+
144
+ console Run REPL console based on PRY
145
+ password Generate hased password from plaintext
146
+
147
+ ```
148
+
149
+ The console executable will run an REPL (Read Evaluate Print Loop) session based on pry.
150
+
151
+ install `pry` first in order to use it. `pry-doc` and `pry-nav` is optional
152
+
153
+ ```bash
154
+ $ gem install pry
155
+ $ gem install pry-doc
156
+ $ gem install pry-nav
157
+ ```
158
+
159
+ you can have a configuration file on `~/.cs.yml` which contain the following
160
+
161
+ ```yaml
162
+ users:
163
+ user1:
164
+ username: "user1@example.com"
165
+ password: ""
166
+ password_md5: 1234567890abcdef1234567890abcdef
167
+ user2:
168
+ username: "user2@example.com"
169
+ password: "V3rryS3curePaswd"
170
+ password_md5: ""
171
+
172
+ default_user: user1
173
+ ```
174
+
175
+ you can either fill in the password or md5-hashed password. It will use `password_md5` if you fill in both
176
+
177
+
178
+ now you can use it on cs console
179
+
180
+ ```bash
181
+ $ cs console
182
+ CS console 0.1.1
183
+
184
+ # create a client with default user
185
+
186
+ [1] pry(main)> client = new_client()
187
+ Successfully logged in with user 'user1@example.com'
188
+ => #<CS::Client:0x00000004cd7540
189
+ @base_uri="https://api.sense-os.nl",
190
+ @session=SESSION_ID "1234567890abcdefg1.23456789">
191
+
192
+ # create a client with another user
193
+
194
+ [2] pry(main)> user2_client = new_client("user2")
195
+ Successfully logged in with user 'user2@example.com'
196
+ => #<CS::Client:0x00000004cd7560
197
+ @base_uri="https://api.sense-os.nl",
198
+ @session=SESSION_ID "1234567890abcdefg1.23456789">
199
+
200
+ # create an empty client object and will not do the login
201
+
202
+ [3] pry(main)> empty_client = new_client(false)
203
+ > #<CS::Client:0x00000004cf2cf0 @base_uri="https://api.sense-os.nl">
204
+ ```
205
+
132
206
  ## Testing
133
207
 
134
208
  $ cp spec/support/spec_config.yml.sample spec/support/spec_config.yml
data/bin/cs ADDED
@@ -0,0 +1,58 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # (C) Ahmy Yulrizka (ahmy@sense-os.nl)
4
+
5
+ require 'optparse'
6
+
7
+ options = {password: '', double: false}
8
+
9
+ def print_help
10
+ <<EOL
11
+
12
+ Available commands are:
13
+
14
+ console Run REPL console based on PRY
15
+ password Generate hased password from plaintext
16
+
17
+ EOL
18
+ end
19
+
20
+ def print_error_and_exit(message, code=1)
21
+ puts "Error: #{message}"
22
+ exit code
23
+ end
24
+
25
+ if ARGV.empty?
26
+ puts "Usage: cs <command>"
27
+ puts print_help
28
+ exit 0
29
+ end
30
+
31
+ current_path = File.dirname(__FILE__)
32
+
33
+ if ARGV[0].nil? or ARGV[0].empty?
34
+ print_error_and_exit("Invalid command")
35
+ end
36
+
37
+ command = ARGV[0]
38
+ executable = "cs-#{command}"
39
+ executable_path = File.join(current_path, executable)
40
+
41
+ if !File.exists?(executable_path)
42
+ OptionParser.new do |opts|
43
+ opts.banner = "Usage: cs <command>"
44
+
45
+ opts.separator ""
46
+ opts.on_tail("-h", "--help", "Show this message") do
47
+
48
+ puts opts
49
+ puts print_help
50
+ exit
51
+ end
52
+ end.parse!
53
+
54
+ print_error_and_exit("Invalid command '#{command}'")
55
+ end
56
+
57
+ arguments = ARGV.slice(1..-1)
58
+ exec(executable_path, *arguments)
@@ -0,0 +1,154 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # (C) Ahmy Yulrizka (ahmy@sense-os.nl)
4
+
5
+ $0 = 'pry'
6
+
7
+ begin
8
+ require 'pry'
9
+ rescue LoadError
10
+ require 'rubygems'
11
+ require 'pry'
12
+ end
13
+
14
+ require 'cs'
15
+ require 'logger'
16
+ require 'optparse'
17
+ require 'digest/md5'
18
+
19
+ DEBUG = Logger::DEBUG
20
+ ERROR = Logger::ERROR
21
+ FATAL = Logger::FATAL
22
+ INFO = Logger::INFO
23
+ UNKNOWN = Logger::UNKNOWN
24
+ WARN = Logger::WARN
25
+
26
+ CS::load_CLI
27
+
28
+ ##
29
+ # OPTION Parsing
30
+ ##
31
+ OPTIONS = {
32
+ config_file: "#{ENV['HOME']}/.cs.yml",
33
+ log_level: INFO
34
+ }
35
+
36
+ OptionParser.new do |opts|
37
+ opts.banner = "Usage: cs console [options]"
38
+
39
+ opts.on("-c", "--config", "Config file default to #{OPTIONS[:config_file]}") do |v|
40
+ OPTIONS[:config_file] = v
41
+ end
42
+
43
+ opts.on("-v", "--verbosity <log_level>", "Log verbosity. <log_level> is one of: debug, error, fatal, info, unknown, warns. Default to info") do |v|
44
+ mapping = {
45
+ "debug" => DEBUG,
46
+ "error" => ERROR,
47
+ "fatal" => FATAL,
48
+ "info" => INFO,
49
+ "unknown" => UNKNOWN,
50
+ "warns" => WARN
51
+ }
52
+
53
+ level = mapping[v.downcase]
54
+ OPTIONS[:log_level] = level
55
+ end
56
+
57
+ opts.separator ""
58
+ opts.on_tail("-h", "--help", "Show this message") do
59
+ puts opts
60
+ exit
61
+ end
62
+ end.parse!
63
+
64
+ ##
65
+ # Helper Method
66
+ ##
67
+
68
+ STDOUT_LOGGER = Logger.new(STDOUT)
69
+ STDOUT_LOGGER.level = OPTIONS[:log_level]
70
+
71
+ def print_error(message)
72
+ STDERR.puts "Error: #{message}"
73
+ end
74
+
75
+ def config(key=nil)
76
+ CS::CLI::Config::get(key)
77
+ end
78
+
79
+ def new_client(user=nil)
80
+ client = CS::Client.new
81
+
82
+ if user == false
83
+ return client
84
+ end
85
+
86
+ user_creds = nil
87
+ if user == false
88
+ return client
89
+ elsif user == nil # load default user
90
+ default_user = config["default_user"]
91
+ user_creds = config["users"][default_user] rescue nil
92
+ else # load user by reference
93
+ user_creds = config["users"][user]
94
+ end
95
+
96
+
97
+ username = user_creds["username"]
98
+ password = user_creds["password_md5"]
99
+
100
+ if password.nil? || password.empty?
101
+ password = user_creds["password"]
102
+ password = Digest::MD5.hexdigest(password)
103
+ end
104
+
105
+
106
+ if client.login!(username, password, false)
107
+ STDOUT.puts "Successfully logged in with user '#{username}'"
108
+ else
109
+ print_error("Failed login with user #{username} -> #{client.session.errors}")
110
+ end
111
+
112
+ client.session.logger = STDOUT_LOGGER
113
+ client
114
+ end
115
+
116
+ def set_log_level(level)
117
+ OPTIONS[:log_level] = level
118
+ STDOUT_LOGGER.level = level
119
+ end
120
+
121
+ def const_to_level_log(value)
122
+ mapping = {
123
+ DEBUG => "debug",
124
+ ERROR => "error",
125
+ FATAL => "fatal",
126
+ INFO => "info",
127
+ UNKNOWN => "unknown",
128
+ WARN => "warns"
129
+ }
130
+
131
+ mapping[value]
132
+ end
133
+
134
+ def log_level?
135
+ const_to_level_log(OPTIONS[:log_level])
136
+ end
137
+
138
+ ##
139
+ # MAIN
140
+ ##
141
+
142
+ begin
143
+ CS::CLI::Config::load_config(OPTIONS[:config_file])
144
+ rescue => e
145
+ if File.exists?(OPTIONS[:config_file])
146
+ throw e
147
+ end
148
+ end
149
+
150
+ puts "CS console #{CS::VERSION}"
151
+ puts
152
+
153
+ # Process command line options and run Pry
154
+ Pry::CLI.parse_options
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # (C) Ahmy Yulrizka (ahmy@sense-os.nl)
4
+
5
+ require 'optparse'
6
+ require 'io/console'
7
+ require 'digest/md5'
8
+
9
+ options = {password: '', double: false}
10
+ OptionParser.new do |opts|
11
+ opts.banner = "Usage: cs password [options]"
12
+
13
+ opts.on("-p", "--password password", "pass password as command line") do |v|
14
+ options[:password] = v
15
+ end
16
+
17
+ opts.on("-d", "--double-md5", "Hash the password twice") do
18
+ options[:double] = true
19
+ end
20
+
21
+ opts.separator ""
22
+ opts.on_tail("-h", "--help", "Show this message") do
23
+
24
+ puts opts
25
+ exit
26
+ end
27
+ end.parse!
28
+
29
+ def hash(plaintext)
30
+ Digest::MD5.hexdigest(plaintext)
31
+ end
32
+
33
+ if options[:password].nil? || options[:password].empty?
34
+ print "password:"
35
+ v = STDIN.noecho(&:gets).chomp
36
+ options[:password] = v
37
+ end
38
+
39
+ retval = hash(options[:password])
40
+
41
+ retval = hash(retval) if options[:double]
42
+
43
+ puts
44
+ puts retval
data/cs.gemspec CHANGED
@@ -8,6 +8,8 @@ Gem::Specification.new do |gem|
8
8
  gem.summary = %q{Client library to communicate with CommonSense written in ruby}
9
9
  gem.homepage = "https://github.com/senseobservationsystems/commonsense-ruby-lib"
10
10
 
11
+ gem.required_ruby_version = '>= 1.9.3'
12
+
11
13
  gem.files = `git ls-files`.split($\)
12
14
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
15
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
@@ -15,6 +17,7 @@ Gem::Specification.new do |gem|
15
17
  gem.require_paths = ["lib"]
16
18
  gem.version = CS::VERSION
17
19
  gem.add_development_dependency("launchy", "~> 2.3")
18
- gem.add_dependency('httparty', '~> 0.11')
20
+ gem.add_development_dependency("pry", "~> 0.9.12.6")
21
+ gem.add_dependency('httparty', '~> 0.12')
19
22
  gem.add_dependency('oauth', '~> 0.4')
20
23
  end
data/lib/cs.rb CHANGED
@@ -9,12 +9,17 @@ require "cs/end_point/user"
9
9
  require "cs/end_point/group"
10
10
  require "cs/end_point/sensor"
11
11
  require "cs/end_point/sensor_data"
12
+ require "cs/end_point/trigger"
13
+ require "cs/end_point/notification"
12
14
  require "cs/parameter_processor"
13
15
  require "cs/relation"
14
16
  require "cs/relation/sensor_relation"
15
17
  require "cs/relation/sensor_data_relation"
16
18
  require "cs/relation/user_relation"
17
19
  require "cs/relation/group_relation"
20
+ require "cs/relation/trigger_relation"
21
+ require "cs/relation/notification_relation"
22
+ require "cs/collection/sensor_data_collection"
18
23
 
19
24
  module CS
20
25
 
@@ -65,10 +70,10 @@ module CS
65
70
  #
66
71
  # client = CS::Client.new
67
72
  # client.login!('username', 'password')
68
- def login!(user, password)
73
+ def login!(user, password, digest=true)
69
74
  @session = Session.new(base_uri: @base_uri)
70
75
  @session.logger = logger
71
- @session.login(user, password)
76
+ @session.login(user, password, digest)
72
77
  end
73
78
 
74
79
  # Create a new session to CommonSense using username and plain text password
@@ -76,8 +81,8 @@ module CS
76
81
  #
77
82
  # client = CS::Client.new
78
83
  # client.login('username', 'password')
79
- def login(user, password)
80
- login!(user, password) rescue false
84
+ def login(user, password, digest=true)
85
+ login!(user, password, digest) rescue false
81
86
  end
82
87
 
83
88
  # Create a new session to CommonSense using OAuth credentials
@@ -100,6 +105,16 @@ module CS
100
105
  @session.session_id = session_id
101
106
  end
102
107
 
108
+ # Create new session by specifying api_key
109
+ #
110
+ # client = CS::Client.new
111
+ # client.session_id = '12345'
112
+ def api_key=(api_key)
113
+ @session = Session.new(base_uri: @base_uri)
114
+ @session.logger = logger
115
+ @session.api_key = api_key
116
+ end
117
+
103
118
  # Retrun logged in user
104
119
  def current_user
105
120
  user = EndPoint::User.new
@@ -133,6 +148,14 @@ module CS
133
148
  Relation::GroupRelation.new(@session)
134
149
  end
135
150
 
151
+ def triggers
152
+ Relation::TriggerRelation.new(@session)
153
+ end
154
+
155
+ def notifications
156
+ Relation::NotificationRelation.new(@session)
157
+ end
158
+
136
159
  def current_groups
137
160
  group = EndPoint::Group.new
138
161
  group.session = @session
@@ -145,5 +168,9 @@ module CS
145
168
  return @session.errors if @session
146
169
  end
147
170
  end
171
+
172
+ def self.load_CLI
173
+ require "cs/cli/cli"
174
+ end
148
175
  end
149
176