chatwork 0.6.0 → 0.6.1
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 +4 -4
- data/CHANGELOG.md +7 -1
- data/README.md +3 -0
- data/lib/chatwork.rb +1 -0
- data/lib/chatwork/base_client.rb +0 -1
- data/lib/chatwork/chatwork_error.rb +32 -12
- data/lib/chatwork/file.rb +1 -1
- data/lib/chatwork/room.rb +7 -0
- data/lib/chatwork/token.rb +1 -4
- data/lib/chatwork/version.rb +1 -1
- data/spec/lib/chatwork/chatwork_error_spec.rb +45 -6
- data/spec/lib/chatwork/token_spec.rb +42 -18
- data/spec/lib/support/utils/raml_parser_spec.rb +0 -17
- data/spec/support/contexts/api_context.rb +1 -1
- data/spec/support/utils/raml_parser.rb +2 -8
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59e4ff9379d4a6103a888096f95051bde6b365ad9e7a165f379563f95a14042d
|
4
|
+
data.tar.gz: 90709b0cd1ea756266fb6072cd656455bc929ed34870dcf3d889781cb29f740a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee3317f8e4afbf7d4c8a6384562202e8d8ce68226e7ccab6e6fc806446b0548df0a1192b5b712ed879a16de4d7bc2aca5dda15f87a596f4f479c62655787a945
|
7
|
+
data.tar.gz: 438dc9cec86531f6be5abb10f824d3a91c9bb0a22078027fb999e2ad19745fe6f67e4b039d14fa43680f4aa39ada8f9aacc71ebb701b980713fe9da51f3a066d
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
# Change Log
|
2
2
|
## Unreleased
|
3
|
-
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.6.
|
3
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.6.1...master)
|
4
|
+
|
5
|
+
## v0.6.1
|
6
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.6.0...v0.6.1)
|
7
|
+
|
8
|
+
* Fixed: ArgumentError when ChatWork::Token.refresh_access_token
|
9
|
+
* https://github.com/asonas/chatwork-ruby/pull/39
|
4
10
|
|
5
11
|
## v0.6.0
|
6
12
|
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.5.0...v0.6.0)
|
data/README.md
CHANGED
@@ -92,6 +92,9 @@ ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
|
|
92
92
|
$ CHATWORK_CLIENT_ID=xxx CHATWORK_CLIENT_SECRET=xxx REFRESH_TOKEN=xxx ruby refresh_access_token.rb
|
93
93
|
```
|
94
94
|
|
95
|
+
## Reference
|
96
|
+
http://www.rubydoc.info/gems/chatwork
|
97
|
+
|
95
98
|
## Development
|
96
99
|
```bash
|
97
100
|
cp .env.example .env
|
data/lib/chatwork.rb
CHANGED
data/lib/chatwork/base_client.rb
CHANGED
@@ -1,15 +1,22 @@
|
|
1
1
|
module ChatWork
|
2
2
|
class ChatWorkError < StandardError
|
3
3
|
def self.from_response(status, body, headers)
|
4
|
-
|
5
|
-
return
|
4
|
+
if headers.has_key?("WWW-Authenticate")
|
5
|
+
return AuthenticateError.from_www_authenticate(
|
6
|
+
www_authenticate: headers["WWW-Authenticate"],
|
7
|
+
status: status,
|
8
|
+
error_response: body["errors"],
|
9
|
+
)
|
6
10
|
end
|
7
11
|
|
8
|
-
|
9
|
-
|
12
|
+
return APIError.new(status, body["errors"]) if body["errors"]
|
13
|
+
|
14
|
+
if body["error"]
|
15
|
+
message = [body["error"], body["error_description"]].compact.join(" ")
|
16
|
+
return AuthenticateError.new(message, status, body, body["error"], body["error_description"])
|
10
17
|
end
|
11
18
|
|
12
|
-
|
19
|
+
APIConnectionError.new("Invalid response #{body.to_hash} (status: #{status})")
|
13
20
|
end
|
14
21
|
|
15
22
|
attr_reader :status
|
@@ -29,7 +36,7 @@ module ChatWork
|
|
29
36
|
|
30
37
|
attr_reader :original_error
|
31
38
|
|
32
|
-
def initialize(message, original_error)
|
39
|
+
def initialize(message, original_error = nil)
|
33
40
|
@original_error = original_error
|
34
41
|
super(message)
|
35
42
|
end
|
@@ -47,14 +54,27 @@ module ChatWork
|
|
47
54
|
class AuthenticateError < ChatWorkError
|
48
55
|
attr_reader :error, :error_description
|
49
56
|
|
50
|
-
def initialize(message, status, error_response)
|
51
|
-
|
52
|
-
@
|
53
|
-
|
54
|
-
message =~ /error_description="([^\"]+)"/
|
55
|
-
@error_description = Regexp.last_match(1)
|
57
|
+
def initialize(message, status, error_response, error, error_description)
|
58
|
+
@error = error
|
59
|
+
@error_description = error_description
|
56
60
|
|
57
61
|
super(message, status, error_response)
|
58
62
|
end
|
63
|
+
|
64
|
+
def self.from_www_authenticate(www_authenticate:, status:, error_response:)
|
65
|
+
www_authenticate =~ /error="([^\"]+)"/
|
66
|
+
error = Regexp.last_match(1)
|
67
|
+
|
68
|
+
www_authenticate =~ /error_description="([^\"]+)"/
|
69
|
+
error_description = Regexp.last_match(1)
|
70
|
+
|
71
|
+
AuthenticateError.new(
|
72
|
+
www_authenticate,
|
73
|
+
status,
|
74
|
+
error_response,
|
75
|
+
error,
|
76
|
+
error_description,
|
77
|
+
)
|
78
|
+
end
|
59
79
|
end
|
60
80
|
end
|
data/lib/chatwork/file.rb
CHANGED
@@ -41,7 +41,7 @@ module ChatWork
|
|
41
41
|
# @param create_download_url [Boolean] whether or not to create a download link.
|
42
42
|
# If set to true, download like will be created for 30 seconds
|
43
43
|
#
|
44
|
-
# @return [
|
44
|
+
# @return [Hashie::Mash]
|
45
45
|
#
|
46
46
|
# @example response format
|
47
47
|
# {
|
data/lib/chatwork/room.rb
CHANGED
@@ -108,6 +108,13 @@ module ChatWork
|
|
108
108
|
# @param icon_preset [String] Type of the group chat icon (group, check, document, meeting, event, project, business,
|
109
109
|
# study, security, star, idea, heart, magcup, beer, music, sports, travel)
|
110
110
|
# @param name [String] Title of the group chat.
|
111
|
+
#
|
112
|
+
# @return [Hashie::Mash]
|
113
|
+
#
|
114
|
+
# @example response format
|
115
|
+
# {
|
116
|
+
# "room_id": 1234
|
117
|
+
# }
|
111
118
|
def self.update(room_id:, description: nil, icon_preset: nil, name: nil)
|
112
119
|
_put("/rooms/#{room_id}", description: description, icon_preset: icon_preset, name: name)
|
113
120
|
end
|
data/lib/chatwork/token.rb
CHANGED
@@ -14,7 +14,6 @@ module ChatWork
|
|
14
14
|
# "refresh_token" => "refresh_token",
|
15
15
|
# "scope" => "users.all:read rooms.all:read_write contacts.all:read_write",
|
16
16
|
# }
|
17
|
-
# ["access_token", "token_type", "expires_in", "refresh_token", "scope"]
|
18
17
|
def self.refresh_access_token(refresh_token, scope = [])
|
19
18
|
params = {
|
20
19
|
grant_type: "refresh_token",
|
@@ -22,9 +21,7 @@ module ChatWork
|
|
22
21
|
}
|
23
22
|
params[:scope] = scope.join(" ") unless scope.empty?
|
24
23
|
|
25
|
-
|
26
|
-
raise response if response.is_a?(Exception)
|
27
|
-
response
|
24
|
+
ChatWork.oauth_client.post("/token", params)
|
28
25
|
end
|
29
26
|
end
|
30
27
|
end
|
data/lib/chatwork/version.rb
CHANGED
@@ -1,16 +1,14 @@
|
|
1
1
|
describe ChatWork::ChatWorkError do
|
2
2
|
describe ".from_response" do
|
3
|
-
subject { ChatWork::ChatWorkError.from_response(status, body, headers) }
|
3
|
+
subject { ChatWork::ChatWorkError.from_response(status, Hashie::Mash.new(body), headers) }
|
4
4
|
|
5
5
|
context "with WWW-Authenticate header" do
|
6
6
|
let(:status) { 401 }
|
7
7
|
|
8
8
|
let(:body) do
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
JSON
|
13
|
-
Hashie::Mash.new(JSON.parse(json))
|
9
|
+
{
|
10
|
+
errors: ["Invalid API Token"],
|
11
|
+
}
|
14
12
|
end
|
15
13
|
|
16
14
|
let(:headers) do
|
@@ -24,5 +22,46 @@ describe ChatWork::ChatWorkError do
|
|
24
22
|
its(:error_description) { should eq "The access token expired" }
|
25
23
|
its(:error_response) { should eq ["Invalid API Token"] }
|
26
24
|
end
|
25
|
+
|
26
|
+
context "with error" do
|
27
|
+
let(:status) { 401 }
|
28
|
+
|
29
|
+
let(:body) do
|
30
|
+
{
|
31
|
+
error: "invalid_client",
|
32
|
+
error_description: "The client ID `client_id` is unknown.",
|
33
|
+
error_uri: nil,
|
34
|
+
}.stringify_keys
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:headers) { {} }
|
38
|
+
|
39
|
+
it { should be_an_instance_of ChatWork::AuthenticateError }
|
40
|
+
its(:message) { should eq "invalid_client The client ID `client_id` is unknown." }
|
41
|
+
its(:error) { should eq "invalid_client" }
|
42
|
+
its(:error_description) { should eq "The client ID `client_id` is unknown." }
|
43
|
+
its(:error_response) { should eq body }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe ChatWork::AuthenticateError do
|
48
|
+
describe ".from_www_authenticate" do
|
49
|
+
subject do
|
50
|
+
ChatWork::AuthenticateError.from_www_authenticate(
|
51
|
+
www_authenticate: www_authenticate,
|
52
|
+
status: status,
|
53
|
+
error_response: error_response,
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
let(:www_authenticate) { 'Bearer error="invalid_token", error_description="The access token expired"' }
|
58
|
+
let(:status) { 401 }
|
59
|
+
let(:error_response) { ["Invalid API Token"] }
|
60
|
+
|
61
|
+
it { should be_an_instance_of ChatWork::AuthenticateError }
|
62
|
+
its(:error) { should eq "invalid_token" }
|
63
|
+
its(:error_description) { should eq "The access token expired" }
|
64
|
+
its(:error_response) { should eq ["Invalid API Token"] }
|
65
|
+
end
|
27
66
|
end
|
28
67
|
end
|
@@ -5,11 +5,6 @@ describe ChatWork::Token do
|
|
5
5
|
before do
|
6
6
|
allow(ChatWork).to receive(:client_id) { client_id }
|
7
7
|
allow(ChatWork).to receive(:client_secret) { client_secret }
|
8
|
-
|
9
|
-
stub_request(:post, "https://oauth.chatwork.com/token").
|
10
|
-
with(body: { "grant_type" => "refresh_token", "refresh_token" => refresh_token },
|
11
|
-
headers: { "Authorization" => "Basic #{signature}", "Content-Type" => "application/x-www-form-urlencoded" }).
|
12
|
-
to_return(status: 200, body: token.to_json, headers: { "Content-Type" => "application/json" })
|
13
8
|
end
|
14
9
|
|
15
10
|
let(:client_id) { "client_id" }
|
@@ -17,20 +12,49 @@ describe ChatWork::Token do
|
|
17
12
|
let(:refresh_token) { "refresh_token" }
|
18
13
|
let(:signature) { "Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=" }
|
19
14
|
let(:scope) { [] }
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
15
|
+
|
16
|
+
context "when successful" do
|
17
|
+
before do
|
18
|
+
stub_request(:post, "https://oauth.chatwork.com/token").
|
19
|
+
with(body: { "grant_type" => "refresh_token", "refresh_token" => refresh_token },
|
20
|
+
headers: { "Authorization" => "Basic #{signature}", "Content-Type" => "application/x-www-form-urlencoded" }).
|
21
|
+
to_return(status: 200, body: token.to_json, headers: { "Content-Type" => "application/json" })
|
22
|
+
end
|
23
|
+
|
24
|
+
let(:token) do
|
25
|
+
{
|
26
|
+
access_token: "new_access_token",
|
27
|
+
token_type: "Bearer",
|
28
|
+
expires_in: "1800",
|
29
|
+
refresh_token: refresh_token,
|
30
|
+
scope: "users.all:read rooms.all:read_write contacts.all:read_write",
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
its(["access_token"]) { should eq "new_access_token" }
|
35
|
+
its(["token_type"]) { should eq "Bearer" }
|
36
|
+
its(["expires_in"]) { should eq "1800" }
|
37
|
+
its(["refresh_token"]) { should eq refresh_token }
|
38
|
+
its(["scope"]) { should eq "users.all:read rooms.all:read_write contacts.all:read_write" }
|
28
39
|
end
|
29
40
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
41
|
+
context "when failure" do
|
42
|
+
before do
|
43
|
+
stub_request(:post, "https://oauth.chatwork.com/token").
|
44
|
+
with(body: { "grant_type" => "refresh_token", "refresh_token" => refresh_token },
|
45
|
+
headers: { "Authorization" => "Basic #{signature}", "Content-Type" => "application/x-www-form-urlencoded" }).
|
46
|
+
to_return(status: 401, body: body.to_json, headers: { "Content-Type" => "application/json" })
|
47
|
+
end
|
48
|
+
|
49
|
+
let(:body) do
|
50
|
+
{
|
51
|
+
error: "invalid_client",
|
52
|
+
error_description: "The client ID `client_id` is unknown.",
|
53
|
+
error_uri: nil,
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
it { expect { subject }.to raise_error ChatWork::AuthenticateError }
|
58
|
+
end
|
35
59
|
end
|
36
60
|
end
|
@@ -76,21 +76,4 @@ RSpec.describe RamlParser do
|
|
76
76
|
its(["description"]) { should eq "group chat description" }
|
77
77
|
its(["icon_preset"]) { should eq "meeting" }
|
78
78
|
end
|
79
|
-
|
80
|
-
describe ".raml" do
|
81
|
-
subject { RamlParser.raml }
|
82
|
-
|
83
|
-
before do
|
84
|
-
RamlParser.instance_variable_set(:@raml, nil)
|
85
|
-
end
|
86
|
-
|
87
|
-
after do
|
88
|
-
RamlParser.instance_variable_set(:@raml, nil)
|
89
|
-
end
|
90
|
-
|
91
|
-
it "3 digit number can be read as a string" do
|
92
|
-
comma_separated_integer_list = subject["traits"][0]["room_members"]["queryParameters"]["members_admin_ids"]["example"]
|
93
|
-
expect(comma_separated_integer_list).to eq "123,542,1001"
|
94
|
-
end
|
95
|
-
end
|
96
79
|
end
|
@@ -32,7 +32,7 @@ RSpec.shared_context :api_context, type: :api do
|
|
32
32
|
query_string = "?" + query_example.to_query
|
33
33
|
when :post, :put
|
34
34
|
request_options[:headers]["Content-Type"] = "application/x-www-form-urlencoded"
|
35
|
-
request_options[:body] = query_example
|
35
|
+
request_options[:body] = query_example.transform_values(&:to_s)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -67,15 +67,9 @@ module RamlParser
|
|
67
67
|
private_class_method :find_node
|
68
68
|
|
69
69
|
def self.raml
|
70
|
-
|
71
|
-
|
72
|
-
yaml_data = schema_file.read
|
73
|
-
|
74
|
-
# e.g. example: 123,542,1001 -> example: '123,542,1001'
|
75
|
-
yaml_data.gsub!(/example: ([0-9,]+)/) { "example: '#{Regexp.last_match(1)}'" }
|
76
|
-
|
77
|
-
@raml = YAML.safe_load(yaml_data)
|
70
|
+
@raml ||= YAML.load_file(schema_file)
|
78
71
|
end
|
72
|
+
private_class_method :raml
|
79
73
|
|
80
74
|
def self.parse_response(response_json)
|
81
75
|
JSON.parse(response_json)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatwork
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- asonas
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2018-01-
|
12
|
+
date: 2018-01-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
@@ -331,7 +331,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
331
331
|
version: '0'
|
332
332
|
requirements: []
|
333
333
|
rubyforge_project:
|
334
|
-
rubygems_version: 2.7.
|
334
|
+
rubygems_version: 2.7.3
|
335
335
|
signing_key:
|
336
336
|
specification_version: 4
|
337
337
|
summary: Ruby bindings of ChatWork API
|