fluent-plugin-typetalk 0.0.2 → 0.0.3

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,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MzFhNThjMWE0NjIzNmIyZDNhMDk4ODg1OTM0OGQ3ZTNjOGIwYjUzOQ==
4
+ YzY5YjQ5MTdhMmYxYzliZDRlMmQzYzUzZTEyNmM4ZDE5NTQwYTNmMw==
5
5
  data.tar.gz: !binary |-
6
- YmE2Y2ZkM2ViZmNmNzg0MzRiMGFhYmI5MzU2N2IwODk0MTA4ZDdlOQ==
6
+ ZWQ1NjVkOGMwZWIwNDQzZjZmYTQxNzMzNzZmOTFiMjhhNzk0NmIwZA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ODQzZDBmMjc1Zjc4Njg1ZWY5NTczMjE3NDI5ODEzZTE0MzIyZDRlZGJiNzU2
10
- NzhlNTljZmYyOWU3MDBjYjI5MmQwMGZiMmNiY2EyNjk3NWEwZGJhYmViNjM2
11
- YmJlOGUzYjM1OWU0MzM4NTRjYTQ4NDA5YjVhMTY4ZDQ1ZDFhYWQ=
9
+ NWU4MGIxZTM2NjUyZWI5YTIzMDY3OGIwYzM5NTcwZTcyNGE1M2RjZGJkODZk
10
+ MDE2ZDFkMjNhODE2MjQwMDdjZDdhMGQ0NWUxYWE3YTg0OWNmM2Y0NTdhYmVh
11
+ ODJjZGI1YmMwNDQ4ZjU5OWRjMjg1ODVhN2VjMTA5MTQzNzk0NTg=
12
12
  data.tar.gz: !binary |-
13
- NjBkMjgyODJjZDgzY2E3NjA1YTI1ODRiMGI4MDlmNTA0NzhhZmUxY2Q0MzIz
14
- ZTZkNmQzODE2NWI5YmVmMzQ0YTcyZGFiMWQ2NWRiYzJhYjMzYjljNjQ4MTQ3
15
- NzFjMTE4OTVkNDhlOTYxYjFhOWQ1YzNiZjFlOWRkZTdiZTFhZDA=
13
+ NWMxY2M4NGIyZmFhZmI0ZDQ5M2I2NTA0N2E3NDA3MTc2MjQ4N2M0M2E0NGM0
14
+ YTBhYjZlNDY1NTMxNjk3NWJmY2EzZWUwZDQzNzJjOTc1OWQxZWNjM2MxMzhi
15
+ ODU5MGRkNjk2ZDNmM2Q5ZDRiYWFjOTFkOWI1MGY3Y2RhNmIzM2E=
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-typetalk"
7
- spec.version = "0.0.2"
7
+ spec.version = "0.0.3"
8
8
  spec.authors = ["tksmd"]
9
9
  spec.email = ["someda@isenshi.com"]
10
10
  spec.description = %q{fluent plugin to send message to typetalk}
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "rake"
22
22
  spec.add_development_dependency "rr", ">= 1.0.0"
23
23
  spec.add_runtime_dependency "fluentd"
24
+ spec.add_runtime_dependency "typetalk"
24
25
 
25
26
  spec.required_ruby_version = '>= 1.9.3'
26
27
  end
@@ -23,13 +23,19 @@ module Fluent
23
23
 
24
24
  def initialize
25
25
  super
26
- require 'erb'
27
26
  require 'socket'
27
+ require 'typetalk'
28
28
  end
29
29
 
30
30
  def configure(conf)
31
31
  super
32
- @typetalk = Typetalk.new(conf['client_id'], conf['client_secret'])
32
+ Typetalk.configure do |c|
33
+ c.client_id = conf['client_id']
34
+ c.client_secret = conf['client_secret']
35
+ c.scope = 'topic.post'
36
+ c.user_agent = "fluent-plugin-typetalk Ruby/#{RUBY_VERSION}"
37
+ end
38
+ @typetalk = Typetalk::Api.new
33
39
  @hostname = Socket.gethostname
34
40
 
35
41
  @out_keys = @out_keys.split(',')
@@ -76,7 +82,19 @@ module Fluent
76
82
 
77
83
  def send_message(tag, time, record)
78
84
  message = evaluate_message(tag, time, record)
79
- @typetalk.post(@topic_id, message)
85
+ begin
86
+ @typetalk.post_message(@topic_id, message)
87
+ rescue Typetalk::Unauthorized
88
+ raise TypetalkError, "invalid credentials used. check client_id and client_secret in your configuration."
89
+ rescue => e
90
+ msg = ''
91
+ res = JSON.parse(e.message) rescue {}
92
+ unless res['body'].nil?
93
+ body = JSON.parse(res['body']) rescue {}
94
+ msg = body['error']
95
+ end
96
+ raise TypetalkError, "failed to post, msg: #{msg}, code: #{res['status']}"
97
+ end
80
98
  end
81
99
 
82
100
  def evaluate_message(tag, time, record)
@@ -99,84 +117,6 @@ module Fluent
99
117
 
100
118
  end
101
119
 
102
- class Typetalk
103
-
104
- USER_AGENT = "fluent-plugin-typetalk Ruby/#{RUBY_VERSION}"
105
-
106
- def initialize(client_id, client_secret)
107
- require 'net/http'
108
- require 'uri'
109
- require 'json'
110
-
111
- @client_id = client_id
112
- @client_secret = client_secret
113
-
114
- @http = Net::HTTP.new('typetalk.in', 443)
115
- @http.use_ssl = true
116
- end
117
-
118
- def post(topic_id, message)
119
- check_token()
120
- $log.debug("Typetalk access_token : #{@access_token}")
121
-
122
- res = @http.post(
123
- "/api/v1/topics/#{topic_id}",
124
- "message=#{message}",
125
- { 'Authorization' => "Bearer #{@access_token}", 'User-Agent' => USER_AGENT }
126
- )
127
-
128
- # todo: handling 429
129
- unless res and res.is_a?(Net::HTTPSuccess)
130
- msg = ""
131
- unless res.body.nil?
132
- json = JSON.parse(res.body)
133
- msg = json.fetch('errors', [])[0].fetch('message',"")
134
- end
135
- raise TypetalkError, "failed to post, msg: #{msg}, code: #{res.code}"
136
- end
137
-
138
- end
139
-
140
- def check_token
141
-
142
- if @access_token.nil?
143
- update_token()
144
- elsif Time.now >= @expires
145
- update_token(true)
146
- end
147
-
148
- end
149
-
150
- def update_token(refresh = false)
151
- params = "client_id=#{@client_id}&client_secret=#{@client_secret}"
152
- unless refresh
153
- params << "&grant_type=client_credentials&scope=topic.post"
154
- else
155
- params << "&grant_type=refresh_token&refresh_token=#{@refresh_token}"
156
- end
157
-
158
- res = @http.post(
159
- "/oauth2/access_token",
160
- params,
161
- { 'User-Agent' => USER_AGENT }
162
- )
163
-
164
- if res.is_a?(Net::HTTPUnauthorized)
165
- raise TypetalkError, "invalid credentials used. check client_id and client_secret in your configuration."
166
- end
167
-
168
- unless res.is_a?(Net::HTTPSuccess)
169
- raise TypetalkError, "unexpected error occured in getting access_token, code: #{res && res.code}"
170
- end
171
-
172
- json = JSON.parse(res.body)
173
- @expires = Time.now + json['expires_in'].to_i
174
- @refresh_token = json['refresh_token']
175
- @access_token = json['access_token']
176
- end
177
-
178
- end
179
-
180
120
  class TypetalkError < RuntimeError; end
181
121
 
182
122
  end
@@ -23,13 +23,13 @@ class TypetalkOutputTest < Test::Unit::TestCase
23
23
 
24
24
  def test_configure
25
25
  d = create_driver()
26
- assert_equal d.instance.typetalk.instance_variable_get(:@client_id), '123456'
27
- assert_equal d.instance.typetalk.instance_variable_get(:@client_secret), 'secret'
26
+ assert_equal '123456', Typetalk.config.client_id
27
+ assert_equal 'secret', Typetalk.config.client_secret
28
28
  end
29
29
 
30
30
  def test_write
31
31
  d = create_driver()
32
- stub(d.instance.typetalk).post(1, 'notice : test1')
32
+ stub(d.instance.typetalk).post_message(1, 'notice : test1')
33
33
  d.emit({'message' => 'test1'})
34
34
  d.run()
35
35
  end
@@ -38,7 +38,7 @@ class TypetalkOutputTest < Test::Unit::TestCase
38
38
  d = create_driver(CONFIG, 'warn')
39
39
  d.instance.message = "notice : %s [%s]"
40
40
  d.instance.out_keys = ["message", "time"]
41
- stub(d.instance.typetalk).post(1, "notice : test1 [1399910738]")
41
+ stub(d.instance.typetalk).post_message(1, "notice : test1 [1399910738]")
42
42
 
43
43
  ENV["TZ"]="Asia/Tokyo"
44
44
  t = Time.strptime('2014-05-13 01:05:38', '%Y-%m-%d %T')
@@ -46,4 +46,46 @@ class TypetalkOutputTest < Test::Unit::TestCase
46
46
  d.run()
47
47
  end
48
48
 
49
- end
49
+ def test_post_message_unauthorized_error
50
+ d = create_driver()
51
+ stub(d.instance.typetalk).post_message(1, 'notice : test1') {
52
+ raise Typetalk::Unauthorized, {status:401, headers:{}, body:''}.to_json
53
+ }
54
+ stub(d.instance.log).error {|name, params|
55
+ assert_equal "out_typetalk:", name
56
+ assert_equal Fluent::TypetalkError, params[:error_class]
57
+ assert_equal "invalid credentials used. check client_id and client_secret in your configuration.", params[:error]
58
+ }
59
+ d.emit({'message' => 'test1'})
60
+ d.run()
61
+ end
62
+
63
+ def test_post_message_invalid_request_error
64
+ d = create_driver()
65
+ stub(d.instance.typetalk).post_message(1, 'notice : test1') {
66
+ raise Typetalk::InvalidRequest, {status:400, headers:{}, body:'{"error":"invalid_client","error_description":""}'}.to_json
67
+ }
68
+ stub(d.instance.log).error {|name, params|
69
+ assert_equal "out_typetalk:", name
70
+ assert_equal Fluent::TypetalkError, params[:error_class]
71
+ assert_equal "failed to post, msg: invalid_client, code: 400", params[:error]
72
+ }
73
+ d.emit({'message' => 'test1'})
74
+ d.run()
75
+ end
76
+
77
+ def test_post_message_notfound_error
78
+ d = create_driver()
79
+ stub(d.instance.typetalk).post_message(1, 'notice : test1') {
80
+ raise Typetalk::NotFound, {status:404, headers:{}, body:''}.to_json
81
+ }
82
+ stub(d.instance.log).error {|name, params|
83
+ assert_equal "out_typetalk:", name
84
+ assert_equal Fluent::TypetalkError, params[:error_class]
85
+ assert_equal "failed to post, msg: , code: 404", params[:error]
86
+ }
87
+ d.emit({'message' => 'test1'})
88
+ d.run()
89
+ end
90
+
91
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-typetalk
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
  - tksmd
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-13 00:00:00.000000000 Z
11
+ date: 2014-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ! '>='
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: typetalk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  description: fluent plugin to send message to typetalk
70
84
  email:
71
85
  - someda@isenshi.com