rb-dayone 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc CHANGED
@@ -1,3 +1,7 @@
1
+ == 0.4.0 / 2012-10-22
2
+
3
+ * Added image support
4
+
1
5
  == 0.3.3 / 2012-08-17
2
6
 
3
7
  * [FIXED] LibXML will now accept UTF-8 characters in journal entries.
data/bin/dayone CHANGED
@@ -42,23 +42,40 @@ Note: setting location to "auto" will set it from the DayOne plist file.
42
42
  end
43
43
 
44
44
  command :add do |c|
45
- c.syntax = "add [--text=\"Entry text\"] [--starred]"
45
+ c.syntax = "add [--text \"Entry text\"] [--image path/to/image.jpg] [--starred]"
46
46
  c.description = "Add an entry to your DayOne journal."
47
47
  c.summary = <<-end
48
48
  Add an entry to your DayOne journal. By default will add an unstarred entry - use the --starred flag to change this.
49
49
 
50
- If you don't specify the --text tag, it will read text from STDIN and use this as the journal entry.
50
+ If you don't specify the --text or --image tag, it will read text from STDIN and use this as the journal entry.
51
51
  end
52
52
 
53
53
  c.option "--text STRING", String, "Specify the journal entry text. If not specified, will read from STDIN"
54
54
  c.option "--starred", "Mark the entry starrd"
55
+ c.option "--image path/to/image.jpg", "Add an image to the entry"
55
56
 
56
57
  c.action do |args, opts|
57
58
  require 'rb-dayone'
58
- entry_text = opts.text || $stdin.read.strip
59
- starred = opts.starred
59
+ entry = DayOne::Entry.new
60
+
61
+ needs_stdin_help = !(opts.image || opts.text)
62
+ if needs_stdin_help
63
+ entry.entry_text = $stdin.read.strip
64
+ else
65
+ entry.entry_text = opts.text if opts.text
66
+
67
+ if opts.image
68
+ if File.exists? opts.image
69
+ entry.image = opts.image
70
+ else
71
+ puts "Can't find image: #{opts.image}"
72
+ exit 1
73
+ end
74
+ end
75
+ end
76
+
77
+ entry.starred = opts.starred
60
78
 
61
- entry = DayOne::Entry.new entry_text, starred:starred
62
79
  entry.create!
63
80
  end
64
81
  end
@@ -1,8 +1,12 @@
1
1
  require 'libxml'
2
+ require 'fileutils'
2
3
 
3
4
  # A text-only journal entry for DayOne.
4
5
  class DayOne::Entry
5
6
 
7
+ # A list of image extensions allowed for attached images
8
+ ALLOWED_IMAGES = ['.jpg','.jpeg']
9
+
6
10
  # The date of the journal entry
7
11
  attr_accessor :creation_date
8
12
 
@@ -14,6 +18,9 @@ class DayOne::Entry
14
18
 
15
19
  # Whether the entry has been saved to file at all.
16
20
  attr_accessor :saved
21
+
22
+ # Path to the entry image
23
+ attr_accessor :image
17
24
 
18
25
  # The PList doctype, used for XML export
19
26
  DOCTYPE = [:DOCTYPE, :plist, :PUBLIC, "-//Apple//DTD PLIST 1.0//EN", "http://www.apple.com/DTDs/PropertyList-1.0.dtd"]
@@ -76,13 +83,18 @@ class DayOne::Entry
76
83
  end
77
84
 
78
85
  # Create a .doentry file with this entry.
79
- # This uses the #to_xml method to generate
80
- # the entry proper.
86
+ # This uses the #to_xml method to generate the entry proper.
87
+ # It will also relocate its attached image, if required.
81
88
  # @return [Boolean] true if the operation was successful.
82
89
  def create!
83
90
  xml = self.to_xml
84
91
  file_location = File.join(DayOne::journal_location,'entries',"#{uuid}.doentry")
85
92
  File.open(file_location,'w'){ |io| io << xml }
93
+ if image
94
+ new_image_path = File.join(DayOne::journal_location, 'photos', "#{uuid}.jpg")
95
+ FileUtils.cp(image, new_image_path)
96
+ @image = new_image_path
97
+ end
86
98
  return true
87
99
  end
88
100
 
@@ -97,4 +109,18 @@ class DayOne::Entry
97
109
  return true
98
110
  end
99
111
  end
112
+
113
+ # Assign an image to the entry
114
+ # For now, this will only accept jpeg images (extension is 'jpg' or 'jpeg', case-insensitive)
115
+ # Later, may support conversion via the appropriate library
116
+ # @param image_path [String] the path to the image
117
+ def image= image_path
118
+ if !File.exists?(image_path)
119
+ raise RuntimeError, "Tried to link a journal entry to the image #{image_path}, but it doesn't exist."
120
+ elsif image_path =~ /\.[^.]+$/ && ALLOWED_IMAGES.include?($&.downcase)
121
+ @image = image_path
122
+ else
123
+ raise RuntimeError, "Tried to link a journal entry to the image #{image_path}, but it's not a supported image format (#{ALLOWED_IMAGES.join(", ")})."
124
+ end
125
+ end
100
126
  end
data/spec/entry_spec.rb CHANGED
@@ -3,11 +3,11 @@ require 'fileutils'
3
3
 
4
4
  describe DayOne::Entry do
5
5
  before :each do
6
- FileUtils::mkdir_p spec_data('working/entries')
6
+ setup_working
7
7
  end
8
8
 
9
9
  after :each do
10
- FileUtils::rm_rf spec_data('working')
10
+ clean_working
11
11
  end
12
12
 
13
13
  describe "#to_xml" do
@@ -34,7 +34,6 @@ describe DayOne::Entry do
34
34
  describe "#create!" do
35
35
  it "should correctly create a .doentry file" do
36
36
 
37
- DayOne::journal_location = spec_data('working')
38
37
 
39
38
  e = subject
40
39
  e.entry_text = "Hello, world!"
data/spec/spec_helper.rb CHANGED
@@ -2,4 +2,14 @@ require './lib/rb-dayone'
2
2
 
3
3
  def spec_data *path
4
4
  File.join(File.dirname(__FILE__), 'data', *path)
5
+ end
6
+
7
+ def setup_working
8
+ FileUtils::mkdir_p spec_data('working/entries')
9
+ FileUtils::mkdir_p spec_data('working/photos')
10
+ DayOne::journal_location = spec_data('working')
11
+ end
12
+
13
+ def clean_working
14
+ FileUtils::rm_rf spec_data('working')
5
15
  end
data/version.txt CHANGED
@@ -1 +1 @@
1
- 0.3.3
1
+ 0.4.0
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rb-dayone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-17 00:00:00.000000000 Z
12
+ date: 2012-10-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder