my-lib 0.0.1.1 → 0.0.1.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.
@@ -0,0 +1,43 @@
1
+
2
+ #
3
+ # Module of Machine
4
+ #
5
+ # this class has queue of jobs.controll jobs and run jobs
6
+ module MyMachine
7
+
8
+ Version = "0.0.1"
9
+
10
+ attr_accessor :queue
11
+
12
+ def initialize
13
+ require 'thread'
14
+ @queue = Queue.new
15
+ end
16
+
17
+ def setup
18
+ setupjobs
19
+ setupmachine
20
+ end
21
+
22
+ def go
23
+ puts "You need write the go method!"
24
+ end
25
+
26
+ def retry(job)
27
+ @queue.push job
28
+ end
29
+
30
+ private
31
+
32
+ def setupjobs
33
+ puts "You need write the setupjobs method!"
34
+ end
35
+
36
+ def setupmachine
37
+ puts "You need write the setupmachine method!"
38
+ end
39
+
40
+ end
41
+
42
+
43
+
data/lib/lib/myat.rb ADDED
@@ -0,0 +1,53 @@
1
+ # -*- coding:utf-8 -*-
2
+ # Atコマンドを突っ込む
3
+ # MyGCalModuleとの連携で使う
4
+ #
5
+ # @config
6
+ # ~/config.ymlに
7
+ # atmodule:
8
+ # scriptdir: /Users/seijiro/scripts
9
+ # rbdir: /Users/seijiro/code/ruby
10
+ #
11
+ # 使い方など
12
+ # class ThisDo
13
+ # include MyAtModule
14
+ # include MyGCalModule
15
+ # して
16
+ # o = ThisDo.new
17
+ # とかでok
18
+ module MyAtModule
19
+ def initialize
20
+ @c = MyConfig.get['atmodule']
21
+ end
22
+
23
+ def gcaljobs_2_at
24
+ @gcal_jobs.each { |job| jobs2at(job) }
25
+ return self
26
+ end
27
+
28
+ def jobs2at(job)
29
+ command = _at_command(job)
30
+ File.open("#{_at_scriptpath(job)}","w") do |io|
31
+ io.write(command)
32
+ end
33
+
34
+ atcommand = "/usr/bin/at -f #{_at_scriptpath(job)} #{job[:start].localtime.strftime("%H:%M %m/%d/%y")}"
35
+ p atcommand
36
+ p command
37
+ system atcommand
38
+ gcal_checkout(job[:object])
39
+ end
40
+
41
+ def _at_scriptpath(job)
42
+ "#{@c['scriptdir']}/job2at_#{job[:start].localtime.strftime("%Y%m%d%H%M")}.sh"
43
+ end
44
+
45
+ def _at_command(job)
46
+ "#! /bin/bash
47
+ #ユーザーの環境変数パスを使いたい
48
+ source ~/.bashrc
49
+ growlnotify -t 'Gcal2At' -m 'pusher tail #{job[:filename]} start . end is #{job[:start].localtime.strftime("%Y/%m/%d/%H/%M")}'
50
+ ruby #{@c['rbdir']}/pushertail.rb #{job[:filename]} '#{job[:end].to_s}'
51
+ "
52
+ end
53
+ end
@@ -0,0 +1,41 @@
1
+ #-*- coding:utf-8 -*-
2
+ #~/config.ymlに配置された設定ファイルを読んで返す
3
+ class MyConfig
4
+ @@file = nil
5
+ @@data = nil
6
+
7
+ def self.set(filename)
8
+ @@file = filename
9
+ end
10
+
11
+ def self.hoge
12
+ return @@file
13
+ end
14
+
15
+ # 設定オブジェクトを返す
16
+ def self.get
17
+ self.read if @@data.nil?
18
+ return @@data
19
+ end
20
+
21
+ private
22
+
23
+ def self.read
24
+ require 'yaml'
25
+ @@file = "#{ENV['HOME']}/config.yml" if @@file.nil?
26
+ @@data = YAML.load_file("#{@@file}")
27
+ rescue
28
+ raise "Can't Read config.yml"
29
+ end
30
+
31
+ def self.reload
32
+ self.read
33
+ end
34
+
35
+ def self.dispose
36
+ @@file = nil
37
+ @@data = nil
38
+ end
39
+ end
40
+
41
+
data/lib/lib/mydb.rb ADDED
@@ -0,0 +1,57 @@
1
+ # DBに接続してなんでもいれておくtableへのインサートを提供する
2
+ #
3
+ # @dbcon:DBコネクション
4
+ # @insert:インサート文
5
+ # @config:
6
+ # ~/config.ymlに
7
+ # mydbmodule:
8
+ # server: localhost
9
+ # port: 3389
10
+ # socket: /tmp/mysql.sock
11
+ # user: xxxx
12
+ # pass: xxxxxx
13
+ # database: xxxxx
14
+ # を設定
15
+ #
16
+ # 使い方
17
+ # class ThisDo
18
+ # include MyDBModule
19
+ # して
20
+ # o = ThisDo.new
21
+ # o.insert_DB("my_app_tail",'data')
22
+ # でOK
23
+ #
24
+ module MyDBModule
25
+ #DBコネクション
26
+ @dbcon
27
+
28
+ #インサート文
29
+ @insert
30
+
31
+ # DBに接続する
32
+ def set_my_db
33
+ require "mysql"
34
+ @c = MyConfig.get
35
+ @dbcon = Mysql::new(
36
+ @c['mydbmodule']['server'],
37
+ @c['mydbmodule']['user'],
38
+ @c['mydbmodule']['pass'],
39
+ @c['mydbmodule']['database'],
40
+ @c['mydbmodule']['port'],
41
+ @c['mydbmodule']['socket'],
42
+ )
43
+ @dbcon.query("set character set utf8") #おまじない
44
+ @dbcon.query("use " + @c['mydbmodule']['database'])
45
+ @insertsql = @dbcon.prepare("insert into keyvalue(`usage`,`value`) values (?,?);")
46
+ end
47
+
48
+ # テーブルにインサートする
49
+ #
50
+ # args
51
+ # usage : string key
52
+ # value : string value
53
+ def insert_DB(key='test_app',value='')
54
+ set_my_db if @dbcon == nil
55
+ @insertsql.execute(key,value)
56
+ end
57
+ end
data/lib/lib/mygcal.rb ADDED
@@ -0,0 +1,76 @@
1
+
2
+ # Googleカレンダーへのアクセスを提供する
3
+ #
4
+ # ~/config.ymlに
5
+ # gmail:
6
+ # address: YourMailAdress@gmail.com
7
+ # pass: xxxxxxxxxx
8
+ # feedurl: http://www.google.com/calendar/feeds/xxxxxxxx%40gmail.com/private/full
9
+ #
10
+ # 使い方など
11
+ # class ThisDo
12
+ # include MyGCalModule
13
+ # して
14
+ # o = ThisDo.new
15
+ # o.gcal_read
16
+ # とかでok
17
+ module MyGCalModule
18
+ attr_accessor :gmail,:gmailpass,:gcalfeedurl,:gcal_query
19
+
20
+ def gcal_read
21
+ service
22
+ @gcal_events = @gcal.events
23
+ return self
24
+ end
25
+
26
+ #GCalへ書きこむ
27
+ def gcal_write(eventdata)
28
+ service
29
+ event = @gcal.create_event
30
+ event.title = eventdata[:title]
31
+ event.st = eventdata[:start]
32
+ event.en = eventdata[:end]
33
+ event.save!
34
+ @gcal_event = event
35
+ return self
36
+ end
37
+
38
+ # gcalのイベントをAtMduleが食べれる形に変換する
39
+ # 共通のJOBクラスで包もうかしら?
40
+ def gcal_parse_2_jobs
41
+ @gcal_jobs = []
42
+ q = @gcal_query ||= '[Gcal2PusherTail'
43
+ @gcal_events.each do |event|
44
+ begin
45
+ kind,filename = event.title.split(']')
46
+ if(kind == @gcal_query && filename != nil)
47
+ @gcal_jobs << {:filename => filename,
48
+ :start => event.st,
49
+ :end => event.en,
50
+ :object => event}
51
+ end
52
+ rescue =>ex
53
+ p ex
54
+ #握りつぶす
55
+ end
56
+ end
57
+ return self
58
+ end
59
+
60
+ #fetchしたデータの取り込み済みマークを立てる
61
+ def gcal_checkout(event)
62
+ event.title = '[FETCHED]' + event.title
63
+ event.save!
64
+ return self
65
+ end
66
+
67
+ # GCalへのアクセス
68
+ def service
69
+ if @gcal_srv.nil?
70
+ require 'gcalapi'
71
+ @c = MyConfig.get['gmail']
72
+ @gcal_srv = GoogleCalendar::Service.new(@c['address'],@c['pass'])
73
+ end
74
+ @gcal = GoogleCalendar::Calendar::new(@gcal_srv, @c['feedurl'])
75
+ end
76
+ end
@@ -0,0 +1,39 @@
1
+
2
+ # Class For Log to /var/log/system.log
3
+ # @author modeverv@gmail.com
4
+ # @example
5
+ # MyLogger.ln("message")
6
+ # MyLogger.lw("message")
7
+ class MyLogger
8
+
9
+ Version = "0.0.1"
10
+
11
+ # log notice
12
+ # @param [String] message message for log
13
+ def self.ln(message)
14
+ self.before
15
+ Syslog.log(Syslog::LOG_NOTICE, "%s", message)
16
+ self.after
17
+ end
18
+
19
+ # log warning
20
+ # @param [String] message message for log
21
+ def self.lw(message)
22
+ self.before
23
+ Syslog.log(Syslog::LOG_WARNING, "%s", message)
24
+ self.after
25
+ end
26
+
27
+ private
28
+ # open syslog
29
+ def self.before
30
+ require 'syslog'
31
+ include Syslog::Constants
32
+ Syslog.open("ruby_syslog.rb")
33
+ end
34
+ # close syslog
35
+ def self.after
36
+ Syslog.close();
37
+ end
38
+
39
+ end
@@ -0,0 +1,13 @@
1
+ #-*- coding:utf-8 -*-
2
+
3
+ # 俺俺インスタンスをつくるためにクラスを用意した
4
+ # 典型的なrequireを書きこんでいく
5
+ # 読み込み済みモジュール
6
+ # kconv time
7
+ class MyObject
8
+ def initialize
9
+ require "kconv"
10
+ require "time"
11
+ require "optparse"
12
+ end
13
+ end
@@ -0,0 +1,85 @@
1
+ # Pusherサービスへのアクセスを提供する
2
+ # https://app.pusherapp.com/apps/7449/api_access?welcome=true
3
+ # @pusher_app_id = 'your pusher app id'
4
+ # @pusher_key = 'pusher key'
5
+ # @pusher_secret = 'pusher secret'
6
+ # @config
7
+ # ~/config.ymlに
8
+ # mypushermodule:
9
+ # app_id: xxx
10
+ # key: xxxxxxxxxxxxx
11
+ # secret: xxxxxxxxxx
12
+ # event: my_event
13
+ # channel: test_channel
14
+ #
15
+ # 使い方など
16
+ # class ThisDo
17
+ # include MyPusherModule
18
+ # して
19
+ # o = ThisDo.new
20
+ # o.push_pusher('test_app','test')
21
+ # とかでok
22
+ module MyPusherModule
23
+
24
+ #pussherが設定されているか?
25
+ @pusherconnected
26
+
27
+ # Pusherへの接続を設定する
28
+ def set_my_pusher
29
+ @c = MyConfig.get['mypushermodule']
30
+ require "pusher"
31
+ Pusher.app_id = @c['app_id']
32
+ Pusher.key = @c['key']
33
+ Pusher.secret = @c['secret']
34
+ @pusher_event = @c['event']
35
+ @pusher_channel = @c['channel']
36
+ p @c
37
+ end
38
+
39
+ # Pusherにデータをpushする
40
+ # args
41
+ # app_name : string アプリの名前
42
+ # data : string データ
43
+ def push_pusher(app_name='test_app',data='test')
44
+ if @pusherconnected == nil
45
+ set_my_pusher
46
+ @pusherconnected = true
47
+ end
48
+ begin
49
+ Pusher[@pusher_channel].trigger!(
50
+ @pusher_event, { app_name => data})
51
+ rescue Pusher::Error => e
52
+ p e
53
+ end
54
+ end
55
+
56
+ def get_puhser_html
57
+ return <<-"EOT"
58
+ <!DOCTYPE html>
59
+ <head>
60
+ <title>PusherTail</title>
61
+ <script src="http://js.pusherapp.com/1.8/pusher.min.js"></script>
62
+ <script>
63
+ // Enable pusher logging - don't include this in production
64
+ Pusher.log = function(message) {
65
+ if (window.console && window.console.log) window.console.log(message);
66
+ };
67
+
68
+ // Flash fallback logging - don't include this in production
69
+ WEB_SOCKET_DEBUG = true;
70
+ var pusher = new Pusher('#{ @c['key']}');
71
+ var channel = pusher.subscribe('#{ @c['channel']}');
72
+ channel.bind('#{ @c['event']}', function(data) {
73
+ hoge = document.getElementById("main").innerHTML;
74
+ document.getElementById("main").innerHTML = data['#{ @c['app_name']}'] + "<hr>" + hoge;
75
+ });
76
+ </script>
77
+ </head>
78
+ <body>
79
+ work at Chrome
80
+ <div id="main">&nbsp;</div>
81
+ </body>
82
+ EOT
83
+ end
84
+
85
+ end
@@ -0,0 +1,67 @@
1
+ # 指定秒数ごとにrun関数をループする
2
+ # 各メソッドを必要に応じて再定義して使う。
3
+ #
4
+ # 使い方:
5
+ # someinstance.extend RunPerSecModule
6
+ # someinstance.run(ループのインターバルsec){
7
+ # __block__for__yield__
8
+ # }
9
+ #
10
+ # OR
11
+ #
12
+ # class ThisDo < MyObject
13
+ # include RunPerSecModule
14
+ # .....
15
+ #
16
+ module RunPerSecModule
17
+ # ループフラグ
18
+ @loop_flg
19
+
20
+ # main_loopをループする
21
+ # sec : ループ間隔 秒
22
+ # before_run_loop,ループ,after_run_loopの順番で実行する。
23
+ # ループの中身はloop_hook_pre,与えられたブロック,main_loop,loop_hook_postの順番で実行する
24
+ def run(sec)
25
+ init_run_per_sec_module
26
+ before_run_loop
27
+ while @loop_flg
28
+ loop_hook_pre
29
+ yield
30
+ main_loop
31
+ loop_hook_post
32
+ sleep sec
33
+ end
34
+ after_run_loop
35
+ end
36
+
37
+ # 外からは使わない
38
+ # 無限ループフラグを立てる
39
+ def init_run_per_sec_module
40
+ @loop_flg = true
41
+ end
42
+
43
+ # runのループを止める
44
+ def stop_run
45
+ @loop_flg = false
46
+ end
47
+
48
+ # runメソッドが呼ばれるとループの前に一回だけ実行される
49
+ def before_run_loop
50
+ end
51
+
52
+ # runメソッドのloopの中で最初に実行される
53
+ def loop_hook_pre
54
+ end
55
+
56
+ # runメソッドのloopの中で実行される
57
+ def main_loop
58
+ end
59
+
60
+ # runのループの中でmain_loopのあとで実行される
61
+ def loop_hook_post
62
+ end
63
+
64
+ # runメソッドが呼ばれるとループのあとで実行される
65
+ def after_run_loop
66
+ end
67
+ end