del 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7f262a1fbdb6bb5a8928920aaa055ac37d1fde2cd789b17bbd66bb9ed0216d6b
4
- data.tar.gz: 525e7f6afa5e20b3f59bc6df19b013b0eea9b8e33208dbb100ddd819f2df2e6f
3
+ metadata.gz: 275fb2e7d68148752f83c9e9c01d72f03631822c8f8c51261b6bc78917e25c24
4
+ data.tar.gz: 7071efd3d8cde47caba440ae0016bcef36df20b99cb65c69067bd967a8b3c473
5
5
  SHA512:
6
- metadata.gz: 4916e972d8a2f76b43b74c26fecc74491defdde8664039a7278ea17962e42a823430e106c9c0dd004ad744ea11de7a88ef3a90e06fc4b3fd8a60e077569e6351
7
- data.tar.gz: bfd3f1bda27c2b65a6222aa1cedc79f004e592a91880fddac74ffca32f2f59154bb32c3c14fc787cdf6f61fa7efd0b565eb5e160a25b866928357b92032e7e94
6
+ metadata.gz: bdd37cdf1329695640e361542372ca9d0195d31c481526751c21417c38303ca9e0001d6568bd5b900c5d9ae303fb7088769712ec62eb91a121fcb5d6c65d193b
7
+ data.tar.gz: c2486029c1495bfde34aaa803a9d20398847feeb1d4669aea2a01dc95955bec795195206f3e0901eba6da27963e6c00fbdb3011eaf19a0897839802ff57b3e18
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Del
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/del`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ Del is a funky robosapien.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,7 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ $ del routes.rb # see lib/del/examples/routes.rb for an example
26
24
 
27
25
  ## Development
28
26
 
@@ -32,7 +30,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
30
 
33
31
  ## Contributing
34
32
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/del.
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mokhan/del.
36
34
 
37
35
  ## License
38
36
 
data/exe/del CHANGED
@@ -3,6 +3,8 @@
3
3
  require "del"
4
4
  require "pathname"
5
5
 
6
+ startup_file = Pathname.new(ARGV[0]) if ARGV[0]
6
7
  Del.start(
7
- dotenv_file: ENV.fetch("DELRC", Pathname.new(Dir.home).join(".delrc"))
8
+ dotenv_file: ENV.fetch("DELRC", Pathname.new(Dir.home).join(".delrc")),
9
+ startup_file: startup_file,
8
10
  )
@@ -0,0 +1,29 @@
1
+ module Del
2
+ class Configuration
3
+ attr_accessor :default_rooms
4
+ attr_accessor :host
5
+ attr_accessor :jid
6
+ attr_accessor :jid
7
+ attr_accessor :logger
8
+ attr_accessor :muc_domain
9
+ attr_accessor :name
10
+ attr_accessor :password
11
+ attr_accessor :rooms
12
+ attr_accessor :router
13
+ attr_accessor :users
14
+
15
+ def initialize
16
+ @default_rooms = ENV.fetch("DEL_ROOMS", '').split(',')
17
+ @host = ENV.fetch("DEL_HOST", 'chat.hipchat.com')
18
+ @jid = ENV.fetch("DEL_JID")
19
+ @logger = Logger.new(STDOUT)
20
+ @logger.level = Logger::INFO
21
+ @muc_domain = ENV.fetch("DEL_MUC_DOMAIN", "conf.hipchat.com")
22
+ @name = ENV.fetch("DEL_FULL_NAME")
23
+ @password = ENV.fetch("DEL_PASSWORD")
24
+ @rooms = Repository.new
25
+ @router = DefaultRouter.new
26
+ @users = Repository.new
27
+ end
28
+ end
29
+ end
@@ -4,8 +4,8 @@ module Del
4
4
 
5
5
  def initialize(configuration:)
6
6
  @configuration = configuration
7
- @rooms = configuration[:rooms]
8
- @users = configuration[:users]
7
+ @rooms = configuration.rooms
8
+ @users = configuration.users
9
9
  @mucs = {}
10
10
  end
11
11
 
@@ -14,9 +14,9 @@ module Del
14
14
  Del.logger.error(error)
15
15
  disconnect
16
16
  end
17
- client.connect(configuration[:host])
17
+ client.connect(configuration.host)
18
18
  sleep 0.0001 until client.is_connected?
19
- client.auth(configuration[:password])
19
+ client.auth(configuration.password)
20
20
  roster = Jabber::Roster::Helper.new(client, false)
21
21
  roster.add_update_callback do |old_item, item|
22
22
  users.upsert(item['jid'], User.new(item['jid'], item)) if item
@@ -25,13 +25,13 @@ module Del
25
25
  roster.wait_for_roster
26
26
  client.add_message_callback do |message|
27
27
  next if message.type == :error || message.body.nil?
28
- user = configuration[:users].find_by(message.from.strip)
28
+ user = configuration.users.find_by(message.from.strip)
29
29
  robot.receive(message.body, source: Source.new(user: user))
30
30
  end
31
31
  client.send(Jabber::Presence.new(:chat))
32
- configuration[:default_rooms].each do |room|
32
+ configuration.default_rooms.each do |room|
33
33
  Del.logger.debug("Joining #{room} as #{robot.name}")
34
- room_jid = jid_for(room, configuration[:muc_domain].dup, robot.name)
34
+ room_jid = jid_for(room, configuration.muc_domain.dup, robot.name)
35
35
  stripped_jid = room_jid.strip.to_s
36
36
  next if @mucs[stripped_jid]
37
37
 
@@ -44,7 +44,7 @@ module Del
44
44
  end
45
45
  muc.join(room_jid)
46
46
  end
47
- list_rooms(configuration[:muc_domain]).each do |room|
47
+ list_rooms(configuration.muc_domain).each do |room|
48
48
  rooms.upsert(room)
49
49
  end
50
50
  end
@@ -61,7 +61,7 @@ module Del
61
61
  end
62
62
 
63
63
  def disconnect
64
- puts "byte me!"
64
+ Del.logger.info("byte me!")
65
65
  client.close
66
66
  rescue IOError, SystemCallError => error
67
67
  Del.logger.error(error)
@@ -74,7 +74,7 @@ module Del
74
74
  end
75
75
 
76
76
  def jid
77
- @jid ||= jid_for(configuration[:jid], "chat.hipchat.com", "bot")
77
+ @jid ||= jid_for(configuration.jid, "chat.hipchat.com", "bot")
78
78
  end
79
79
 
80
80
  def list_rooms(muc_domain)
@@ -1,9 +1,19 @@
1
1
  module Del
2
2
  class DefaultRouter
3
- def route(message)
4
- Del.logger.info(message.to_s)
3
+ def initialize(routes = [])
4
+ @routes = routes
5
+ end
5
6
 
6
- message.reply(message.text.reverse)
7
+ def register(pattern, &block)
8
+ @routes.push(pattern: pattern, command: block)
9
+ end
10
+
11
+ def route(message)
12
+ @routes.each do |route|
13
+ if route[:pattern].match(message.text)
14
+ route[:command].call(message)
15
+ end
16
+ end
7
17
  end
8
18
  end
9
19
  end
@@ -0,0 +1,7 @@
1
+ Del.configure do |config|
2
+ puts "Registering custom routes."
3
+
4
+ config.router.register(/.*/) do |message|
5
+ message.reply(message.text.reverse)
6
+ end
7
+ end
data/lib/del/robot.rb CHANGED
@@ -6,15 +6,16 @@ module Del
6
6
 
7
7
  def initialize(configuration:)
8
8
  @connection = Connection.new(configuration: configuration)
9
- @jid = configuration[:jid]
10
- @name = configuration[:name]
11
- @router = configuration[:router]
12
- @users = configuration[:users]
13
- @rooms = configuration[:rooms]
9
+ @jid = configuration.jid
10
+ @name = configuration.name
11
+ @router = configuration.router
12
+ @users = configuration.users
13
+ @rooms = configuration.rooms
14
14
  end
15
15
 
16
16
  def get_funky!
17
17
  connection.connect(self)
18
+ Del.logger.info("It's fire! 🔥")
18
19
  sleep
19
20
  rescue Interrupt
20
21
  connection.disconnect
data/lib/del/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Del
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/lib/del.rb CHANGED
@@ -6,6 +6,7 @@ require "xmpp4r/muc/helper/simplemucclient"
6
6
  require "xmpp4r/roster/helper/roster"
7
7
 
8
8
  require "del/connection"
9
+ require "del/configuration"
9
10
  require "del/default_router"
10
11
  require "del/message"
11
12
  require "del/repository"
@@ -15,11 +16,16 @@ require "del/user"
15
16
  require "del/version"
16
17
 
17
18
  module Del
18
- def self.start(dotenv_file:)
19
+ def self.start(dotenv_file:, startup_file:)
19
20
  puts "Loading... #{dotenv_file}"
20
21
  Dotenv.load(dotenv_file.to_s)
21
- puts "It's fire! 🔥"
22
- Del.logger.level = Logger::INFO
22
+ Del.configure do |config|
23
+ config.router.register(/.*/) do |message|
24
+ logger.debug(message.to_s)
25
+ end
26
+ end
27
+ load startup_file if startup_file && File.exist?(startup_file)
28
+
23
29
  del = Robot.new(configuration: configuration)
24
30
  del.get_funky!
25
31
  end
@@ -29,21 +35,10 @@ module Del
29
35
  end
30
36
 
31
37
  def self.configuration
32
- @configuration ||= {
33
- default_rooms: ENV.fetch("DEL_ROOMS", '').split(','),
34
- host: ENV.fetch("DEL_HOST"),
35
- jid: ENV.fetch("DEL_JID"),
36
- logger: Logger.new(STDOUT),
37
- muc_domain: ENV.fetch("DEL_MUC_DOMAIN"),
38
- name: ENV.fetch("DEL_FULL_NAME"),
39
- password: ENV.fetch("DEL_PASSWORD"),
40
- rooms: Repository.new,
41
- router: DefaultRouter.new,
42
- users: Repository.new,
43
- }
38
+ @configuration ||= Configuration.new
44
39
  end
45
40
 
46
41
  def self.logger
47
- @logger ||= configuration[:logger]
42
+ @logger ||= configuration.logger
48
43
  end
49
44
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: del
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - mo
@@ -100,8 +100,10 @@ files:
100
100
  - del.gemspec
101
101
  - exe/del
102
102
  - lib/del.rb
103
+ - lib/del/configuration.rb
103
104
  - lib/del/connection.rb
104
105
  - lib/del/default_router.rb
106
+ - lib/del/examples/routes.rb
105
107
  - lib/del/message.rb
106
108
  - lib/del/repository.rb
107
109
  - lib/del/robot.rb