rubirai 0.0.2

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.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.github/dependabot.yml +11 -0
  3. data/.github/workflows/CI.yml +64 -0
  4. data/.github/workflows/docs.yml +32 -0
  5. data/.github/workflows/pull_request.yml +34 -0
  6. data/.gitignore +145 -0
  7. data/.rubocop.yml +41 -0
  8. data/.yardopts +7 -0
  9. data/Gemfile +19 -0
  10. data/LICENSE +661 -0
  11. data/README.md +24 -0
  12. data/Rakefile +16 -0
  13. data/examples/helper.rb +3 -0
  14. data/examples/listener_example.rb +25 -0
  15. data/examples/simple_example.rb +24 -0
  16. data/lib/rubirai.rb +66 -0
  17. data/lib/rubirai/auth.rb +73 -0
  18. data/lib/rubirai/errors.rb +26 -0
  19. data/lib/rubirai/event_recv.rb +83 -0
  20. data/lib/rubirai/event_resp.rb +129 -0
  21. data/lib/rubirai/events/bot_events.rb +53 -0
  22. data/lib/rubirai/events/event.rb +115 -0
  23. data/lib/rubirai/events/message_events.rb +77 -0
  24. data/lib/rubirai/events/request_events.rb +35 -0
  25. data/lib/rubirai/events/rubirai_events.rb +17 -0
  26. data/lib/rubirai/listener.rb +44 -0
  27. data/lib/rubirai/listing.rb +37 -0
  28. data/lib/rubirai/management.rb +200 -0
  29. data/lib/rubirai/message.rb +84 -0
  30. data/lib/rubirai/messages/message.rb +306 -0
  31. data/lib/rubirai/messages/message_chain.rb +119 -0
  32. data/lib/rubirai/multipart.rb +44 -0
  33. data/lib/rubirai/objects/group.rb +23 -0
  34. data/lib/rubirai/objects/info.rb +71 -0
  35. data/lib/rubirai/objects/user.rb +30 -0
  36. data/lib/rubirai/plugin_info.rb +19 -0
  37. data/lib/rubirai/retcode.rb +18 -0
  38. data/lib/rubirai/session.rb +26 -0
  39. data/lib/rubirai/utils.rb +62 -0
  40. data/lib/rubirai/version.rb +9 -0
  41. data/misc/common.css +11 -0
  42. data/rubirai.gemspec +24 -0
  43. data/spec/auth_spec.rb +118 -0
  44. data/spec/error_spec.rb +30 -0
  45. data/spec/events/event_spec.rb +78 -0
  46. data/spec/message_spec.rb +12 -0
  47. data/spec/messages/message_chain_spec.rb +32 -0
  48. data/spec/messages/message_spec.rb +171 -0
  49. data/spec/plugin_info_spec.rb +28 -0
  50. data/spec/rubirai_bot_spec.rb +45 -0
  51. data/spec/spec_helper.rb +31 -0
  52. data/spec/utils_spec.rb +70 -0
  53. metadata +121 -0
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'errors' do
6
+ it 'should create RubiraiError with specific message' do
7
+ expect { raise Rubirai::RubiraiError, 'message' }
8
+ .to raise_error(Rubirai::RubiraiError, 'message') do |err|
9
+ expect(err).to be_a_kind_of(RuntimeError)
10
+ end
11
+ end
12
+
13
+ it 'should create HttpResponseError with specific message' do
14
+ expect { raise Rubirai::HttpResponseError, '404' }.to raise_error(
15
+ Rubirai::HttpResponseError,
16
+ 'Http Error: 404'
17
+ ) do |err|
18
+ expect(err).to be_a_kind_of(Rubirai::RubiraiError)
19
+ end
20
+ end
21
+
22
+ it 'should create MiraiError with specific message' do
23
+ Rubirai::RETURN_CODE.each do |code, msg|
24
+ expect { raise Rubirai::MiraiError, code }
25
+ .to raise_error(Rubirai::MiraiError, "Mirai error: #{code} - #{msg}") do |err|
26
+ expect(err).to be_a_kind_of(Rubirai::RubiraiError)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Rubirai::Event do
4
+ before do
5
+ end
6
+
7
+ after do
8
+ end
9
+
10
+ it 'should parse hash to a bot online event' do
11
+ hash = {
12
+ "type": 'BotOnlineEvent',
13
+ "qq": 123456
14
+ }.stringify_keys
15
+ e = Rubirai::Event.parse hash
16
+ expect(e).to be_a(Rubirai::Event)
17
+ expect(e).to be_a(Rubirai::BotEvent)
18
+ expect(e).to be_a(Rubirai::BotOnlineEvent)
19
+ expect(e.qq).to eq(hash['qq'])
20
+ end
21
+
22
+ it 'should parse hash to a bot active offline event' do
23
+ hash = {
24
+ "type": 'BotOfflineEventActive',
25
+ "qq": 123456
26
+ }.stringify_keys
27
+ e = Rubirai::Event.parse hash
28
+ expect(e).to be_a(Rubirai::BotActiveOfflineEvent)
29
+ expect(e.qq).to eq(hash['qq'])
30
+ end
31
+
32
+ it 'should parse hash to a bot forced offline event' do
33
+ hash = {
34
+ "type": 'BotOfflineEventForce',
35
+ "qq": 123456
36
+ }.stringify_keys
37
+ e = Rubirai::Event.parse hash
38
+ expect(e).to be_a(Rubirai::BotForcedOfflineEvent)
39
+ expect(e.qq).to eq(hash['qq'])
40
+ end
41
+
42
+ it 'should parse hash to a bot dropped event' do
43
+ hash = {
44
+ "type": 'BotOfflineEventDropped',
45
+ "qq": 123456
46
+ }.stringify_keys
47
+ e = Rubirai::Event.parse hash
48
+ expect(e).to be_a(Rubirai::BotDroppedEvent)
49
+ expect(e.qq).to eq(hash['qq'])
50
+ end
51
+
52
+ it 'should parse hash to a bot relogin event' do
53
+ hash = {
54
+ "type": 'BotReloginEvent',
55
+ "qq": 123456
56
+ }.stringify_keys
57
+ e = Rubirai::Event.parse hash
58
+ expect(e).to be_a(Rubirai::BotReloginEvent)
59
+ expect(e.qq).to eq(hash['qq'])
60
+ end
61
+
62
+ it 'should parse hash to a bot permission change event' do
63
+ hash = {
64
+ "type": 'BotGroupPermissionChangeEvent',
65
+ "origin": 'MEMBER',
66
+ "new": 'ADMINISTRATOR',
67
+ "current": 'ADMINISTRATOR',
68
+ "group": {
69
+ "id": 123456789,
70
+ "name": 'Mirai Technology',
71
+ "permission": 'ADMINISTRATOR'
72
+ }
73
+ }
74
+ e = Rubirai::Event.parse hash
75
+ expect(e).to be_a(Rubirai::BotGroupPermissionChangedEvent)
76
+ expect(e.class.type)
77
+ end
78
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'message api' do
6
+ before :all do
7
+ @mirai_bot = new_bot
8
+ end
9
+
10
+ after do
11
+ end
12
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Rubirai::MessageChain do
4
+ before do
5
+ end
6
+
7
+ after do
8
+ end
9
+
10
+ it 'converts objects to message chain' do
11
+ hash = {
12
+ "type": 'Quote',
13
+ "id": 123456,
14
+ "groupId": 123456789,
15
+ "senderId": 987654321,
16
+ "targetId": 9876543210,
17
+ "origin": [
18
+ { "type": 'Plain', text: 'text' }.stringify_keys
19
+ ]
20
+ }.stringify_keys
21
+ chain = Rubirai::MessageChain.make(
22
+ 'hi', 2, Rubirai::AtMessage.from(target: 114514), 3, 'hi', :good, hash
23
+ )
24
+ arr = [
25
+ { type: 'Plain', text: 'hi2' },
26
+ { type: 'At', target: 114514 },
27
+ { type: 'Plain', text: '3higood' },
28
+ hash
29
+ ].map(&:stringify_keys)
30
+ expect(chain.to_a).to eq(arr)
31
+ end
32
+ end
@@ -0,0 +1,171 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe Rubirai::Message do
4
+ before do
5
+ end
6
+
7
+ after do
8
+ end
9
+
10
+ it 'should initialize and convert Source messages correctly' do
11
+ hash = {
12
+ 'type' => 'Source',
13
+ 'id' => 123456,
14
+ 'time' => 123456
15
+ }
16
+ sm = Rubirai::SourceMessage.new hash
17
+ expect(sm).to be_a(Rubirai::SourceMessage)
18
+ expect(sm.time).to eq(123456)
19
+ expect(sm.id).to eq(123456)
20
+ expect(sm.bot).to be_nil
21
+ expect(sm.type).to eq(:Source)
22
+
23
+ expect(sm.to_h).to eq(hash)
24
+ end
25
+
26
+ it 'should initialize and convert Quote message correctly' do
27
+ hash = {
28
+ "type": 'Quote',
29
+ "id": 123456,
30
+ "groupId": 123456789,
31
+ "senderId": 987654321,
32
+ "targetId": 9876543210,
33
+ "origin": [
34
+ { "type": 'Plain', text: 'text' }.stringify_keys
35
+ ]
36
+ }.stringify_keys
37
+ qm = Rubirai::QuoteMessage.new hash
38
+ expect(qm.id).to eq(123456)
39
+ expect(qm.origin).to be_a(Rubirai::MessageChain)
40
+ expect(qm.origin.sender_id).to eq(987654321)
41
+ expect(qm.origin.messages).to be_a(Array)
42
+ expect(qm.origin.messages.length).to eq(1)
43
+ expect(qm.origin.messages[0]).to be_a(Rubirai::Message)
44
+ expect(qm.origin.messages[0].type).to eq(:Plain)
45
+ expect(qm.origin.messages[0].text).to eq('text')
46
+
47
+ expect(qm.to_h).to eq(hash)
48
+ end
49
+
50
+ it 'should initialize and convert At message correctly' do
51
+ hash = {
52
+ "type": 'At',
53
+ "target": 123456,
54
+ "display": '@Mirai'
55
+ }.stringify_keys
56
+ am = Rubirai::AtMessage.new hash
57
+ expect(am.type).to eq(:At)
58
+ expect(am.target).to eq(123456)
59
+ expect(am.display).to eq('@Mirai')
60
+
61
+ expect(am.to_h).to eq(hash)
62
+ expect(Rubirai::AtMessage.from(target: 114514).to_h).to eq({
63
+ 'type' => 'At',
64
+ 'target' => 114514
65
+ })
66
+ end
67
+
68
+ it 'should initialize and convert AtAll message correctly' do
69
+ hash = { 'type' => 'AtAll' }
70
+ aam = Rubirai::AtAllMessage.new hash
71
+ expect(aam.type).to eq(:AtAll)
72
+ expect(aam.bot).to eq(nil)
73
+
74
+ expect(aam.to_h).to eq(hash)
75
+ end
76
+
77
+ it 'should initialize and convert Face message correctly' do
78
+ hash = {
79
+ "type": 'Face',
80
+ "faceId": 123,
81
+ "name": 'bu'
82
+ }.stringify_keys
83
+ fm = Rubirai::FaceMessage.new hash
84
+ expect(fm.type).to eq(:Face)
85
+ expect(fm.face_id).to eq(123)
86
+ expect(fm.name).to eq('bu')
87
+
88
+ expect(fm.to_h).to eq(hash)
89
+ end
90
+
91
+ it 'should initialize and convert Plain message correctly' do
92
+ hash = {
93
+ "type": 'Plain',
94
+ "text": 'Mirai牛逼'
95
+ }.stringify_keys
96
+ pm = Rubirai::PlainMessage.new hash
97
+ expect(pm.type).to eq(:Plain)
98
+ expect(pm.text).to eq('Mirai牛逼')
99
+
100
+ expect(pm.to_h).to eq(hash)
101
+ end
102
+
103
+ it 'should initialize and convert Image message correctly' do
104
+ hash = {
105
+ "type": 'Image',
106
+ "imageId": '{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.mirai',
107
+ "path": nil
108
+ }.stringify_keys
109
+ im = Rubirai::ImageMessage.new hash
110
+ expect(im.type).to eq(:Image)
111
+ expect(im.path).to be_nil
112
+ expect(im.image_id).to eq(hash['imageId'])
113
+
114
+ expect(im.to_h).to eq(hash.compact)
115
+ end
116
+
117
+ it 'should initialize and convert music share message correctly' do
118
+ hash = {
119
+ "type": 'MusicShare',
120
+ "kind": 'NeteaseCloudMusic',
121
+ "title": '相见恨晚',
122
+ "summary": '彭佳慧',
123
+ "jumpUrl": 'https://y.music.163.com/m/song/280761/',
124
+ "pictureUrl": 'http://p4.music.126.net/GpsgjHB_9XgtrBVXt8XX4w==/93458488373078.jpg',
125
+ "musicUrl": 'http://music.163.com/song/media/outer/url?id=280761&userid=52707509',
126
+ "brief": '[分享]相见恨晚'
127
+ }.stringify_keys
128
+ msm = Rubirai::MusicShareMessage.new hash
129
+ expect(msm.type).to eq(:MusicShare)
130
+ expect(msm.kind).to eq(hash['kind'])
131
+ expect(msm.jump_url).to eq(hash['jumpUrl'])
132
+ expect(msm.music_url).to eq(hash['musicUrl'])
133
+ end
134
+
135
+ it 'should build correctly from hash' do
136
+ hash = {
137
+ "type": 'Face',
138
+ "faceId": 123,
139
+ "name": 'bu'
140
+ }.stringify_keys
141
+ msg = Rubirai::Message.build_from hash
142
+ expect(msg).to be_a(Rubirai::FaceMessage)
143
+ expect(msg.to_h).to eq(hash)
144
+
145
+ bot = Rubirai::Bot.new 'test'
146
+ hash = {
147
+ "type": 'Quote',
148
+ "id": 123456,
149
+ "groupId": 123456789,
150
+ "senderId": 987654321,
151
+ "targetId": 9876543210,
152
+ "origin": [
153
+ { "type": 'Plain', text: 'text' }.stringify_keys
154
+ ]
155
+ }.stringify_keys
156
+ msg = Rubirai::Message.build_from hash, bot
157
+ expect(msg).to be_a(Rubirai::QuoteMessage)
158
+ expect(msg.bot).to eq(bot)
159
+ expect(msg.to_h).to eq(hash)
160
+ end
161
+
162
+ it 'should have kind of constructor methods' do
163
+ Rubirai::Message.all_types.each do |type|
164
+ expect(Rubirai.class.method_defined?("#{type}Message")).to be_truthy
165
+ end
166
+
167
+ am = Rubirai::AtMessage(target: 123456)
168
+ expect(am).to be_a(Rubirai::AtMessage)
169
+ expect(am.target).to eq(123456)
170
+ end
171
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'plugin info api' do
6
+ before do
7
+ @mirai_bot = new_bot
8
+ end
9
+
10
+ after do
11
+ # Do nothing
12
+ end
13
+
14
+ it 'should return the plugin info' do
15
+ stub_request(:get, @mirai_bot.gen_uri('/about'))
16
+ .to_return(status: 200, body: '{
17
+ "code": 0,
18
+ "errorMessage": "",
19
+ "data": {
20
+ "version": "v1.0.0"
21
+ }
22
+ }')
23
+ res = @mirai_bot.about
24
+ expect(res).to be_a_kind_of(Hash)
25
+ expect(res).to have_key('version')
26
+ expect(res['version']).to eq('v1.0.0')
27
+ end
28
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'rubirai bot' do
6
+ before do
7
+ # Do nothing
8
+ end
9
+
10
+ after do
11
+ # Do nothing
12
+ end
13
+
14
+ it 'should be able to generate a new bot' do
15
+ res = Rubirai::Bot.new 'test', 123
16
+ expect(res).to be_a_kind_of(Rubirai::Bot)
17
+ end
18
+
19
+ it 'should generate correct uri' do
20
+ bot = Rubirai::Bot.new 'test_hostname', 1145
21
+ uri = bot.gen_uri('path')
22
+ expect(uri).to be_a_kind_of(URI::HTTP)
23
+ expect(uri.to_s).to eq('http://test_hostname:1145/path')
24
+
25
+ expect(bot.gen_uri('/a/').to_s).to eq('http://test_hostname:1145/a/')
26
+
27
+ bot = Rubirai::Bot.new 'host'
28
+ expect(bot.gen_uri('/b').to_s).to eq('http://host/b')
29
+ end
30
+
31
+ it 'should check types' do
32
+ bot = Rubirai::Bot
33
+ expect { bot.ensure_type_in(:x, *%i[x y z]) }.not_to raise_error
34
+ expect { bot.ensure_type_in(:x, :X) }.not_to raise_error
35
+ expect { bot.ensure_type_in(:xAb, *%i[XaB Y z]) }.not_to raise_error
36
+ expect { bot.ensure_type_in(:x, *%i[y z]) }.to raise_error(
37
+ Rubirai::RubiraiError,
38
+ 'not valid type: should be one of ["y", "z"]'
39
+ )
40
+ expect { bot.ensure_type_in(:x, :y) }.to raise_error(
41
+ Rubirai::RubiraiError,
42
+ 'not valid type: should be one of ["y"]'
43
+ )
44
+ end
45
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'simplecov'
4
+ SimpleCov.start do
5
+ project_name 'rubirai'
6
+ track_files 'lib/**/*.rb'
7
+ add_filter '/spec/'
8
+ end
9
+
10
+ if ENV['CI'] == 'true'
11
+ require 'codecov'
12
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
13
+ end
14
+
15
+ require 'webmock/rspec'
16
+ require 'rspec'
17
+ require 'rubirai'
18
+
19
+ def new_bot
20
+ Rubirai::Bot.new('test')
21
+ end
22
+
23
+ WebMock.disable_net_connect!(allow_localhost: false)
24
+
25
+ RSpec.configure do |config|
26
+ config.before :all do
27
+ @auth_key = 'test_auth_key'
28
+ @session_key = 'test_session_key'
29
+ @qq = 1145141919
30
+ end
31
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spec_helper'
4
+
5
+ describe 'errors' do
6
+ it 'stringifies keys' do
7
+ original = {
8
+ a: 3,
9
+ b: 4,
10
+ 'c' => 5,
11
+ D: 6
12
+ }
13
+ expected = {
14
+ 'a' => 3,
15
+ 'b' => 4,
16
+ 'c' => 5,
17
+ 'D' => 6
18
+ }
19
+ expect(original.stringify_keys).to eq(expected)
20
+
21
+ original.stringify_keys!
22
+
23
+ expect(original).to eq(expected)
24
+ end
25
+
26
+ it 'symbolizes keys' do
27
+ original = {
28
+ 'a' => 3,
29
+ 'b' => 4,
30
+ :c => 5,
31
+ 'D' => 6,
32
+ :E => 7
33
+ }
34
+ expected = {
35
+ a: 3,
36
+ b: 4,
37
+ c: 5,
38
+ D: 6,
39
+ E: 7
40
+ }
41
+
42
+ expect(original.symbolize_keys).to eq(expected)
43
+
44
+ original.symbolize_keys!
45
+
46
+ expect(original).to eq(expected)
47
+ end
48
+
49
+ it 'transforms to camel case' do
50
+ expect('SnakE_cAsE'.snake_to_camel).to eq('SnakeCase')
51
+ expect('SnakE_cAsE'.snake_to_camel(lower: true)).to eq('snakeCase')
52
+
53
+ expect('cat'.snake_to_camel).to eq('Cat')
54
+ expect('cAT'.snake_to_camel).to eq('Cat')
55
+ expect('CaT'.snake_to_camel).to eq('Cat')
56
+ expect('cat'.snake_to_camel(lower: true)).to eq('cat')
57
+ expect('cAT'.snake_to_camel(lower: true)).to eq('cat')
58
+ expect('CaT'.snake_to_camel(lower: true)).to eq('cat')
59
+ end
60
+
61
+ it 'checks type' do
62
+ expect { 'cat'.must_be! String }.not_to raise_error
63
+ expect { { a: 3 }.must_be! [Array, Hash] }.not_to raise_error
64
+ expect { 'cat'.must_be! Array }.to raise_error(RuntimeError, 'assert failed: `cat\' must be of type Array')
65
+ expect do
66
+ 'cat'.must_be! [Array, Hash]
67
+ end.to raise_error(RuntimeError, 'assert failed: `cat\' must be of type [Array, Hash]')
68
+ expect { 'cat'.must_be! Array, ArgumentError, 'argument error' }.to raise_error(ArgumentError, 'argument error')
69
+ end
70
+ end