ticking_away 0.0.2 → 0.0.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
  SHA256:
3
- metadata.gz: 0e696ea9fcd6af36dfc8a9bd7be89923a35fdf70eb4d05bb9fe3c3003606de6f
4
- data.tar.gz: 6d22e898217913ac1b7bb349f62c83d220c2d83be92bf2b5c4976e382cc4a90e
3
+ metadata.gz: 0c06d94aad03bc4e9947771ec24dd99e561b1d45693b2333147c8dd957ffff87
4
+ data.tar.gz: c829b7fb99e9afb7c36c6ab81a28a41dbae087ebbe74c52dadb43430d0528588
5
5
  SHA512:
6
- metadata.gz: 487b5fc04e9f0994bd8e082e09d389496457bb7357a3f770076b83b441f10caa76a839835e89e2a9b2e3afadc072c4dd034c3c6b754e5f2f0793dc789bcf203f
7
- data.tar.gz: 3dbe408e1701b270c3494c1701ee3dbc97f395e1b83e3b38994dc5edd96b603d4af7b34a806bc190b14b97d76359a738b42101ce0cfea00212b7f674f3bb4c9c
6
+ metadata.gz: 91440c550061b71bda6bcd8c730485db7d3dba0f6fcdfe8230efc2c4bf90be90395fee1d75f7a04b0c5f9d463f33eb21b5811d95cd7a2eb6b9037efccc77b6f0
7
+ data.tar.gz: 191755c7921062d692e4e6344c0056a5b820a4fce35b90cc4077dd918029eddb6ad3ce247aec49f63b1fc3828f86ff8ff488ceefef60e652e478ca7bedb485ba
data/lib/ticking_away.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'ticking_away/chat_bot'
2
2
  require 'ticking_away/cinch/plugins/time_info'
3
3
  require 'ticking_away/world_time'
4
- require 'ticking_away/custom_errors'
4
+ require 'ticking_away/errors'
5
5
  require 'ticking_away/json_file_storage'
6
6
  require 'ticking_away/bot'
@@ -20,7 +20,7 @@ module TickingAway
20
20
  end
21
21
 
22
22
  # Send a string to the Bot in the format of
23
- # <user_name>: <message>
23
+ # <user_name>: <message> or just <message>
24
24
  # Only !timeat <tz_info> and !timepopularity <tz_info or prefix>
25
25
  # commands will return a string response
26
26
  def chat(msg)
@@ -64,7 +64,7 @@ module TickingAway
64
64
  msg[cmd_length..msg.length]
65
65
  end
66
66
 
67
- # Generate the time message, returning "unknown location"
67
+ # Generate the time message, returning "unknown timezone"
68
68
  # for any unrecognized time zones and logging any uncaught
69
69
  # errors before returning an excuse at random.
70
70
  # Stats will only be incremented if the api call was successful
@@ -76,7 +76,7 @@ module TickingAway
76
76
  rescue TickingAway::Errors::UnrecognizedTimeZone => e
77
77
  puts e.message
78
78
  'unknown timezone'
79
- rescue => e
79
+ rescue TickingAway::Errors::TimeTravelIsHard => e
80
80
  puts e.message
81
81
  EXCUSES.sample
82
82
  end
@@ -35,25 +35,16 @@
35
35
  # Copyright © 2021 Jeff Wood
36
36
  #
37
37
  # This program is free software: you can redistribute it and/or modify
38
- # it under the terms of the GNU Lesser General Public License as published by
39
- # the Free Software Foundation, either version 3 of the License, or
40
- # (at your option) any later version.
41
- #
42
- # This program is distributed in the hope that it will be useful,
43
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
44
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45
- # GNU Lesser General Public License for more details.
46
- #
47
- # You should have received a copy of the GNU Lesser General Public License
48
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
38
+ # it under the terms of the MIT License.
49
39
  require 'cinch'
50
40
 
51
41
  module TickingAway
52
42
  class TimeInfo
53
43
  include ::Cinch::Plugin
54
44
 
55
- match (/timeat */), method: :timeat
56
- match (/timepopularity */), method: :timepopularity
45
+ # regex needs work
46
+ match %r{timeat [a-zA-Z0-9_\-/]+}, method: :timeat
47
+ match %r{timepopularity [a-zA-Z0-9_\-/]+}, method: :timepopularity
57
48
 
58
49
  listen_to :connect, method: :on_connect
59
50
 
@@ -2,6 +2,7 @@ module TickingAway
2
2
  module Errors
3
3
  # Custom Errors
4
4
  class UnrecognizedTimeZone < StandardError; end
5
- class ApiUrlNotFound < StandardError; end
5
+
6
+ class TimeTravelIsHard < StandardError; end
6
7
  end
7
8
  end
@@ -24,20 +24,22 @@ module TickingAway
24
24
  end
25
25
 
26
26
  # Get the number of times !timeat was called for a
27
- # tz_info or prefix. Partial prefix matches count towards
28
- # the total.
29
- # If we didn't want them to, it could check the
30
- # next char in the key after the .start_with? match. If it's outside the
31
- # length or a "/", then the prefix or tz_info matches exactly
27
+ # tz_info or prefix. Partial prefix matches do not count.
32
28
  def get_stat(stat_name)
33
29
  call_count = 0
34
30
  stats.each do |key, value|
35
- call_count += value if key.start_with?(stat_name)
31
+ call_count += value if key.start_with?(stat_name) && full_match?(stat_name, key)
36
32
  end
37
33
 
38
34
  call_count
39
35
  end
40
36
 
37
+ def full_match?(stat_name, key)
38
+ return true if key.length == stat_name.length || key[stat_name.length] == '/'
39
+
40
+ false
41
+ end
42
+
41
43
  def save_stats
42
44
  File.write(filename, JSON.dump(stats))
43
45
  end
@@ -14,19 +14,24 @@ module TickingAway
14
14
  def time_at(base_url, tz_info)
15
15
  request_url = "#{base_url}/timezone/#{tz_info}"
16
16
 
17
- response = HTTParty.get(request_url)
17
+ response = call_api(request_url)
18
18
  handle_response(response, request_url)
19
+ end
20
+
21
+ def call_api(request_url)
22
+ HTTParty.get(request_url)
19
23
  rescue => e
20
- puts "Could not connect to time server #{request_url}"
21
- raise e
24
+ raise TickingAway::Errors::TimeTravelIsHard, e.message
22
25
  end
23
26
 
24
27
  def handle_response(response, request_url)
25
28
  # Convert JSON response to Hash, handling an empty or nil body
26
- parsed_response = response.body.nil? || response.body.empty? ? {} : JSON.parse(response.body)
29
+ parsed_response = parse_response(response.body)
27
30
 
28
31
  case response.code
29
32
  when 200
33
+ raise TickingAway::Errors::UnrecognizedTimeZone, 'Error: non-time response' unless parsed_response.is_a?(Hash)
34
+
30
35
  puts "Event: Retreived current time for #{parsed_response['timezone']}: #{parsed_response['datetime']}"
31
36
  when 404
32
37
  # Differentiate between an unknown location response and a random 404 by checking the response body
@@ -34,14 +39,20 @@ module TickingAway
34
39
  raise TickingAway::Errors::UnrecognizedTimeZone, "Error: Unrecognized Time Zone #{request_url}"
35
40
  end
36
41
 
37
- raise TickingAway::Errors::ApiUrlNotFound, "Error: 404 response for #{request_url}"
42
+ raise TickingAway::Errors::TimeTravelIsHard, "Error: 404 response for #{request_url}"
38
43
  else
39
- raise "Error: #{response.code} #{parsed_response}"
44
+ raise TickingAway::Errors::TimeTravelIsHard, "Error: #{response.code} #{parsed_response}"
40
45
  end
41
46
 
42
47
  # Convert the time from a RFC3339 formatted string to a Time object
43
48
  Time.parse(parsed_response['datetime'])
44
49
  end
50
+
51
+ def parse_response(body)
52
+ JSON.parse(body)
53
+ rescue => e
54
+ raise TickingAway::Errors::TimeTravelIsHard, e.message
55
+ end
45
56
  end
46
57
  end
47
58
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ticking_away
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Wood
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-09 00:00:00.000000000 Z
11
+ date: 2021-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cinch
@@ -104,7 +104,7 @@ files:
104
104
  - lib/ticking_away/bot.rb
105
105
  - lib/ticking_away/chat_bot.rb
106
106
  - lib/ticking_away/cinch/plugins/time_info.rb
107
- - lib/ticking_away/custom_errors.rb
107
+ - lib/ticking_away/errors.rb
108
108
  - lib/ticking_away/json_file_storage.rb
109
109
  - lib/ticking_away/world_time.rb
110
110
  homepage: https://github.com/woodjeffrey2/ticking_away