sifttter-redux 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,138 @@
1
+ module SifttterRedux
2
+ # ======================================================
3
+ # DBU Module
4
+ #
5
+ # Wrapper module for the Dropbox Uploader project
6
+ # ======================================================
7
+ module DBU
8
+ DEFAULT_MESSAGE = 'RUNNING DROPBOX UPLOADER'
9
+
10
+ # ----------------------------------------------------
11
+ # download method
12
+ #
13
+ # Downloads the files at the remote Dropbox filepath
14
+ # to a filepath on the local filesystem.
15
+ # @return Void
16
+ # ----------------------------------------------------
17
+ def self.download
18
+ if !@local_path.nil? && !@remote_path.nil?
19
+ CliMessage.info_block(@message ||= DEFAULT_MESSAGE, 'Done.', SifttterRedux.verbose) do
20
+ if SifttterRedux.verbose
21
+ system "#{ @dbu } download #{ @remote_path } #{ @local_path }"
22
+ else
23
+ exec = `#{ @dbu } download #{ @remote_path } #{ @local_path }`
24
+ end
25
+ end
26
+ else
27
+ fail ArgumentError, 'Local and remote DBU targets cannot be nil'
28
+ end
29
+ end
30
+
31
+ # ----------------------------------------------------
32
+ # install_wizard method
33
+ #
34
+ # Runs a wizard that installs Dropbox Uploader on the
35
+ # local filesystem.
36
+ # @return Void
37
+ # ----------------------------------------------------
38
+ def self.install_wizard
39
+ valid_directory_chosen = false
40
+
41
+ CliMessage.section_block('CONFIGURING DROPBOX UPLOADER...') do
42
+ until valid_directory_chosen
43
+ # Prompt the user for a location to save Dropbox Uploader. '
44
+ path = CliMessage.prompt('Location for Dropbox-Uploader', DBU_LOCAL_FILEPATH)
45
+ path.chop! if path.end_with?('/')
46
+ path = '/usr/local/opt' if path.empty?
47
+
48
+ # If the entered directory exists, clone the repository.
49
+ if File.directory?(path)
50
+ valid_directory_chosen = true
51
+ path << '/Dropbox-Uploader'
52
+
53
+ if File.directory?(path)
54
+ CliMessage.warning("Using pre-existing Dropbox Uploader at #{ path }...")
55
+ else
56
+ CliMessage.info_block("Downloading Dropbox Uploader to #{ path }...", 'Done.', true) do
57
+ system "git clone https://github.com/andreafabrizi/Dropbox-Uploader.git #{ path }"
58
+ end
59
+ end
60
+
61
+ Configuration.add_section('db_uploader')
62
+ Configuration['db_uploader'].merge!('local_filepath' => path)
63
+ else
64
+ puts "Sorry, but #{ path } isn't a valid directory."
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ # ----------------------------------------------------
71
+ # load method
72
+ #
73
+ # Loads the location of dropbox_uploader.sh.
74
+ # @param dbu_path The local filepath to the script
75
+ # @return Void
76
+ # ----------------------------------------------------
77
+ def self.load(dbu_path)
78
+ @dbu = dbu_path
79
+ end
80
+
81
+ # ----------------------------------------------------
82
+ # set_local_target method
83
+ #
84
+ # Sets the local filesystem path to which files will
85
+ # be downloaded.
86
+ # @param path A local filepath
87
+ # @return Void
88
+ # ----------------------------------------------------
89
+ def self.local_target=(path)
90
+ @local_path = path
91
+ end
92
+
93
+ # ----------------------------------------------------
94
+ # set_message method
95
+ #
96
+ # Sets the message to be displayed just before
97
+ # downloading commences.
98
+ # @param message The string to display
99
+ # @return Void
100
+ # ----------------------------------------------------
101
+ def self.message=(message)
102
+ @message = message
103
+ end
104
+
105
+ # ----------------------------------------------------
106
+ # set_remote_target method
107
+ #
108
+ # Sets the remote Dropbox path from which files will
109
+ # be downloaded.
110
+ # @param path A remote Dropbox filepath
111
+ # @return Void
112
+ # ----------------------------------------------------
113
+ def self.remote_target=(path)
114
+ @remote_path = path
115
+ end
116
+
117
+ # ----------------------------------------------------
118
+ # upload method
119
+ #
120
+ # Uploads the files from the local filesystem path to
121
+ # the remote Dropbox path.
122
+ # @return Void
123
+ # ----------------------------------------------------
124
+ def self.upload
125
+ if !@local_path.nil? && !@remote_path.nil?
126
+ CliMessage.info_block(@message ||= DEFAULT_MESSAGE, 'Done.', SifttterRedux.verbose) do
127
+ if SifttterRedux.verbose
128
+ system "#{ @dbu } upload #{ @local_path } #{ @remote_path }"
129
+ else
130
+ exec = `#{ @dbu } upload #{ @local_path } #{ @remote_path }`
131
+ end
132
+ end
133
+ else
134
+ fail ArgumentError, 'Local and remote DBU targets cannot be nil'
135
+ end
136
+ end
137
+ end
138
+ end
@@ -0,0 +1,65 @@
1
+ # ======================================================
2
+ # String Class
3
+ # ======================================================
4
+ class String
5
+ # ----------------------------------------------------
6
+ # colorize method
7
+ #
8
+ # Outputs a string in a formatted color.
9
+ # @param color_code The code to use
10
+ # @return Void
11
+ # ----------------------------------------------------
12
+ def colorize(color_code)
13
+ "\e[#{color_code}m#{self}\e[0m"
14
+ end
15
+
16
+ # ----------------------------------------------------
17
+ # blue method
18
+ #
19
+ # Convenience method to output a blue string
20
+ # @return Void
21
+ # ----------------------------------------------------
22
+ def blue
23
+ colorize(34)
24
+ end
25
+
26
+ # ----------------------------------------------------
27
+ # green method
28
+ #
29
+ # Convenience method to output a green string
30
+ # @return Void
31
+ # ----------------------------------------------------
32
+ def green
33
+ colorize(32)
34
+ end
35
+
36
+ # ----------------------------------------------------
37
+ # purple method
38
+ #
39
+ # Convenience method to output a purple string
40
+ # @return Void
41
+ # ----------------------------------------------------
42
+ def purple
43
+ colorize(35)
44
+ end
45
+
46
+ # ----------------------------------------------------
47
+ # red method
48
+ #
49
+ # Convenience method to output a red string
50
+ # @return Void
51
+ # ----------------------------------------------------
52
+ def red
53
+ colorize(31)
54
+ end
55
+
56
+ # ----------------------------------------------------
57
+ # yellow method
58
+ #
59
+ # Convenience method to output a yellow string
60
+ # @return Void
61
+ # ----------------------------------------------------
62
+ def yellow
63
+ colorize(33)
64
+ end
65
+ end
@@ -0,0 +1,48 @@
1
+ module SifttterRedux
2
+ # ======================================================
3
+ # OS Module
4
+ #
5
+ # Module to easily find the running operating system
6
+ # ======================================================
7
+ module OS
8
+ # ----------------------------------------------------
9
+ # linux? method
10
+ #
11
+ # Returns true if the host OS is Linux.
12
+ # @return Bool
13
+ # ----------------------------------------------------
14
+ def OS.linux?
15
+ self.unix? and not self.mac?
16
+ end
17
+
18
+ # ----------------------------------------------------
19
+ # mac? method
20
+ #
21
+ # Returns true if the host OS is OS X.
22
+ # @return Bool
23
+ # ----------------------------------------------------
24
+ def OS.mac?
25
+ !(/darwin/ =~ RUBY_PLATFORM).nil?
26
+ end
27
+
28
+ # ----------------------------------------------------
29
+ # unix? method
30
+ #
31
+ # Returns true if the host OS is Unix.
32
+ # @return Bool
33
+ # ----------------------------------------------------
34
+ def OS.unix?
35
+ !self.windows?
36
+ end
37
+
38
+ # ----------------------------------------------------
39
+ # windows? method
40
+ #
41
+ # Returns true if the host OS is Windows
42
+ # @return Bool
43
+ # ----------------------------------------------------
44
+ def OS.windows?
45
+ !(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM).nil?
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,98 @@
1
+ module SifttterRedux
2
+ # ======================================================
3
+ # Sifttter Module
4
+ #
5
+ # Wrapper module for Sifttter itself
6
+ # ======================================================
7
+ module Sifttter
8
+ using SifttterRedux::OS
9
+
10
+ # ----------------------------------------------------
11
+ # run_sifttter method
12
+ #
13
+ # Modified form of Sifttter
14
+ #
15
+ # Sifttter: An IFTTT-to-Day One Logger by Craig Eley
16
+ # Based on tp-dailylog.rb by Brett Terpstra 2012
17
+ # @param date The date to use when scanning Sifttter
18
+ # @return Void
19
+ # ----------------------------------------------------
20
+ def self.run(date)
21
+ uuid_command = 'uuidgen' if OS.mac?
22
+ uuid_command = 'uuid' if OS.linux?
23
+ uuid = %x{#{ uuid_command }}.gsub(/-/, '').strip
24
+
25
+ date_for_title = date.strftime('%B %d, %Y')
26
+ datestamp = date.to_time.utc.iso8601
27
+ starred = false
28
+
29
+ template = ERB.new <<-XMLTEMPLATE
30
+ <?xml version="1.0" encoding="UTF-8"?>
31
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
32
+ <plist version="1.0">
33
+ <dict>
34
+ <key>Creation Date</key>
35
+ <date><%= datestamp %></date>
36
+ <key>Entry Text</key>
37
+ <string><%= entrytext %></string>
38
+ <key>Starred</key>
39
+ <<%= starred %>/>
40
+ <key>Tags</key>
41
+ <array>
42
+ <string>daily logs</string>
43
+ </array>
44
+ <key>UUID</key>
45
+ <string><%= uuid %></string>
46
+ </dict>
47
+ </plist>
48
+ XMLTEMPLATE
49
+
50
+ date_regex = "(?:#{ date.strftime("%B") } 0?#{ date.strftime("%-d") }, #{ date.strftime("%Y") })"
51
+ time_regex = "(?:\d{1,2}:\d{1,2}\s?[AaPpMm]{2})"
52
+
53
+ files = `find #{ Configuration['sifttter_redux']['sifttter_local_filepath'] } -type f -name "*.txt" | grep -v -i daily | sort`
54
+
55
+ projects = []
56
+ files.split("\n").each do |file|
57
+ if File.exists?(file.strip)
58
+ f = File.open(file.strip, encoding: 'UTF-8')
59
+ lines = f.read
60
+ f.close
61
+ project = '### ' + File.basename(file).gsub(/^.*?\/([^\/]+)$/, "\\1") + "\n"
62
+
63
+ found_completed = false
64
+ lines.each_line do |line|
65
+ if line =~ /&/
66
+ line.gsub!(/[&]/, 'and')
67
+ end
68
+ if line =~ /#{ date_regex }/
69
+ found_completed = true
70
+ project += line.gsub(/@done/,"").gsub(/#{ date_regex }\s(-|at)\s/, "").gsub(/#{ time_regex }\s-\s/, "").strip + "\n"
71
+ end
72
+ end
73
+ end
74
+ if found_completed
75
+ projects.push(project)
76
+ end
77
+ end
78
+
79
+ if projects.length <=0
80
+ CliMessage.warning('No entries found...')
81
+ end
82
+
83
+ if projects.length > 0
84
+ entrytext = "# Things done on #{ date_for_title }\n\n"
85
+ projects.each do |project|
86
+ entrytext += project.gsub(/.txt/, ' ') + "\n\n"
87
+ end
88
+
89
+ Dir.mkdir(Configuration['sifttter_redux']['dayone_local_filepath']) if !Dir.exists?(Configuration['sifttter_redux']['dayone_local_filepath'])
90
+
91
+ fh = File.new(File.expand_path(Configuration['sifttter_redux']['dayone_local_filepath'] + '/' + uuid + '.doentry'), 'w+')
92
+ fh.puts template.result(binding)
93
+ fh.close
94
+ CliMessage.success("Entry logged for #{ date_for_title }...")
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,3 @@
1
+ module SifttterRedux
2
+ VERSION = '0.3.2'
3
+ end
@@ -1,5 +1,5 @@
1
1
  # Ensure we require the local version and not one we might have installed already
2
- require File.join([File.dirname(__FILE__), 'lib', 'sifttter-redux', 'constants.rb'])
2
+ require File.join([File.dirname(__FILE__), 'lib', 'sifttter_redux', 'version.rb'])
3
3
 
4
4
  spec = Gem::Specification.new do |s|
5
5
  s.name = 'sifttter-redux'
@@ -9,10 +9,10 @@ spec = Gem::Specification.new do |s|
9
9
  s.homepage = 'https://github.com/bachya/sifttter-redux'
10
10
  s.platform = Gem::Platform::RUBY
11
11
  s.summary = 'Automated IFTTT to Day One engine.'
12
- s.description = 'Sifttter Redux is a modification of Craig Eley\'s Sifttter that allows for smart installation on a standalone *NIX device (such as a Raspberry Pi).'
12
+ s.description = 'A customized IFTTT-to-Day One service that allows for smart installation and automated running on a standalone *NIX device (such as a Raspberry Pi).'
13
13
 
14
- s.files = `git ls-files`.split($/)
15
- s.test_files = `git ls-files -- test`.split($/)
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- test`.split("\n")
16
16
  s.require_paths << 'lib'
17
17
  s.bindir = 'bin'
18
18
  s.executables << 'srd'
@@ -23,6 +23,5 @@ spec = Gem::Specification.new do |s|
23
23
 
24
24
  s.add_development_dependency('rake', '~> 0')
25
25
  s.add_runtime_dependency('chronic', '0.10.2')
26
- s.add_runtime_dependency('colored','1.2')
27
26
  s.add_runtime_dependency('gli','2.9.0')
28
27
  end
@@ -1,106 +1,102 @@
1
- require 'date'
2
- require 'test_helper'
3
- require File.join(File.dirname(__FILE__), '..', 'lib/sifttter-redux/date-range-maker.rb')
1
+ require "date"
2
+ require "test_helper"
3
+ require File.join(File.dirname(__FILE__), "..", "lib/sifttter_redux/date_from_to_maker.rb")
4
4
 
5
5
  class DefaultTest < Test::Unit::TestCase
6
6
 
7
- def setup
8
- $drm = DateRangeMaker.new
9
- end
10
-
11
7
  def test_today
12
- assert_equal($drm.today, (Date.today..Date.today))
8
+ assert_equal(SifttterRedux::DateRangeMaker.today, (Date.today..Date.today))
13
9
  end
14
-
10
+
15
11
  def test_yesterday
16
- assert_equal($drm.yesterday, (Date.today - 1..Date.today - 1))
12
+ assert_equal(SifttterRedux::DateRangeMaker.yesterday, (Date.today - 1..Date.today - 1))
17
13
  end
18
-
14
+
19
15
  def test_last_5_days
20
- assert_equal($drm.last_n_days(5), (Date.today - 5...Date.today))
16
+ assert_equal(SifttterRedux::DateRangeMaker.last_n_days(5), (Date.today - 5...Date.today))
21
17
  end
22
-
18
+
23
19
  def test_last_5_days_include_today
24
- assert_equal($drm.last_n_days(5, {:include_today => true}), (Date.today - 5..Date.today))
20
+ assert_equal(SifttterRedux::DateRangeMaker.last_n_days(5, true), (Date.today - 5..Date.today))
25
21
  end
26
-
22
+
27
23
  def test_last_12_days
28
- assert_equal($drm.last_n_days(12), (Date.today - 12...Date.today))
24
+ assert_equal(SifttterRedux::DateRangeMaker.last_n_days(12), (Date.today - 12...Date.today))
29
25
  end
30
-
26
+
31
27
  def test_last_12_days_include_today
32
- assert_equal($drm.last_n_days(12, {:include_today => true}), (Date.today - 12..Date.today))
28
+ assert_equal(SifttterRedux::DateRangeMaker.last_n_days(12, true), (Date.today - 12..Date.today))
33
29
  end
34
-
30
+
35
31
  def test_current_week
36
32
  end_date = Date.today - Date.today.wday + 7
37
33
  if end_date > Date.today
38
34
  end_date = Date.today
39
35
  end
40
-
41
- assert_equal($drm.last_n_weeks, (Date.today - Date.today.wday + 1...end_date))
36
+
37
+ assert_equal(SifttterRedux::DateRangeMaker.last_n_weeks, (Date.today - Date.today.wday + 1...end_date))
42
38
  end
43
-
39
+
44
40
  def test_current_week_include_today
45
41
  end_date = Date.today - Date.today.wday + 7
46
42
  if end_date > Date.today
47
43
  end_date = Date.today
48
44
  end
49
-
50
- assert_equal($drm.last_n_weeks(0, {:include_today => true}), (Date.today - Date.today.wday + 1..end_date))
45
+
46
+ assert_equal(SifttterRedux::DateRangeMaker.last_n_weeks(0, true), (Date.today - Date.today.wday + 1..end_date))
51
47
  end
52
-
48
+
53
49
  def test_last_2_weeks
54
50
  end_date = Date.today - Date.today.wday + 7
55
51
  if end_date > Date.today
56
52
  end_date = Date.today
57
53
  end
58
-
59
- assert_equal($drm.last_n_weeks(2), (Date.today - Date.today.wday - 13...end_date))
54
+
55
+ assert_equal(SifttterRedux::DateRangeMaker.last_n_weeks(2), (Date.today - Date.today.wday - 13...end_date))
60
56
  end
61
-
57
+
62
58
  def test_last_2_weeks_include_today
63
59
  end_date = Date.today - Date.today.wday + 7
64
60
  if end_date > Date.today
65
61
  end_date = Date.today
66
62
  end
67
-
68
- assert_equal($drm.last_n_weeks(2, {:include_today => true}), (Date.today - Date.today.wday - 13..end_date))
63
+
64
+ assert_equal(SifttterRedux::DateRangeMaker.last_n_weeks(2, true), (Date.today - Date.today.wday - 13..end_date))
69
65
  end
70
-
71
- def test_range_only_start_date
72
- assert_equal($drm.range("2014-02-01", nil), (Date.parse("2014-02-01")...Date.today))
66
+
67
+ def test_from_to_only_start_date
68
+ assert_equal(SifttterRedux::DateRangeMaker.from_to("2014-02-01", nil), (Date.parse("2014-02-01")...Date.today))
73
69
  end
74
-
75
- def test_range_only_start_date_include_today
76
- assert_equal($drm.range("2014-02-01", nil, {:include_today => true}), (Date.parse("2014-02-01")..Date.today))
70
+
71
+ def test_from_to_only_start_date_include_today
72
+ assert_equal(SifttterRedux::DateRangeMaker.from_to("2014-02-01", nil, true), (Date.parse("2014-02-01")..Date.today))
77
73
  end
78
-
79
- def test_range_start_date_and_end_date
80
- assert_equal($drm.range("2014-02-01", "2014-02-05"), (Date.parse("2014-02-01")..Date.parse("2014-02-05")))
74
+
75
+ def test_from_to_start_date_and_end_date
76
+ assert_equal(SifttterRedux::DateRangeMaker.from_to("2014-02-01", "2014-02-05"), (Date.parse("2014-02-01")..Date.parse("2014-02-05")))
81
77
  end
82
-
83
- def test_range_bad_dates
84
- assert_raise BadChronicDateError do
85
- $drm.range("Bad Start Date", "Bad End Date")
78
+
79
+ def test_from_to_bad_dates
80
+ assert_raise ArgumentError do
81
+ SifttterRedux::DateRangeMaker.from_to("Bad Start Date", "Bad End Date")
86
82
  end
87
83
  end
88
-
89
- def test_range_end_date_with_no_start_date
90
- assert_raise InvalidFlagsError do
91
- $drm.range(nil, Date.today)
84
+
85
+ def test_from_to_end_date_with_no_start_date
86
+ assert_raise ArgumentError do
87
+ SifttterRedux::DateRangeMaker.from_to(nil, Date.today)
92
88
  end
93
89
  end
94
-
95
- def test_range_end_date_before_start_date
96
- assert_raise BadDateOrderError do
97
- $drm.range(Date.today, Date.today - 1)
90
+
91
+ def test_from_to_end_date_before_start_date
92
+ assert_raise ArgumentError do
93
+ SifttterRedux::DateRangeMaker.from_to(Date.today, Date.today - 1)
98
94
  end
99
95
  end
100
-
101
- def test_range_negative_look_back
102
- assert_raise NegativeDaysError do
103
- $drm.last_n_days(-5)
96
+
97
+ def test_from_to_negative_look_back
98
+ assert_raise ArgumentError do
99
+ SifttterRedux::DateRangeMaker.last_n_days(-5)
104
100
  end
105
101
  end
106
102
  end