mixin_bot 0.3.7 → 0.3.8

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: ed2fac4118078dffd141f4147ffd7dfea3bc529180cea2a6136622ceafe1b70b
4
- data.tar.gz: cd147227f703ecb292819e137cf6c625563bb339fc7da996a2adbc1db0cc466a
3
+ metadata.gz: 59e234762e65de8e0858a88eaae1c25a8951076345728b20e65d2c15294fc831
4
+ data.tar.gz: 190b8385d4501ff7e603751670f160257f69651679cb4572f4037c4d7c955a67
5
5
  SHA512:
6
- metadata.gz: 982f3caa95756397116ea9c3a559f574dc1881be80325d02961c2f8f62943273726714d54f5bf019d4d599be31f8200637822b5f0229a0857253e62add6f69ce
7
- data.tar.gz: 2f433ee6e767e1c8a3176f55c8806687dbae560cc4376ca9f2b110644fe444976a209e81f9dab475a1ed28068ac351ca17706e3581cea82784636767a50888d0
6
+ metadata.gz: 92d6704e15ce02a56e5f3451c75689b27caf6b7a782597a5535e736926c0c6090cc75d7f40d2190c29ddb32858b910f4b3f0bbcf7f295d474298258ae60c73d6
7
+ data.tar.gz: 19b4de6e5645793750091cb311b38ecedf5e5ba5b6fd0ffaa1916d0be9f2c5fd53a33be658e21e992fdaffe01bbeee83c500b2cb0223ea04fdcd5fda1db87270
data/lib/mixin_bot.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # third-party dependencies
3
4
  require 'English'
4
5
  require 'base64'
5
6
  require 'digest'
@@ -10,6 +11,7 @@ require 'msgpack'
10
11
  require 'open3'
11
12
  require 'openssl'
12
13
  require 'rbnacl'
14
+
13
15
  require_relative './mixin_bot/api'
14
16
  require_relative './mixin_bot/cli'
15
17
  require_relative './mixin_bot/version'
@@ -22,4 +24,10 @@ module MixinBot
22
24
  def self.api
23
25
  @api ||= MixinBot::API.new
24
26
  end
27
+
28
+ class HttpError < StandardError; end
29
+ class RequestError < StandardError; end
30
+ class ResponseError < StandardError; end
31
+ class UnauthorizedError < StandardError; end
32
+ class ForbiddenError < StandardError; end
25
33
  end
data/lib/mixin_bot/api.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative './client'
4
- require_relative './errors'
5
4
  require_relative './api/app'
5
+ require_relative './api/asset'
6
6
  require_relative './api/attachment'
7
7
  require_relative './api/auth'
8
8
  require_relative './api/blaze'
@@ -54,6 +54,7 @@ module MixinBot
54
54
  end
55
55
 
56
56
  include MixinBot::API::App
57
+ include MixinBot::API::Asset
57
58
  include MixinBot::API::Attachment
58
59
  include MixinBot::API::Auth
59
60
  include MixinBot::API::Blaze
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MixinBot
4
+ class API
5
+ module Asset
6
+ # https://developers.mixin.one/api/alpha-mixin-network/read-assets/
7
+ def assets(access_token: nil)
8
+ path = '/assets'
9
+ access_token ||= access_token('GET', path, '')
10
+ authorization = format('Bearer %<access_token>s', access_token: access_token)
11
+ client.get(path, headers: { 'Authorization': authorization })
12
+ end
13
+ alias read_assets assets
14
+
15
+ # https://developers.mixin.one/api/alpha-mixin-network/read-asset/
16
+ def asset(asset_id, access_token: nil)
17
+ path = format('/assets/%<asset_id>s', asset_id: asset_id)
18
+ access_token ||= access_token('GET', path, '')
19
+ authorization = format('Bearer %<access_token>s', access_token: access_token)
20
+ client.get(path, headers: { 'Authorization': authorization })
21
+ end
22
+ alias read_asset asset
23
+
24
+ # https://developers.mixin.one/document/wallet/api/ticker
25
+ def ticker(asset_id, offset, access_token: nil)
26
+ offset = DateTime.rfc3339 offset if offset.is_a? String
27
+ offset = offset.rfc3339 if offset.is_a?(DateTime) || offset.is_a?(Time)
28
+ path = format('/ticker?asset=%<asset_id>s&offset=%<offset>s', asset_id: asset_id, offset: offset)
29
+ access_token ||= access_token('GET', path, '')
30
+ authorization = format('Bearer %<access_token>s', access_token: access_token)
31
+ client.get(path, headers: { 'Authorization': authorization })
32
+ end
33
+ alias read_ticker ticker
34
+ end
35
+ end
36
+ end
@@ -3,17 +3,19 @@
3
3
  module MixinBot
4
4
  class API
5
5
  module Conversation
6
- def read_conversation(conversation_id)
6
+ def conversation(conversation_id)
7
7
  path = format('/conversations/%<conversation_id>s', conversation_id: conversation_id)
8
8
  access_token ||= access_token('GET', path, '')
9
9
  authorization = format('Bearer %<access_token>s', access_token: access_token)
10
10
  client.get(path, headers: { 'Authorization': authorization })
11
11
  end
12
+ alias read_conversation conversation
12
13
 
13
- def read_conversation_by_user_id(user_id)
14
+ def conversation_by_user_id(user_id)
14
15
  conversation_id = unique_conversation_id(user_id)
15
16
  read_conversation(conversation_id)
16
17
  end
18
+ alias read_conversation_by_user_id conversation_by_user_id
17
19
 
18
20
  def create_contact_conversation(user_id)
19
21
  path = '/conversations'
@@ -33,7 +35,7 @@ module MixinBot
33
35
  client.post(path, headers: { 'Authorization': authorization }, json: payload)
34
36
  end
35
37
 
36
- def unique_conversation_id(user_id, opponent_id = nil)
38
+ def unique_uuid(user_id, opponent_id = nil)
37
39
  opponent_id ||= client_id
38
40
  md5 = Digest::MD5.new
39
41
  md5 << [user_id, opponent_id].min
@@ -53,6 +55,7 @@ module MixinBot
53
55
  fifth: hex[20..]
54
56
  )
55
57
  end
58
+ alias unique_conversation_id unique_uuid
56
59
  end
57
60
  end
58
61
  end
@@ -4,12 +4,13 @@ module MixinBot
4
4
  class API
5
5
  module Me
6
6
  # https://developers.mixin.one/api/beta-mixin-message/read-profile/
7
- def read_me(access_token: nil)
7
+ def me(access_token: nil)
8
8
  path = '/me'
9
9
  access_token ||= access_token('GET', path, '')
10
10
  authorization = format('Bearer %<access_token>s', access_token: access_token)
11
11
  client.get(path, headers: { 'Authorization': authorization })
12
12
  end
13
+ alias read_me me
13
14
 
14
15
  # https://developers.mixin.one/api/beta-mixin-message/update-profile/
15
16
  # avatar_base64:
@@ -25,29 +26,14 @@ module MixinBot
25
26
  client.post(path, headers: { 'Authorization': authorization }, json: payload)
26
27
  end
27
28
 
28
- # https://developers.mixin.one/api/alpha-mixin-network/read-assets/
29
- def read_assets(access_token: nil)
30
- path = '/assets'
31
- access_token ||= access_token('GET', path, '')
32
- authorization = format('Bearer %<access_token>s', access_token: access_token)
33
- client.get(path, headers: { 'Authorization': authorization })
34
- end
35
-
36
- # https://developers.mixin.one/api/alpha-mixin-network/read-asset/
37
- def read_asset(asset_id, access_token: nil)
38
- path = format('/assets/%<asset_id>s', asset_id: asset_id)
39
- access_token ||= access_token('GET', path, '')
40
- authorization = format('Bearer %<access_token>s', access_token: access_token)
41
- client.get(path, headers: { 'Authorization': authorization })
42
- end
43
-
44
29
  # https://developers.mixin.one/api/beta-mixin-message/friends/
45
- def read_friends(access_token: nil)
30
+ def friends(access_token: nil)
46
31
  path = '/friends'
47
32
  access_token ||= access_token('GET', path, '')
48
33
  authorization = format('Bearer %<access_token>s', access_token: access_token)
49
34
  client.get(path, headers: { 'Authorization': authorization })
50
35
  end
36
+ alias read_friends friends
51
37
  end
52
38
  end
53
39
  end
@@ -3,7 +3,7 @@
3
3
  module MixinBot
4
4
  class API
5
5
  module Snapshot
6
- def read_network_snapshots(options = {})
6
+ def network_snapshots(options = {})
7
7
  path = format(
8
8
  '/network/snapshots?limit=%<limit>s&offset=%<offset>s&asset=%<asset>s&order=%<order>s',
9
9
  limit: options[:limit],
@@ -16,8 +16,9 @@ module MixinBot
16
16
  authorization = format('Bearer %<access_token>s', access_token: access_token)
17
17
  client.get(path, headers: { 'Authorization': authorization })
18
18
  end
19
+ alias read_network_snapshots network_snapshots
19
20
 
20
- def read_snapshots(options = {})
21
+ def snapshots(options = {})
21
22
  path = format(
22
23
  '/snapshots?limit=%<limit>s&offset=%<offset>s&asset=%<asset>s',
23
24
  limit: options[:limit],
@@ -29,14 +30,16 @@ module MixinBot
29
30
  authorization = format('Bearer %<access_token>s', access_token: access_token)
30
31
  client.get(path, headers: { 'Authorization': authorization })
31
32
  end
33
+ alias read_snapshots snapshots
32
34
 
33
- def read_network_snapshot(snapshot_id, options = {})
35
+ def network_snapshot(snapshot_id, options = {})
34
36
  path = format('/network/snapshots/%<snapshot_id>s', snapshot_id: snapshot_id)
35
37
 
36
38
  access_token = options[:access_token] || access_token('GET', path)
37
39
  authorization = format('Bearer %<access_token>s', access_token: access_token)
38
40
  client.get(path, headers: { 'Authorization': authorization })
39
41
  end
42
+ alias read_network_snapshot network_snapshot
40
43
  end
41
44
  end
42
45
  end
@@ -3,7 +3,10 @@
3
3
  module MixinBot
4
4
  class API
5
5
  module Transfer
6
- def create_transfer(pin, options, access_token: nil)
6
+ TRANSFER_ARGUMENTS = %i[asset_id opponent_id amount].freeze
7
+ def create_transfer(pin, options = {})
8
+ raise ArgumentError, "#{TRANSFER_ARGUMENTS.join(', ')} are needed for create transfer" unless TRANSFER_ARGUMENTS.all? { |param| options.keys.include? param }
9
+
7
10
  asset_id = options[:asset_id]
8
11
  opponent_id = options[:opponent_id]
9
12
  amount = options[:amount]
@@ -21,17 +24,19 @@ module MixinBot
21
24
  memo: memo
22
25
  }
23
26
 
27
+ access_token = options[:access_token]
24
28
  access_token ||= access_token('POST', path, payload.to_json)
25
29
  authorization = format('Bearer %<access_token>s', access_token: access_token)
26
30
  client.post(path, headers: { 'Authorization': authorization }, json: payload)
27
31
  end
28
32
 
29
- def read_transfer(trace_id, access_token: nil)
33
+ def transfer(trace_id, access_token: nil)
30
34
  path = format('/transfers/trace/%<trace_id>s', trace_id: trace_id)
31
35
  access_token ||= access_token('GET', path, '')
32
36
  authorization = format('Bearer %<access_token>s', access_token: access_token)
33
37
  client.get(path, headers: { 'Authorization': authorization })
34
38
  end
39
+ alias read_transfer transfer
35
40
  end
36
41
  end
37
42
  end
@@ -29,17 +29,42 @@ module MixinBot
29
29
  begin
30
30
  response = HTTP.timeout(connect: 5, write: 5, read: 5).request(verb, uri, options)
31
31
  rescue HTTP::Error => e
32
- raise Errors::HttpError, e.message
32
+ raise HttpError, e.message
33
33
  end
34
34
 
35
- raise Errors::APIError.new(nil, response.to_s) unless response.status.success?
35
+ raise RequestError.new(nil, response.to_s) unless response.status.success?
36
36
 
37
37
  parse_response(response) do |parse_as, result|
38
38
  case parse_as
39
39
  when :json
40
- break result if result[:errcode].nil? || result[:errcode].zero?
40
+ if result['error'].nil?
41
+ result.merge! result['data'] if result['data'].is_a? Hash
42
+ break result
43
+ end
41
44
 
42
- raise Errors::APIError.new(result[:errcode], result[:errmsg])
45
+ errmsg = "errcode: #{result['error']['code']}, errmsg: #{result['error']['description']}"
46
+
47
+ # status code description
48
+ # 202 400 The request body can’t be pasred as valid data.
49
+ # 202 401 Unauthorized.
50
+ # 202 403 Forbidden.
51
+ # 202 404 The endpoint is not found.
52
+ # 202 429 Too Many Requests.
53
+ # 202 10006 App update required.
54
+ # 202 20116 The group chat is full.
55
+ # 500 500 Internal Server Error.
56
+ # 500 7000 Blaze server error.
57
+ # 500 7001 The blaze operation timeout.
58
+ case result['error']['code']
59
+ when 401
60
+ raise UnauthorizedError, errmsg
61
+ when 403, 20116
62
+ raise ForbiddenError, errmsg
63
+ when 400, 404, 429, 10006, 20133, 500, 7000, 7001
64
+ raise ResponseError, errmsg
65
+ else
66
+ raise ResponseError, errmsg
67
+ end
43
68
  else
44
69
  result
45
70
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MixinBot
4
- VERSION = '0.3.7'
4
+ VERSION = '0.3.8'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixin_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.7
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - an-lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -136,6 +136,20 @@ dependencies:
136
136
  - - "~>"
137
137
  - !ruby/object:Gem::Version
138
138
  version: '1.0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.13.1
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.13.1
139
153
  - !ruby/object:Gem::Dependency
140
154
  name: rake
141
155
  requirement: !ruby/object:Gem::Requirement
@@ -219,6 +233,7 @@ files:
219
233
  - lib/mixin_bot.rb
220
234
  - lib/mixin_bot/api.rb
221
235
  - lib/mixin_bot/api/app.rb
236
+ - lib/mixin_bot/api/asset.rb
222
237
  - lib/mixin_bot/api/attachment.rb
223
238
  - lib/mixin_bot/api/auth.rb
224
239
  - lib/mixin_bot/api/blaze.rb
@@ -237,7 +252,6 @@ files:
237
252
  - lib/mixin_bot/cli/multisig.rb
238
253
  - lib/mixin_bot/cli/node.rb
239
254
  - lib/mixin_bot/client.rb
240
- - lib/mixin_bot/errors.rb
241
255
  - lib/mixin_bot/version.rb
242
256
  homepage: https://github.com/an-lee/mixin_bot
243
257
  licenses:
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module MixinBot
4
- module Errors
5
- # 通用异常
6
- Error = Class.new(StandardError)
7
-
8
- # HTTP 异常,比如请求超时等
9
- HttpError = Class.new(Error)
10
-
11
- # API 异常,比如返回失败状态码
12
- class APIError < Error
13
- attr_reader :errcode, :errmsg
14
-
15
- def initialize(errcode, errmsg)
16
- @errcode = errcode
17
- @errmsg = errmsg
18
-
19
- super(format('[%<errcode>s]: %<errmsg>s', errcode: @errcode, errmsg: @errmsg))
20
- end
21
- end
22
- end
23
- end