chatwork 0.4.1 → 0.5.0
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/.rspec +1 -0
- data/CHANGELOG.md +98 -0
- data/README.md +50 -0
- data/chatwork.gemspec +2 -1
- data/lib/chatwork.rb +50 -1
- data/lib/chatwork/base_client.rb +48 -0
- data/lib/chatwork/chatwork_error.rb +19 -1
- data/lib/chatwork/client.rb +7 -39
- data/lib/chatwork/oauth_client.rb +10 -0
- data/lib/chatwork/operations.rb +4 -4
- data/lib/chatwork/token.rb +30 -0
- data/lib/chatwork/version.rb +1 -1
- data/spec/lib/chatwork/chatwork_error_spec.rb +26 -0
- data/spec/lib/chatwork/client_spec.rb +34 -0
- data/spec/lib/chatwork/member_spec.rb +15 -0
- data/spec/lib/chatwork/oauth_client_spec.rb +16 -0
- data/spec/lib/chatwork/token_spec.rb +17 -0
- data/spec/lib/chatwork_spec.rb +19 -3
- data/spec/shared_oauth_stubs.rb +49 -0
- data/spec/shared_stubs.rb +39 -0
- data/spec/spec_helper.rb +6 -0
- metadata +40 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be16993941272b82492aa6671c2cce25e9ea8a4d
|
4
|
+
data.tar.gz: 63c1282e35926da56a30e5a1cca103633414fd02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33cdf63bf2505a9a02da3958f86ed11851e01838a75da820cb6b67ba3db401686aeb746a05d68f25b1be7a33b8274d4b6b555677b2f66772cbbf84e2b5e96c37
|
7
|
+
data.tar.gz: dce5ad9a60dcb6f843761055fd122a8414472d97f343198466e3b63c0238514a5455126e4bdb6d75af95fa02d12c3629c7aacec41fb94fa999193800985a81af
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
## [v0.4.1](https://github.com/asonas/chatwork-ruby/tree/v0.4.1) (2017-02-17)
|
4
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.4.0...v0.4.1)
|
5
|
+
|
6
|
+
**Merged pull requests:**
|
7
|
+
|
8
|
+
- Relax faraday dependency for faraday v0.11.0 [\#18](https://github.com/asonas/chatwork-ruby/pull/18) ([sue445](https://github.com/sue445))
|
9
|
+
|
10
|
+
## [v0.4.0](https://github.com/asonas/chatwork-ruby/tree/v0.4.0) (2017-01-31)
|
11
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.3.1...v0.4.0)
|
12
|
+
|
13
|
+
**Merged pull requests:**
|
14
|
+
|
15
|
+
- api v2 release [\#14](https://github.com/asonas/chatwork-ruby/pull/14) ([hatappi](https://github.com/hatappi))
|
16
|
+
- 🏠 Set the homepage URL [\#13](https://github.com/asonas/chatwork-ruby/pull/13) ([amatsuda](https://github.com/amatsuda))
|
17
|
+
|
18
|
+
## [v0.3.1](https://github.com/asonas/chatwork-ruby/tree/v0.3.1) (2016-07-20)
|
19
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.3.0...v0.3.1)
|
20
|
+
|
21
|
+
**Closed issues:**
|
22
|
+
|
23
|
+
- api\_keyはデフォルトで環境変数を使ってほしい [\#11](https://github.com/asonas/chatwork-ruby/issues/11)
|
24
|
+
|
25
|
+
**Merged pull requests:**
|
26
|
+
|
27
|
+
- Fixed a typo and bug [\#9](https://github.com/asonas/chatwork-ruby/pull/9) ([henteko](https://github.com/henteko))
|
28
|
+
|
29
|
+
## [v0.3.0](https://github.com/asonas/chatwork-ruby/tree/v0.3.0) (2016-06-20)
|
30
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.2.0...v0.3.0)
|
31
|
+
|
32
|
+
**Merged pull requests:**
|
33
|
+
|
34
|
+
- api\_keyのデフォルトを環境変数にする [\#12](https://github.com/asonas/chatwork-ruby/pull/12) ([hatappi](https://github.com/hatappi))
|
35
|
+
|
36
|
+
## [v0.2.0](https://github.com/asonas/chatwork-ruby/tree/v0.2.0) (2015-10-21)
|
37
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.1.2...v0.2.0)
|
38
|
+
|
39
|
+
**Merged pull requests:**
|
40
|
+
|
41
|
+
- Implement Contacts \#get method [\#10](https://github.com/asonas/chatwork-ruby/pull/10) ([cy-ken-goto](https://github.com/cy-ken-goto))
|
42
|
+
|
43
|
+
## [v0.1.2](https://github.com/asonas/chatwork-ruby/tree/v0.1.2) (2015-02-24)
|
44
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.1.1...v0.1.2)
|
45
|
+
|
46
|
+
**Merged pull requests:**
|
47
|
+
|
48
|
+
- Implement Member\#get method. [\#8](https://github.com/asonas/chatwork-ruby/pull/8) ([tamano](https://github.com/tamano))
|
49
|
+
|
50
|
+
## [v0.1.1](https://github.com/asonas/chatwork-ruby/tree/v0.1.1) (2015-01-29)
|
51
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.1.0...v0.1.1)
|
52
|
+
|
53
|
+
**Merged pull requests:**
|
54
|
+
|
55
|
+
- Define Room\#get method. [\#7](https://github.com/asonas/chatwork-ruby/pull/7) ([tokada](https://github.com/tokada))
|
56
|
+
|
57
|
+
## [v0.1.0](https://github.com/asonas/chatwork-ruby/tree/v0.1.0) (2015-01-26)
|
58
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.0.7...v0.1.0)
|
59
|
+
|
60
|
+
**Merged pull requests:**
|
61
|
+
|
62
|
+
- Implements tasks [\#6](https://github.com/asonas/chatwork-ruby/pull/6) ([asonas](https://github.com/asonas))
|
63
|
+
|
64
|
+
## [v0.0.7](https://github.com/asonas/chatwork-ruby/tree/v0.0.7) (2015-01-20)
|
65
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.0.6...v0.0.7)
|
66
|
+
|
67
|
+
**Merged pull requests:**
|
68
|
+
|
69
|
+
- Add get messages API. [\#5](https://github.com/asonas/chatwork-ruby/pull/5) ([tamano](https://github.com/tamano))
|
70
|
+
|
71
|
+
## [v0.0.6](https://github.com/asonas/chatwork-ruby/tree/v0.0.6) (2014-12-19)
|
72
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.0.5...v0.0.6)
|
73
|
+
|
74
|
+
**Merged pull requests:**
|
75
|
+
|
76
|
+
- Add Tasks [\#4](https://github.com/asonas/chatwork-ruby/pull/4) ([tamano](https://github.com/tamano))
|
77
|
+
|
78
|
+
## [v0.0.5](https://github.com/asonas/chatwork-ruby/tree/v0.0.5) (2014-04-20)
|
79
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.0.3...v0.0.5)
|
80
|
+
|
81
|
+
**Merged pull requests:**
|
82
|
+
|
83
|
+
- faraday gem update [\#2](https://github.com/asonas/chatwork-ruby/pull/2) ([yoneapp](https://github.com/yoneapp))
|
84
|
+
|
85
|
+
## [v0.0.3](https://github.com/asonas/chatwork-ruby/tree/v0.0.3) (2013-12-30)
|
86
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.0.2...v0.0.3)
|
87
|
+
|
88
|
+
## [v0.0.2](https://github.com/asonas/chatwork-ruby/tree/v0.0.2) (2013-11-29)
|
89
|
+
[Full Changelog](https://github.com/asonas/chatwork-ruby/compare/v0.0.1...v0.0.2)
|
90
|
+
|
91
|
+
**Merged pull requests:**
|
92
|
+
|
93
|
+
- Fix a typo in ChatWork module [\#1](https://github.com/asonas/chatwork-ruby/pull/1) ([eitoball](https://github.com/eitoball))
|
94
|
+
|
95
|
+
## [v0.0.1](https://github.com/asonas/chatwork-ruby/tree/v0.0.1) (2013-11-28)
|
96
|
+
|
97
|
+
|
98
|
+
\* *This Change Log was automatically generated by [github_changelog_generator](https://github.com/skywinder/Github-Changelog-Generator)*
|
data/README.md
CHANGED
@@ -18,6 +18,8 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
|
21
|
+
### Case 1. with API Key
|
22
|
+
|
21
23
|
``` ruby
|
22
24
|
require "chatwork"
|
23
25
|
|
@@ -36,6 +38,54 @@ ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
|
|
36
38
|
$ CHATWORK_API_TOKEN=xxx ruby send_message.rb
|
37
39
|
```
|
38
40
|
|
41
|
+
### Case 2. with OAuth access token
|
42
|
+
``` ruby
|
43
|
+
require "chatwork"
|
44
|
+
|
45
|
+
# Create message
|
46
|
+
ChatWork.access_token = "XXX"
|
47
|
+
ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
|
48
|
+
```
|
49
|
+
|
50
|
+
or
|
51
|
+
|
52
|
+
``` sh
|
53
|
+
$ cat send_message_with_access_token.rb
|
54
|
+
require "chatwork"
|
55
|
+
|
56
|
+
ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
|
57
|
+
$ CHATWORK_ACCESS_TOKEN=xxx ruby send_message_with_access_token.rb
|
58
|
+
```
|
59
|
+
|
60
|
+
### Case 3. Refresh access token with refresh token
|
61
|
+
``` ruby
|
62
|
+
require "chatwork"
|
63
|
+
|
64
|
+
ChatWork.client_id = "XXX"
|
65
|
+
ChatWork.client_secret = "XXX"
|
66
|
+
refresh_token = "XXX"
|
67
|
+
token = ChatWork::Token.refresh_access_token(refresh_token)
|
68
|
+
new_access_token = token["access_token"]
|
69
|
+
|
70
|
+
# Create message
|
71
|
+
ChatWork.access_token = new_access_token
|
72
|
+
ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
|
73
|
+
```
|
74
|
+
|
75
|
+
or
|
76
|
+
|
77
|
+
``` sh
|
78
|
+
$ cat refresh_access_token.rb
|
79
|
+
require "chatwork"
|
80
|
+
|
81
|
+
token = ChatWork::Token.refresh_access_token(ENV["REFRESH_TOKEN"])
|
82
|
+
new_access_token = token["access_token"]
|
83
|
+
|
84
|
+
# Create message
|
85
|
+
ChatWork.access_token = new_access_token
|
86
|
+
ChatWork::Message.create(room_id: 1234, body: "Hello, ChatWork!")
|
87
|
+
$ CHATWORK_CLIENT_ID=xxx CHATWORK_CLIENT_SECRET=xxx REFRESH_TOKEN=xxx ruby refresh_access_token.rb
|
88
|
+
```
|
39
89
|
|
40
90
|
## Contributing
|
41
91
|
|
data/chatwork.gemspec
CHANGED
@@ -22,5 +22,6 @@ Gem::Specification.new do |spec|
|
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
24
24
|
spec.add_development_dependency "rake"
|
25
|
-
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "rspec-its"
|
26
27
|
end
|
data/lib/chatwork.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
require "chatwork/version"
|
2
2
|
|
3
3
|
module ChatWork
|
4
|
+
autoload(:BaseClient, 'chatwork/base_client')
|
4
5
|
autoload(:Client, 'chatwork/client')
|
6
|
+
autoload(:OAuthClient, 'chatwork/oauth_client')
|
7
|
+
autoload(:Token, 'chatwork/token')
|
5
8
|
autoload(:Operations, 'chatwork/operations')
|
6
9
|
autoload(:ChatWorkError, 'chatwork/chatwork_error')
|
7
10
|
autoload(:APIConnectionError, 'chatwork/chatwork_error')
|
@@ -16,12 +19,20 @@ module ChatWork
|
|
16
19
|
autoload(:Contacts, 'chatwork/contacts')
|
17
20
|
|
18
21
|
@api_base = 'https://api.chatwork.com/'
|
22
|
+
@oauth_api_base = 'https://oauth.chatwork.com/'
|
19
23
|
@api_version = '/v2'
|
20
24
|
@api_key = nil
|
25
|
+
@access_token = nil
|
26
|
+
@client_id = nil
|
27
|
+
@client_secret = nil
|
21
28
|
|
22
29
|
class << self
|
23
30
|
def client
|
24
|
-
@client ||= Client.new(api_key, api_base, api_version)
|
31
|
+
@client ||= Client.new(api_key, access_token, api_base, api_version)
|
32
|
+
end
|
33
|
+
|
34
|
+
def oauth_client
|
35
|
+
@oauth_client ||= OAuthClient.new(client_id, client_secret, oauth_api_base)
|
25
36
|
end
|
26
37
|
|
27
38
|
def api_base=(new_value)
|
@@ -29,19 +40,57 @@ module ChatWork
|
|
29
40
|
@client = nil
|
30
41
|
end
|
31
42
|
|
43
|
+
def oauth_api_base=(new_value)
|
44
|
+
@oauth_api_base = new_value
|
45
|
+
@oauth_client = nil
|
46
|
+
end
|
47
|
+
|
32
48
|
def api_key=(new_value)
|
33
49
|
@api_key = new_value
|
50
|
+
@access_token = nil
|
51
|
+
@client = nil
|
52
|
+
end
|
53
|
+
|
54
|
+
def access_token=(new_value)
|
55
|
+
@api_key = nil
|
56
|
+
@access_token = new_value
|
34
57
|
@client = nil
|
35
58
|
end
|
36
59
|
|
60
|
+
def client_id=(new_value)
|
61
|
+
@client_id = new_value
|
62
|
+
@oauth_client = nil
|
63
|
+
end
|
64
|
+
|
65
|
+
def client_secret=(new_value)
|
66
|
+
@client_secret = new_value
|
67
|
+
@oauth_client = nil
|
68
|
+
end
|
69
|
+
|
37
70
|
def api_base
|
38
71
|
@api_base
|
39
72
|
end
|
40
73
|
|
74
|
+
def oauth_api_base
|
75
|
+
@oauth_api_base
|
76
|
+
end
|
77
|
+
|
41
78
|
def api_key
|
42
79
|
@api_key || ENV['CHATWORK_API_TOKEN']
|
43
80
|
end
|
44
81
|
|
82
|
+
def access_token
|
83
|
+
@access_token || ENV['CHATWORK_ACCESS_TOKEN']
|
84
|
+
end
|
85
|
+
|
86
|
+
def client_id
|
87
|
+
@client_id || ENV['CHATWORK_CLIENT_ID']
|
88
|
+
end
|
89
|
+
|
90
|
+
def client_secret
|
91
|
+
@client_secret || ENV['CHATWORK_CLIENT_SECRET']
|
92
|
+
end
|
93
|
+
|
45
94
|
def api_version
|
46
95
|
@api_version
|
47
96
|
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module ChatWork
|
5
|
+
class BaseClient
|
6
|
+
def initialize(api_base, api_version, header)
|
7
|
+
default_header = {
|
8
|
+
'User-Agent' => "ChatWork#{api_version} RubyBinding/#{ChatWork::VERSION}"
|
9
|
+
}
|
10
|
+
|
11
|
+
default_header.merge!(header)
|
12
|
+
|
13
|
+
@conn = Faraday.new("#{api_base}#{api_version}", headers: default_header) do |builder|
|
14
|
+
builder.request :url_encoded
|
15
|
+
builder.adapter Faraday.default_adapter
|
16
|
+
end
|
17
|
+
@api_version = api_version
|
18
|
+
end
|
19
|
+
|
20
|
+
def handle_response(response)
|
21
|
+
case response.status
|
22
|
+
when 204
|
23
|
+
ChatWork::ChatWorkError.from_response(response.status, response.body, response.headers)
|
24
|
+
when 200..299
|
25
|
+
begin
|
26
|
+
JSON.parse(response.body)
|
27
|
+
rescue JSON::ParserError => e
|
28
|
+
raise ChatWork::APIConnectionError.new("Response JSON is broken. #{e.message}: #{response.body}", e)
|
29
|
+
end
|
30
|
+
else
|
31
|
+
ChatWork::ChatWorkError.from_response(response.status, response.body, response.headers)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Faraday::Connection::METHODS.each do |method|
|
36
|
+
define_method(method) do |url, *args, &block|
|
37
|
+
begin
|
38
|
+
response = @conn.__send__(method, @api_version + url, *args)
|
39
|
+
rescue Faraday::Error::ClientError => e
|
40
|
+
raise ChatWork::APIConnectionError.faraday_error(e)
|
41
|
+
end
|
42
|
+
payload = handle_response(response)
|
43
|
+
block.call(payload, response.headers) if block
|
44
|
+
payload
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -2,7 +2,7 @@ require 'json'
|
|
2
2
|
module ChatWork
|
3
3
|
class ChatWorkError < StandardError
|
4
4
|
|
5
|
-
def self.from_response(status, body)
|
5
|
+
def self.from_response(status, body, headers)
|
6
6
|
# HTTP status 204 don't have body.
|
7
7
|
return APIError.new(status, "") if status == 204
|
8
8
|
|
@@ -16,6 +16,10 @@ module ChatWork
|
|
16
16
|
return APIConnectionError.new("Invalid response #{body}")
|
17
17
|
end
|
18
18
|
|
19
|
+
if headers.has_key?('WWW-Authenticate')
|
20
|
+
return AuthenticateError.new(headers['WWW-Authenticate'], status, hash['errors'])
|
21
|
+
end
|
22
|
+
|
19
23
|
APIError.new(status, hash['errors'])
|
20
24
|
end
|
21
25
|
|
@@ -49,4 +53,18 @@ module ChatWork
|
|
49
53
|
super(error_response[0], status, error_response)
|
50
54
|
end
|
51
55
|
end
|
56
|
+
|
57
|
+
class AuthenticateError < ChatWorkError
|
58
|
+
attr_reader :error, :error_description
|
59
|
+
|
60
|
+
def initialize(message, status, error_response)
|
61
|
+
message =~ /error="([^\"]+)"/
|
62
|
+
@error = Regexp.last_match(1)
|
63
|
+
|
64
|
+
message =~ /error_description="([^\"]+)"/
|
65
|
+
@error_description = Regexp.last_match(1)
|
66
|
+
|
67
|
+
super(message, status, error_response)
|
68
|
+
end
|
69
|
+
end
|
52
70
|
end
|
data/lib/chatwork/client.rb
CHANGED
@@ -1,44 +1,12 @@
|
|
1
|
-
require 'faraday'
|
2
|
-
require 'json'
|
3
|
-
|
4
1
|
module ChatWork
|
5
|
-
class Client
|
6
|
-
def initialize(api_key, api_base, api_version)
|
7
|
-
|
8
|
-
'X-ChatWorkToken' => api_key
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
@conn = Faraday.new("#{api_base}#{api_version}", headers: default_header) do |builder|
|
13
|
-
builder.request :url_encoded
|
14
|
-
builder.adapter Faraday.default_adapter
|
15
|
-
end
|
16
|
-
@api_version = api_version
|
17
|
-
end
|
18
|
-
|
19
|
-
def handle_response(response)
|
20
|
-
case response.status
|
21
|
-
when 204
|
22
|
-
ChatWork::ChatWorkError.from_response(response.status, response.body)
|
23
|
-
when 200..299
|
24
|
-
begin
|
25
|
-
JSON.parse(response.body)
|
26
|
-
rescue JSON::ParserError => e
|
27
|
-
raise ChatWork::APIConnectionError.new("Response JSON is broken. #{e.message}: #{response.body}", e)
|
28
|
-
end
|
2
|
+
class Client < BaseClient
|
3
|
+
def initialize(api_key, access_token, api_base, api_version)
|
4
|
+
if api_key
|
5
|
+
super(api_base, api_version, {'X-ChatWorkToken' => api_key})
|
6
|
+
elsif access_token
|
7
|
+
super(api_base, api_version, {'Authorization' => "Bearer #{access_token}"})
|
29
8
|
else
|
30
|
-
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
Faraday::Connection::METHODS.each do |method|
|
35
|
-
define_method(method) do |url, *args|
|
36
|
-
begin
|
37
|
-
response = @conn.__send__(method, @api_version + url, *args)
|
38
|
-
rescue Faraday::Error::ClientError => e
|
39
|
-
raise ChatWork::APIConnectionError.faraday_error(e)
|
40
|
-
end
|
41
|
-
handle_response(response)
|
9
|
+
raise "Either api_key or access_token is required"
|
42
10
|
end
|
43
11
|
end
|
44
12
|
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module ChatWork
|
4
|
+
class OAuthClient < BaseClient
|
5
|
+
def initialize(client_id, client_secret, oauth_api_base)
|
6
|
+
signature = Base64.encode64("#{client_id}:#{client_secret}").gsub("\n", "")
|
7
|
+
super(oauth_api_base, "", {'Authorization' => "Basic #{signature}"})
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
data/lib/chatwork/operations.rb
CHANGED
@@ -11,20 +11,20 @@ module ChatWork
|
|
11
11
|
|
12
12
|
def define_get
|
13
13
|
instance_eval do
|
14
|
-
def get(params = {})
|
14
|
+
def get(params = {}, &block)
|
15
15
|
@assign_path = parse_if_hash_key_exists(path, params, :room_id)
|
16
16
|
attach_nested_resource_id(params)
|
17
|
-
convert(ChatWork.client.get(@assign_path, params))
|
17
|
+
convert(ChatWork.client.get(@assign_path, params, &block))
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
def define_create
|
23
23
|
instance_eval do
|
24
|
-
def create(params = {})
|
24
|
+
def create(params = {}, &block)
|
25
25
|
@assign_path = parse_if_hash_key_exists(path, params, :room_id)
|
26
26
|
attach_nested_resource_id(params)
|
27
|
-
convert(ChatWork.client.post(@assign_path, params))
|
27
|
+
convert(ChatWork.client.post(@assign_path, params, &block))
|
28
28
|
end
|
29
29
|
end
|
30
30
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ChatWork
|
2
|
+
class Token
|
3
|
+
# refresh access_token with refresh_token
|
4
|
+
#
|
5
|
+
# @param refresh_token [String]
|
6
|
+
# @param scope [Array<String>]
|
7
|
+
#
|
8
|
+
# @return [Hash]
|
9
|
+
# @example response
|
10
|
+
# {
|
11
|
+
# "access_token" => "new_access_token",
|
12
|
+
# "token_type" => "Bearer",
|
13
|
+
# "expires_in" => "1800",
|
14
|
+
# "refresh_token" => "refresh_token",
|
15
|
+
# "scope" => "users.all:read rooms.all:read_write contacts.all:read_write",
|
16
|
+
# }
|
17
|
+
# ["access_token", "token_type", "expires_in", "refresh_token", "scope"]
|
18
|
+
def self.refresh_access_token(refresh_token, scope = [])
|
19
|
+
params = {
|
20
|
+
grant_type: "refresh_token",
|
21
|
+
refresh_token: refresh_token,
|
22
|
+
}
|
23
|
+
params[:scope] = scope.join(" ") unless scope.empty?
|
24
|
+
|
25
|
+
response = ChatWork.oauth_client.post("/token", params)
|
26
|
+
raise response if response.kind_of?(Exception)
|
27
|
+
response
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/chatwork/version.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
describe ChatWork::ChatWorkError do
|
2
|
+
describe '.from_response' do
|
3
|
+
subject { ChatWork::ChatWorkError.from_response(status, body, headers) }
|
4
|
+
|
5
|
+
context "with WWW-Authenticate header" do
|
6
|
+
let(:status) { 401 }
|
7
|
+
|
8
|
+
let(:body) do
|
9
|
+
<<-JSON
|
10
|
+
{"errors":["Invalid API Token"]}
|
11
|
+
JSON
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:headers) do
|
15
|
+
{
|
16
|
+
'WWW-Authenticate' => 'Bearer error="invalid_token", error_description="The access token expired"'
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
it { should be_an_instance_of ChatWork::AuthenticateError }
|
21
|
+
its(:error) { should eq 'invalid_token' }
|
22
|
+
its(:error_description) { should eq 'The access token expired' }
|
23
|
+
its(:error_response) { should eq ['Invalid API Token'] }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
describe ChatWork::Client do
|
2
|
+
describe '#initialize' do
|
3
|
+
subject { ChatWork::Client.new(api_key, access_token, api_base, api_version) }
|
4
|
+
|
5
|
+
let(:api_key) { nil }
|
6
|
+
let(:access_token) { nil }
|
7
|
+
let(:api_base) { 'https://api.chatwork.com/' }
|
8
|
+
let(:api_version) { '/v2' }
|
9
|
+
|
10
|
+
context 'with api_key' do
|
11
|
+
let(:api_key) { 'my_api_key' }
|
12
|
+
|
13
|
+
it 'client has X-ChatWorkToken header' do
|
14
|
+
connection = subject.instance_variable_get(:@conn)
|
15
|
+
|
16
|
+
expect(connection.headers['X-ChatWorkToken']).to eq "my_api_key"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'with access_token' do
|
21
|
+
let(:access_token) { 'my_access_token' }
|
22
|
+
|
23
|
+
it 'client has Authorization header' do
|
24
|
+
connection = subject.instance_variable_get(:@conn)
|
25
|
+
|
26
|
+
expect(connection.headers['Authorization']).to eq "Bearer my_access_token"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'without both api_key and access_token' do
|
31
|
+
it { expect { subject }.to raise_error 'Either api_key or access_token is required' }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
describe ChatWork::Member do
|
2
|
+
include_context 'shared stubs'
|
3
|
+
describe 'get room members' do
|
4
|
+
it 'should work' do
|
5
|
+
expect(ChatWork::Member.get(room_id: 1)).to eq([{ "account_id"=> 123, "name"=> "John Smith"}])
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should work with block' do
|
9
|
+
ChatWork::Member.get(room_id: 1) do |body, headers|
|
10
|
+
expect(body).to eq([{ "account_id"=> 123, "name"=> "John Smith"}])
|
11
|
+
expect(headers).to eq({'X-ratelimit-limit'=> '100', 'X-ratelimit-remaining'=> '44'})
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
describe ChatWork::OAuthClient do
|
2
|
+
describe '#initialize' do
|
3
|
+
subject { ChatWork::OAuthClient.new(client_id, client_secret, api_base) }
|
4
|
+
|
5
|
+
let(:client_id) { 'client_id' }
|
6
|
+
let(:client_secret) { 'client_secret' }
|
7
|
+
let(:api_base) { 'https://token.chatwork.com/' }
|
8
|
+
let(:signature) { 'Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=' }
|
9
|
+
|
10
|
+
it 'client has Authorization header' do
|
11
|
+
connection = subject.instance_variable_get(:@conn)
|
12
|
+
|
13
|
+
expect(connection.headers['Authorization']).to eq "Basic #{signature}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative '../../shared_oauth_stubs.rb'
|
2
|
+
|
3
|
+
describe ChatWork::Token do
|
4
|
+
include_context 'shared oauth stubs'
|
5
|
+
|
6
|
+
describe '.refresh_access_token' do
|
7
|
+
subject { ChatWork::Token.refresh_access_token(refresh_token, scope) }
|
8
|
+
|
9
|
+
let(:scope) { [] }
|
10
|
+
|
11
|
+
its(["access_token"]) { should eq "new_access_token" }
|
12
|
+
its(["token_type"]) { should eq "Bearer" }
|
13
|
+
its(["expires_in"]) { should eq "1800" }
|
14
|
+
its(["refresh_token"]) { should eq "refresh_token" }
|
15
|
+
its(["scope"]) { should eq "users.all:read rooms.all:read_write contacts.all:read_write" }
|
16
|
+
end
|
17
|
+
end
|
data/spec/lib/chatwork_spec.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'chatwork'
|
3
|
-
|
4
1
|
describe ChatWork do
|
5
2
|
describe '#client' do
|
6
3
|
subject { super().client }
|
4
|
+
|
5
|
+
before do
|
6
|
+
allow(ChatWork).to receive(:api_key) { "aaaa" }
|
7
|
+
end
|
8
|
+
|
7
9
|
it { should be_a(ChatWork::Client) }
|
8
10
|
end
|
9
11
|
|
@@ -38,4 +40,18 @@ describe ChatWork do
|
|
38
40
|
it { is_expected.to eq test_token }
|
39
41
|
end
|
40
42
|
end
|
43
|
+
|
44
|
+
describe '#access_token' do
|
45
|
+
context 'when does not set env' do
|
46
|
+
subject { super().access_token }
|
47
|
+
it { should be_nil }
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when sets env' do
|
51
|
+
let(:test_token) { 'chatwork_test_token' }
|
52
|
+
before { ENV['CHATWORK_ACCESS_TOKEN'] = test_token }
|
53
|
+
subject { super().access_token }
|
54
|
+
it { is_expected.to eq test_token }
|
55
|
+
end
|
56
|
+
end
|
41
57
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
RSpec.shared_context "shared oauth stubs" do
|
4
|
+
let(:client_id) { 'client_id' }
|
5
|
+
let(:client_secret) { 'client_secret' }
|
6
|
+
let(:refresh_token) { 'refresh_token' }
|
7
|
+
let(:api_version) { ChatWork.api_version }
|
8
|
+
let(:oauth_api_base) { ChatWork.oauth_api_base }
|
9
|
+
let(:signature) { 'Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=' }
|
10
|
+
|
11
|
+
let(:test_adapter) {
|
12
|
+
token = {
|
13
|
+
access_token: 'new_access_token',
|
14
|
+
token_type: 'Bearer',
|
15
|
+
expires_in: '1800',
|
16
|
+
refresh_token: refresh_token,
|
17
|
+
scope: 'users.all:read rooms.all:read_write contacts.all:read_write'
|
18
|
+
}
|
19
|
+
|
20
|
+
Faraday::Adapter::Test::Stubs.new do |stub|
|
21
|
+
stub.post("/token") {
|
22
|
+
[200,
|
23
|
+
{"Content-Type" => "application/json"},
|
24
|
+
token.to_json ]
|
25
|
+
}
|
26
|
+
end
|
27
|
+
}
|
28
|
+
|
29
|
+
let(:faraday) {
|
30
|
+
default_header = {
|
31
|
+
'Authorization': "Basic #{signature}",
|
32
|
+
'User-Agent': "ChatWork#{api_version} RubyBinding/#{ChatWork::VERSION}"
|
33
|
+
}
|
34
|
+
Faraday.new(oauth_api_base, headers: default_header) do |builder|
|
35
|
+
builder.request :url_encoded
|
36
|
+
builder.adapter Faraday.default_adapter
|
37
|
+
builder.adapter :test, test_adapter
|
38
|
+
end
|
39
|
+
}
|
40
|
+
|
41
|
+
let(:oauth_client) {
|
42
|
+
allow(Faraday).to receive(:new).and_return(faraday)
|
43
|
+
ChatWork::OAuthClient.new(client_id, client_secret, oauth_api_base)
|
44
|
+
}
|
45
|
+
|
46
|
+
before(:each) do
|
47
|
+
allow(ChatWork).to receive(:oauth_client).and_return(oauth_client)
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
|
3
|
+
RSpec.shared_context "shared stubs" do
|
4
|
+
let(:api_key) { 'api_key' }
|
5
|
+
let(:access_token) { nil }
|
6
|
+
let(:api_version) { ChatWork.api_version }
|
7
|
+
let(:api_base) { ChatWork.api_base }
|
8
|
+
|
9
|
+
let(:test_adapter) {
|
10
|
+
Faraday::Adapter::Test::Stubs.new do |stub|
|
11
|
+
stub.get("#{api_version}/rooms/1/members") { [200,
|
12
|
+
{'X-RateLimit-Limit': '100', 'X-RateLimit-Remaining': '44'},
|
13
|
+
[{ "account_id"=> 123, "name"=> "John Smith"}].to_json ]
|
14
|
+
}
|
15
|
+
end
|
16
|
+
}
|
17
|
+
|
18
|
+
let(:faraday) {
|
19
|
+
default_header = {
|
20
|
+
'X-ChatWorkToken': api_key,
|
21
|
+
'User-Agent': "ChatWork#{api_version} RubyBinding/#{ChatWork::VERSION}"
|
22
|
+
}
|
23
|
+
Faraday.new("#{api_base}#{api_version}", headers: default_header) do |builder|
|
24
|
+
builder.request :url_encoded
|
25
|
+
builder.adapter Faraday.default_adapter
|
26
|
+
builder.adapter :test, test_adapter
|
27
|
+
end
|
28
|
+
}
|
29
|
+
|
30
|
+
let(:client) {
|
31
|
+
allow(Faraday).to receive(:new).and_return(faraday)
|
32
|
+
client = ChatWork::Client.new(api_key, access_token, api_base, api_version)
|
33
|
+
client
|
34
|
+
}
|
35
|
+
|
36
|
+
before(:each) do
|
37
|
+
allow(ChatWork).to receive(:client).and_return(client)
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chatwork
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- asonas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -56,16 +56,30 @@ dependencies:
|
|
56
56
|
name: rspec
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-its
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
description: ChatWork is cloud-based business chat tool
|
70
84
|
email:
|
71
85
|
- hzw1258@gmail.com
|
@@ -74,12 +88,15 @@ extensions: []
|
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
76
90
|
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- CHANGELOG.md
|
77
93
|
- Gemfile
|
78
94
|
- LICENSE.txt
|
79
95
|
- README.md
|
80
96
|
- Rakefile
|
81
97
|
- chatwork.gemspec
|
82
98
|
- lib/chatwork.rb
|
99
|
+
- lib/chatwork/base_client.rb
|
83
100
|
- lib/chatwork/chatwork_error.rb
|
84
101
|
- lib/chatwork/client.rb
|
85
102
|
- lib/chatwork/contacts.rb
|
@@ -88,11 +105,20 @@ files:
|
|
88
105
|
- lib/chatwork/member.rb
|
89
106
|
- lib/chatwork/message.rb
|
90
107
|
- lib/chatwork/my_task.rb
|
108
|
+
- lib/chatwork/oauth_client.rb
|
91
109
|
- lib/chatwork/operations.rb
|
92
110
|
- lib/chatwork/room.rb
|
93
111
|
- lib/chatwork/task.rb
|
112
|
+
- lib/chatwork/token.rb
|
94
113
|
- lib/chatwork/version.rb
|
114
|
+
- spec/lib/chatwork/chatwork_error_spec.rb
|
115
|
+
- spec/lib/chatwork/client_spec.rb
|
116
|
+
- spec/lib/chatwork/member_spec.rb
|
117
|
+
- spec/lib/chatwork/oauth_client_spec.rb
|
118
|
+
- spec/lib/chatwork/token_spec.rb
|
95
119
|
- spec/lib/chatwork_spec.rb
|
120
|
+
- spec/shared_oauth_stubs.rb
|
121
|
+
- spec/shared_stubs.rb
|
96
122
|
- spec/spec_helper.rb
|
97
123
|
homepage: https://github.com/asonas/chatwork-ruby
|
98
124
|
licenses:
|
@@ -114,10 +140,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
140
|
version: '0'
|
115
141
|
requirements: []
|
116
142
|
rubyforge_project:
|
117
|
-
rubygems_version: 2.6.
|
143
|
+
rubygems_version: 2.6.11
|
118
144
|
signing_key:
|
119
145
|
specification_version: 4
|
120
146
|
summary: Ruby bindings of Chatwork API
|
121
147
|
test_files:
|
148
|
+
- spec/lib/chatwork/chatwork_error_spec.rb
|
149
|
+
- spec/lib/chatwork/client_spec.rb
|
150
|
+
- spec/lib/chatwork/member_spec.rb
|
151
|
+
- spec/lib/chatwork/oauth_client_spec.rb
|
152
|
+
- spec/lib/chatwork/token_spec.rb
|
122
153
|
- spec/lib/chatwork_spec.rb
|
154
|
+
- spec/shared_oauth_stubs.rb
|
155
|
+
- spec/shared_stubs.rb
|
123
156
|
- spec/spec_helper.rb
|