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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2726cbb931ac2533e1296cbb21a620db62e69e0
4
- data.tar.gz: cd678ffce483f6d477eae0d31972cf0c63ed6b48
3
+ metadata.gz: fa48d1d1c60153c671a81fa04a952031eebd5205
4
+ data.tar.gz: 60aacafd709ec296c06a72728477ea47fb74f37f
5
5
  SHA512:
6
- metadata.gz: b087c39e4d93ccfa8a66a6f4bb5eecb05c606a8d595fa6f38a672ca4571fdb1cc2d381afd7ffa85623f167e56ed2c87eb4943e376592660b7e053abd8c86661f
7
- data.tar.gz: afebd25c0173416546c4dbabf12d5e8f4a637c94abd76f7128df2855d7b40f5f836c0856969e5d6c3a8ee3ea35fbe4cfb0a1b286752554ea1512fbaca68d6466
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
@@ -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' => 'ChatWork#{api_version} RubyBinding/#{ChatWork::VERSION}'
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, rensponse.body)
28
+ ChatWork::ChatWorkError.from_response(response.status, response.body)
29
29
  end
30
30
  end
31
31
 
@@ -0,0 +1,13 @@
1
+ module ChatWork
2
+ class Message < Entity
3
+ install_class_operations :create
4
+
5
+ def self.path
6
+ "/rooms/%d/messages"
7
+ end
8
+
9
+ def path
10
+ "/rooms/%d/messages"
11
+ end
12
+ end
13
+ end
@@ -7,7 +7,14 @@ module ChatWork
7
7
  def define_create
8
8
  instance_eval do
9
9
  def create(params = {})
10
- convert(ChatWork.client.post(path, params))
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
- '/rooms'
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/#{id}"
10
+ "/rooms"
15
11
  end
16
12
  end
17
13
  end
@@ -1,3 +1,3 @@
1
1
  module ChatWork
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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.2
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-29 00:00:00.000000000 Z
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