Class: Wptools::Command
- Inherits:
-
Object
- Object
- Wptools::Command
- Defined in:
- lib/wptools.rb
Class Method Summary collapse
Instance Method Summary collapse
- #buzz(filename) ⇒ Object
-
#initialize(opts, config) ⇒ Command
constructor
A new instance of Command.
- #list(num) ⇒ Object
- #read_analytics(filename) ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(opts, config) ⇒ Command
Returns a new instance of Command.
75 76 77 78 79 80 |
# File 'lib/wptools.rb', line 75 def initialize(opts, config) @opts = opts @config = config spec = {adapter: 'mysql2', host: 'localhost', username: @config.dbuser, password: @config.dbpasswd, database: @config.dbname} DB.prepare(spec) end |
Class Method Details
.load_yaml(filename) ⇒ Object
41 42 43 44 45 |
# File 'lib/wptools.rb', line 41 def self.load_yaml(filename) content = File.read(filename) content = ERB.new(content).result YAML.load(content) end |
.run(argv) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/wptools.rb', line 47 def self.run(argv) STDOUT.sync = true opts = {} opt = OptionParser.new(argv) opt. = "Usage: #{opt.program_name} [-h|--help] config.yml" opt.separator('') opt.separator "#{opt.program_name} Available Options" opt.on_head('-h', '--help', 'Show this message') do |v| puts opt.help exit end opt.on('-v', '--verbose', 'Verbose message') {|v| opts[:v] = v} opt.on('-n', '--dry-run', 'Message only') {|v| opts[:n] = v} commands = ['list', 'buzz', 'none'] opt.on('-c COMMAND', '--command=COMMAND', commands, commands.join('|')) {|v| opts[:c] = v} opt.on('-l NUM', '--limit NUM', 'Limit number') {|v| opts[:l] = v.to_i} opt.on('-d DATAFILE', '--data DATAFILE', 'CSV Datafile') {|v| opts[:d] = v } opt.parse!(argv) if argv.empty? puts opt.help exit end config_file = argv[0] || "~/.wptoolsrc" config = Config.new(load_yaml(config_file)) command = Command.new(opts, config) command.run end |
Instance Method Details
#buzz(filename) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/wptools.rb', line 97 def buzz(filename) from_date, to_date, pvs = read_analytics(filename) post_pvs = [] WpPost.published.where("post_date >= ? AND post_date <= ?", from_date, to_date).order(post_date: "DESC").each do |post| pv = pvs[post.post_name] || 0 post_pvs << [post, pv] end yd = from_date.strftime("%Y/%m") limit = 10 post_pvs.sort{|a, b| b[1] <=> a[1]}.each_with_index do |post_pvs, index| break if index >= limit post, pv = post_pvs print "#{yd},#{index+1},\"#{post.post_title}\",#{pv}\n" end end |
#list(num) ⇒ Object
91 92 93 94 95 |
# File 'lib/wptools.rb', line 91 def list(num) WpPost.published_posts.order(post_date: "DESC").limit(num).each do | post| print "#{post.id} #{post.post_type} #{post.post_date_str} #{post.post_title} #{post.post_name}\n" end end |
#read_analytics(filename) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/wptools.rb', line 114 def read_analytics(filename) p filename # Google Analysticsから出力したページ毎のPVデータを読み込む from_date = nil to_date = nil if filename =~ /(\d\d\d\d)(\d\d)(\d\d)-(\d\d\d\d)(\d\d)(\d\d)\.csv$/ from_date = Time.local($1.to_i, $2.to_i, $3.to_i) to_date = Time.local($4.to_i, $5.to_i, $6.to_i) end if from_date.nil? || to_date.nil? raise "Unknown file name #{filename}" end # slugごとにpv数を保存 pvs = {} CSV.foreach(filename) do |raw| if raw.size == 8 pagename = raw[0] pv = raw[1].gsub(/,/, '').to_i if pagename =~ /\/([^\/]+)\/$/ pvs[$1] = pv end end end return from_date, to_date, pvs end |
#run ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/wptools.rb', line 82 def run if @opts[:c] == 'list' list(@opts[:l]) elsif @opts[:c] == 'buzz' buzz(@opts[:d]) elsif @opts[:c] == 'none' end end |