fluent-plugin-json-api 0.0.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 +7 -0
- data/.gitignore +25 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +29 -0
- data/Rakefile +11 -0
- data/fluent-plugin-json-api.gemspec +20 -0
- data/lib/fluent/plugin/in_json_api.rb +77 -0
- data/test/plugin/in_json_api.rb +110 -0
- metadata +64 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d1006b0671e52a065430a413925fb13586297a3f
|
4
|
+
data.tar.gz: 419f04fa670350c08beb147d11fb8b048af6311f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dba458a5d507da398eb37ced674b2bea989216cdeea110e43482a8b29952c4f932f48eb02ec9fe11a6db09f91c364c2b2fdf88e7cd1c7bd3848f327ad2428099
|
7
|
+
data.tar.gz: c679b8fd5b329027fcb20b258ca021d81499e50728034ad1661562d6015830cfaf83827d2e9815b59ae8faf7c7b1b5797b579b7c6f98aa0dbe19aa616e24a671
|
data/.gitignore
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# For TextMate, emacs, vim
|
2
|
+
*.gem
|
3
|
+
*.rbc
|
4
|
+
*.swp
|
5
|
+
*.tmproj
|
6
|
+
*~
|
7
|
+
.\#*
|
8
|
+
.bundle
|
9
|
+
.config
|
10
|
+
.yardoc
|
11
|
+
Gemfile.lock
|
12
|
+
InstalledFiles
|
13
|
+
\#*
|
14
|
+
_yardoc
|
15
|
+
coverage
|
16
|
+
doc/
|
17
|
+
lib/bundler/man
|
18
|
+
pkg
|
19
|
+
pkg/*
|
20
|
+
rdoc
|
21
|
+
spec/reports
|
22
|
+
test/tmp
|
23
|
+
test/version_tmp
|
24
|
+
tmp
|
25
|
+
tmtags
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Freddie FUJIWARA
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Fluent::Plugin::Json::Api
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'fluent-plugin-json-api'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install fluent-plugin-json-api
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "fluent-plugin-json-api"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["Freddie Fujiwara"]
|
9
|
+
s.date = %q{2013-09-29}
|
10
|
+
s.email = "github@ze.gs"
|
11
|
+
s.homepage = "http://github.com/freddiefujiwara/fluent-plugin-json-api"
|
12
|
+
s.summary = "json-api plugin for Fluentd"
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency %q<fluentd>, ["~> 0.10.0"]
|
20
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
module Fluent
|
2
|
+
class JsonApiError < StandardError
|
3
|
+
end
|
4
|
+
class JsonApiInput < Input
|
5
|
+
Plugin.register_input('json_api', self)
|
6
|
+
|
7
|
+
config_param :url, :string, :default => 'http://pipes.yahoo.com/pipes/pipe.run?_id=c9b9df32b4c3e0ccbe4547ae7e00ed2f&_render=json&condition=d7D&genre=100533&page=__PAGE__'
|
8
|
+
config_param :rules , :string
|
9
|
+
config_param :sleep , :integer, :default => 0
|
10
|
+
config_param :tag, :string
|
11
|
+
|
12
|
+
attr_reader :urls
|
13
|
+
def initialize
|
14
|
+
super
|
15
|
+
require 'json'
|
16
|
+
require 'net/http'
|
17
|
+
@urls = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def configure(config)
|
21
|
+
super
|
22
|
+
|
23
|
+
begin
|
24
|
+
result = []
|
25
|
+
(eval config['rules']).each_pair do |key,replecements|
|
26
|
+
combinations = []
|
27
|
+
(replecements.to_a).each do |replacement|
|
28
|
+
combinations << {key => replacement}
|
29
|
+
end
|
30
|
+
result << combinations
|
31
|
+
end
|
32
|
+
|
33
|
+
@rules = result[0].product(*result[1..-1])
|
34
|
+
rescue SyntaxError,StandardError => e
|
35
|
+
raise Fluent::ConfigError.new "rules has some errors #{e}"
|
36
|
+
end
|
37
|
+
|
38
|
+
raise Fluent::ConfigError unless ['http','https'].include? URI.parse(config['url']).scheme
|
39
|
+
@rules.each do |pair|
|
40
|
+
url = config['url']
|
41
|
+
pair.each do |key_replacements|
|
42
|
+
key_replacements.each do |key,replacement|
|
43
|
+
url = url.gsub(key.to_s,replacement.to_s)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
@urls.push url
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def start
|
51
|
+
super
|
52
|
+
@thread = Thread.new(&method(:run))
|
53
|
+
end
|
54
|
+
|
55
|
+
def run
|
56
|
+
loop {
|
57
|
+
@urls.each do |url|
|
58
|
+
Engine.emit @tag, Engine.now , crawl(url)
|
59
|
+
sleep @sleep if @sleep.to_i > 0
|
60
|
+
end
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def crawl(url)
|
65
|
+
response = Net::HTTP.get_response(URI.parse(url))
|
66
|
+
case response
|
67
|
+
when Net::HTTPSuccess
|
68
|
+
return JSON.parse response.body
|
69
|
+
end
|
70
|
+
raise Fluent::JsonApiError.new
|
71
|
+
end
|
72
|
+
|
73
|
+
def shutdown
|
74
|
+
Thread.kill(@thread)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'fluent/test'
|
2
|
+
require 'json'
|
3
|
+
require 'fluent/plugin/in_json_api'
|
4
|
+
|
5
|
+
class FileInputTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
Fluent::Test.setup
|
8
|
+
|
9
|
+
@d = create_driver %[
|
10
|
+
url http://pipes.yahoo.com/pipes/pipe.run?_id=c9b9df32b4c3e0ccbe4547ae7e00ed2f&_render=json&condition=d7D&genre=__GENRE__&page=__PAGE__
|
11
|
+
rules {'__PAGE__' => (1..10),'__GENRE__' => [1,100533]}
|
12
|
+
sleep 1
|
13
|
+
tag input.json
|
14
|
+
]
|
15
|
+
@time = Time.now.to_i
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_driver(config = CONFIG)
|
19
|
+
Fluent::Test::OutputTestDriver.new(Fluent::JsonApiInput).configure(config)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_configure
|
23
|
+
assert_equal 'http://pipes.yahoo.com/pipes/pipe.run?_id=c9b9df32b4c3e0ccbe4547ae7e00ed2f&_render=json&condition=d7D&genre=__GENRE__&page=__PAGE__' , @d.instance.url
|
24
|
+
assert_equal @d.instance.rules.size, 20 #10(page) x 2(genre)
|
25
|
+
assert_equal 1 , @d.instance.sleep
|
26
|
+
assert_equal 'input.json', @d.instance.tag
|
27
|
+
[ %[
|
28
|
+
url hoge
|
29
|
+
rules {'__PAGE__' => (1..10)}
|
30
|
+
sleep 1
|
31
|
+
tag input.json
|
32
|
+
], #url wrong
|
33
|
+
%[
|
34
|
+
url ftp://hoge.com
|
35
|
+
rules {'__PAGE__' => (1..10)}
|
36
|
+
sleep 1
|
37
|
+
tag input.json
|
38
|
+
], #url must start at http
|
39
|
+
%[
|
40
|
+
url http://hoge.com
|
41
|
+
sleep 1
|
42
|
+
rules {'__PAGE__' => (1..10)}
|
43
|
+
], #no tag
|
44
|
+
%[
|
45
|
+
url http://hoge.com
|
46
|
+
sleep 1
|
47
|
+
rules {__PAGE__}
|
48
|
+
tag input.json
|
49
|
+
], #rules must be {'key' => array}
|
50
|
+
%[
|
51
|
+
url http://hoge.com
|
52
|
+
sleep 1
|
53
|
+
rules (1..2)
|
54
|
+
tag input.json
|
55
|
+
], #rules must be {'key' => array}
|
56
|
+
].each do |config|
|
57
|
+
assert_raise Fluent::ConfigError do
|
58
|
+
create_driver config
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_urls
|
64
|
+
assert_equal @d.instance.urls.size, 20
|
65
|
+
index = 0
|
66
|
+
(1..10).each do |page|
|
67
|
+
[1,100533].each do |genre|
|
68
|
+
assert_equal "http://pipes.yahoo.com/pipes/pipe.run?_id=c9b9df32b4c3e0ccbe4547ae7e00ed2f&_render=json&condition=d7D&genre=#{genre}&page=#{page}", @d.instance.urls[index]
|
69
|
+
|
70
|
+
index += 1
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_crawls
|
76
|
+
for page in 1..1
|
77
|
+
url = "http://pipes.yahoo.com/pipes/pipe.run?_id=c9b9df32b4c3e0ccbe4547ae7e00ed2f&_render=json&condition=d7D&genre=100533&page=#{page}"
|
78
|
+
response = @d.instance.crawl url
|
79
|
+
assert_not_nil response
|
80
|
+
assert_equal response['count'], 30
|
81
|
+
assert_equal response['value']['items'].size, 30
|
82
|
+
response['value']['items'].each do |item|
|
83
|
+
#content
|
84
|
+
assert item['author']['content'].to_f >= 0.0
|
85
|
+
assert item['author']['content'].to_f <= 5.0
|
86
|
+
|
87
|
+
#link
|
88
|
+
assert_equal URI.parse(item['link']).scheme, 'http' if item['link']
|
89
|
+
#title
|
90
|
+
assert_equal URI.parse(item['title']).scheme, 'http' if item['title']
|
91
|
+
|
92
|
+
#description
|
93
|
+
assert_not_nil item['description']
|
94
|
+
|
95
|
+
#pubDate
|
96
|
+
assert_not_nil item['pubDate']['content']
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
#parse error
|
101
|
+
assert_raise JSON::ParserError do
|
102
|
+
@d.instance.crawl 'http://www.rakuten.co.jp'
|
103
|
+
end
|
104
|
+
|
105
|
+
#404 Not found
|
106
|
+
assert_raise Fluent::JsonApiError do
|
107
|
+
@d.instance.crawl 'http://www.google.com/404'
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fluent-plugin-json-api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Freddie Fujiwara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-09-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: fluentd
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.10.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.10.0
|
27
|
+
description:
|
28
|
+
email: github@ze.gs
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- ".gitignore"
|
34
|
+
- Gemfile
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- fluent-plugin-json-api.gemspec
|
39
|
+
- lib/fluent/plugin/in_json_api.rb
|
40
|
+
- test/plugin/in_json_api.rb
|
41
|
+
homepage: http://github.com/freddiefujiwara/fluent-plugin-json-api
|
42
|
+
licenses: []
|
43
|
+
metadata: {}
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 2.0.0.preview2
|
61
|
+
signing_key:
|
62
|
+
specification_version: 4
|
63
|
+
summary: json-api plugin for Fluentd
|
64
|
+
test_files: []
|