chatwork 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/chatwork.rb +7 -0
- data/lib/chatwork/chatwork_error.rb +50 -0
- data/lib/chatwork/client.rb +3 -3
- data/lib/chatwork/message.rb +13 -0
- data/lib/chatwork/operations.rb +8 -1
- data/lib/chatwork/room.rb +2 -6
- data/lib/chatwork/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa48d1d1c60153c671a81fa04a952031eebd5205
|
4
|
+
data.tar.gz: 60aacafd709ec296c06a72728477ea47fb74f37f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0859e190eedb73f2567b9f09e974b088ba7de3c042dec216e9a75e697cef15f621a4236ad2480dee6a24e1c6093101daa7a25a079b8b3cb0c3b5037885cc462e
|
7
|
+
data.tar.gz: 67e06333414965360910464ffda1c351db85cda582d5b174e384d8d7ec0eb860f278fdcaccd6cb6fa93b792230bea73cd161fca6852f9372cc2dc1fac2ecb5ff
|
data/lib/chatwork.rb
CHANGED
@@ -2,6 +2,13 @@ require "chatwork/version"
|
|
2
2
|
|
3
3
|
module ChatWork
|
4
4
|
autoload(:Client, 'chatwork/client')
|
5
|
+
autoload(:Operations, 'chatwork/operations')
|
6
|
+
autoload(:ChatWorkError, 'chatwork/chatwork_error')
|
7
|
+
autoload(:APIConnectionError, 'chatwork/chatwork_error')
|
8
|
+
autoload(:APIError, 'chatwork/chatwork_error')
|
9
|
+
autoload(:Room, 'chatwork/room')
|
10
|
+
autoload(:Entity, 'chatwork/entity')
|
11
|
+
autoload(:Message, 'chatwork/message')
|
5
12
|
|
6
13
|
@api_base = 'https://api.chatwork.com/'
|
7
14
|
@api_version = '/v1'
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'json'
|
2
|
+
module ChatWork
|
3
|
+
class ChatWorkError < StandardError
|
4
|
+
|
5
|
+
def self.from_response(status, body)
|
6
|
+
hash =
|
7
|
+
begin
|
8
|
+
JSON.load(body)
|
9
|
+
rescue JSON::ParserError => e
|
10
|
+
return ChatWork::APIConnectionError.new("Response JSON is broken. #{e.message}: #{body}", e)
|
11
|
+
end
|
12
|
+
|
13
|
+
unless hash['errors']
|
14
|
+
return APIConnectionError.new("Invalid response #{body}")
|
15
|
+
end
|
16
|
+
|
17
|
+
APIError.new(status, hash["errors"])
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_reader :status
|
21
|
+
attr_reader :error_response
|
22
|
+
|
23
|
+
def initialize(message, status = nil, error_response = nil)
|
24
|
+
@status, @error_response = status, error_response
|
25
|
+
super(message)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class APIConnectionError < ChatWorkError
|
30
|
+
def self.faraday_error(e)
|
31
|
+
new("Connection with ChatWork API server failed. #{e.message}", e)
|
32
|
+
end
|
33
|
+
|
34
|
+
attr_reader :original_error
|
35
|
+
|
36
|
+
def initialize(message, original_error)
|
37
|
+
@original_error = original_error
|
38
|
+
super(message)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class APIError < ChatWorkError
|
43
|
+
attr_reader :errors
|
44
|
+
|
45
|
+
def initilize(message, error_response)
|
46
|
+
@errors = error_response["errors"]
|
47
|
+
super(error_response["message"], status, error_response)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/chatwork/client.rb
CHANGED
@@ -6,10 +6,10 @@ module ChatWork
|
|
6
6
|
def initialize(api_key, api_base, api_version)
|
7
7
|
default_header = {
|
8
8
|
'X-ChatWorkToken' => api_key,
|
9
|
-
'User-Agent' =>
|
9
|
+
'User-Agent' => "ChatWork#{api_version} RubyBinding/#{ChatWork::VERSION}"
|
10
10
|
}
|
11
11
|
|
12
|
-
@conn = Faraday.new(api_base, headers: default_header) do |builder|
|
12
|
+
@conn = Faraday.new("#{api_base}#{api_version}", headers: default_header) do |builder|
|
13
13
|
builder.request :url_encoded
|
14
14
|
builder.adapter Faraday.default_adapter
|
15
15
|
end
|
@@ -25,7 +25,7 @@ module ChatWork
|
|
25
25
|
raise ChatWork::APIConnectionError.new("Response JSON is broken. #{e.message}: #{response.body}")
|
26
26
|
end
|
27
27
|
else
|
28
|
-
ChatWork::ChatWorkError.from_response(response.status,
|
28
|
+
ChatWork::ChatWorkError.from_response(response.status, response.body)
|
29
29
|
end
|
30
30
|
end
|
31
31
|
|
data/lib/chatwork/operations.rb
CHANGED
@@ -7,7 +7,14 @@ module ChatWork
|
|
7
7
|
def define_create
|
8
8
|
instance_eval do
|
9
9
|
def create(params = {})
|
10
|
-
|
10
|
+
# TODO: Consider other pattern
|
11
|
+
# /rooms and /rooms/:room_id
|
12
|
+
assign_path = if params.include?(:room_id)
|
13
|
+
path % params.delete(:room_id)
|
14
|
+
else
|
15
|
+
path
|
16
|
+
end
|
17
|
+
convert(ChatWork.client.post(assign_path, params))
|
11
18
|
end
|
12
19
|
end
|
13
20
|
end
|
data/lib/chatwork/room.rb
CHANGED
@@ -3,15 +3,11 @@ module ChatWork
|
|
3
3
|
install_class_operations :create
|
4
4
|
|
5
5
|
def self.path
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
def message
|
10
|
-
ChatWork.client.post([path, 'messages'].join('/'), params)
|
6
|
+
"/rooms"
|
11
7
|
end
|
12
8
|
|
13
9
|
def path
|
14
|
-
"/rooms
|
10
|
+
"/rooms"
|
15
11
|
end
|
16
12
|
end
|
17
13
|
end
|
data/lib/chatwork/version.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.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- asonas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -80,8 +80,10 @@ files:
|
|
80
80
|
- Rakefile
|
81
81
|
- chatwork.gemspec
|
82
82
|
- lib/chatwork.rb
|
83
|
+
- lib/chatwork/chatwork_error.rb
|
83
84
|
- lib/chatwork/client.rb
|
84
85
|
- lib/chatwork/entity.rb
|
86
|
+
- lib/chatwork/message.rb
|
85
87
|
- lib/chatwork/operations.rb
|
86
88
|
- lib/chatwork/room.rb
|
87
89
|
- lib/chatwork/version.rb
|