Skip to content

Commit f708731

Browse files
committed
#55 Add Plugin Subscription::StatDb
1 parent c8bd41a commit f708731

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

plugins/subscription/stat_db.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
# Name:: Automatic::Plugin::Subscription::StatDb
3+
# Author:: 774 <http://id774.net>
4+
# Created:: Jul 12, 2013
5+
# Updated:: Jul 12, 2013
6+
# Copyright:: Copyright (c) 2012-2013 Automatic Ruby Developers.
7+
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.
8+
9+
module Automatic::Plugin
10+
class SubscriptionStatDb
11+
require 'open-uri'
12+
require 'active_support'
13+
require 'active_support/core_ext'
14+
require 'rss'
15+
16+
def initialize(config, pipeline=[])
17+
@config = config
18+
@pipeline = pipeline
19+
end
20+
21+
def run
22+
@return_feeds = []
23+
@config['params'].each {|param|
24+
url = "http://statdb.nstac.go.jp/api/1.0b/app/getStatsData?appId=#{@config['appid']}&#{param}"
25+
retries = 0
26+
begin
27+
create_rss(URI.encode(url))
28+
rescue
29+
retries += 1
30+
Automatic::Log.puts("error", "ErrorCount: #{retries}, Fault in parsing: #{url}")
31+
sleep @config['interval'].to_i unless @config['interval'].nil?
32+
retry if retries <= @config['retry'].to_i unless @config['retry'].nil?
33+
end
34+
}
35+
@return_feeds
36+
end
37+
38+
private
39+
def create_rss(url)
40+
Automatic::Log.puts("info", "Parsing: #{url}")
41+
hash = Hash.from_xml open(url).read
42+
json = hash.to_json
43+
data = ActiveSupport::JSON.decode(json)
44+
unless data.nil?
45+
rss = create(data, url)
46+
sleep @config['interval'].to_i unless @config['interval'].nil?
47+
@return_feeds << rss
48+
end
49+
end
50+
51+
def create(data, url)
52+
RSS::Maker.make("2.0") {|maker|
53+
xss = maker.xml_stylesheets.new_xml_stylesheet
54+
maker.channel.title = "Automatic Ruby"
55+
maker.channel.description = "Automatic Ruby"
56+
maker.channel.link = "https://github.com/automaticruby/automaticruby"
57+
maker.items.do_sort = true
58+
item = maker.items.new_item
59+
item.title = "StatDB Feed by Automatic Ruby"
60+
item.link = url
61+
item.content_encoded = data
62+
item.date = Time.now
63+
}
64+
end
65+
end
66+
end

test/integration/test_statdb.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
global:
2+
timezone: Asia/Tokyo
3+
cache:
4+
base: /tmp
5+
log:
6+
level: info
7+
8+
plugins:
9+
- module: SubscriptionStatDb
10+
config:
11+
appid: your_app_id
12+
params:
13+
- cdArea=09003,22004&cdCat01=010920070&cdTimeFrom=2012000101&cdTimeTo=2013000303
14+
15+
- module: PublishConsole

0 commit comments

Comments
 (0)