rb-dayone 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,4 +2,5 @@ source :rubygems
2
2
 
3
3
  gem 'builder', '~> 2.0'
4
4
  gem 'commander', '~> 4.1.2'
5
- gem 'libxml-ruby', '~> 2.3.3'
5
+ gem 'libxml-ruby', '~> 2.3.3'
6
+ gem 'nokogiri', '~> 1.5.0'
data/History.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ == 0.4.1 / 2012-11-14
2
+
3
+ * [FIXED] rb-dayone was looking in the wrong place for preferences
4
+ * [FIXED] new preference files not json-parseable. Swapped to XML/Nokogiri
5
+
1
6
  == 0.4.0 / 2012-10-22
2
7
 
3
8
  * Added image support
data/lib/rb-dayone.rb CHANGED
@@ -39,7 +39,7 @@ module DayOne
39
39
  # the DayOne plist file stored in +~/Library/Preferences+.
40
40
  # @return [String] the DayONe journal location
41
41
  def auto_journal_location
42
- @auto_journal_location ||= File.expand_path(plist_reader['NSNavLastRootDirectory'])
42
+ @auto_journal_location ||= File.expand_path(plist_reader.journal_location)
43
43
  end
44
44
 
45
45
  # All DayOne entries, as file names
@@ -1,4 +1,4 @@
1
- require 'json'
1
+ require 'nokogiri'
2
2
 
3
3
  # The PListReader is a class that reads PLists.
4
4
  # It is used to read the DayOne preferences PList.
@@ -10,18 +10,57 @@ class DayOne::PlistReader
10
10
  # Initialize the PList Reader, given a path
11
11
  # @param [String] path the path to the PList; defaults to the standard DayOne preference plist
12
12
  def initialize path=nil
13
- @path = path || File.join(ENV['HOME'], 'Library', 'Preferences', 'com.dayoneapp.dayone.plist')
13
+ default_paths = [ # There have been two default paths over the versions...
14
+ File.join(ENV['HOME'], 'Library', 'Group Containers', '5U8NS4GX82.dayoneapp', 'Data', 'Preferences', 'dayone.plist'), # From 1.7 on
15
+ File.join(ENV['HOME'], 'Library', 'Preferences', 'com.dayoneapp.dayone.plist') # Pre-1.7
16
+ ]
17
+
18
+
19
+ @path = if path
20
+ path
21
+ else
22
+ first_valid_default_path = default_paths.find{ |p| File.exists?(p) }
23
+ if first_valid_default_path.nil?
24
+ raise RuntimeError, "Cannot locate DayOne preference file"
25
+ else
26
+ first_valid_default_path
27
+ end
28
+ end
14
29
  end
15
30
 
16
31
  # Retrieve the body of the plist as an array, parsed through JSON
17
32
  # @return [Hash] the body of the plist as a hash.
18
33
  def body
19
34
  if !@body
20
- json_string = `plutil -convert json -o - '#{path}'`
21
- @body = JSON.parse(json_string)
35
+ @body = Nokogiri::XML `plutil -convert xml1 -o - '#{path}'`
22
36
  end
23
37
  @body
24
38
  end
39
+
40
+ # Retrieves an array of Nokogiri XML objects representing the
41
+ # keys of the plist
42
+ # @return [Array] the keys in the plist
43
+ def keys
44
+ body.xpath('//dict/key')
45
+ end
46
+
47
+ # Retrieves a specific key's value, or nil
48
+ # @return the value of the key, or nil if it doesn't exist
49
+ def key key_value
50
+ key_element = keys.find{ |e| e.content == key_value }
51
+ if key_element
52
+ key_element.next_element.content
53
+ else
54
+ nil
55
+ end
56
+ end
57
+
58
+ # Retrieve the DayOne journal location
59
+ # @return [String] the path to the current DayOne journal location
60
+ def journal_location
61
+ key('JournalPackageURL') || # First, try the 1.7 location...
62
+ key('NSNavLastRootDirectory')
63
+ end
25
64
 
26
65
  # This allows us to access the body's method as well as the reader's.
27
66
  def method_missing sym, *args
data/spec/dayone_spec.rb CHANGED
@@ -18,7 +18,9 @@ describe DayOne do
18
18
  end
19
19
 
20
20
  it "should return a default value when +location+ is 'auto'" do
21
- DayOne.plist_reader = {'NSNavLastRootDirectory' => 'foo'}
21
+ reader = double('ReaderMock')
22
+ reader.should_receive(:journal_location).and_return('foo')
23
+ DayOne.plist_reader = reader
22
24
  DayOne::dayone_folder = location('auto')
23
25
  DayOne::journal_location.should == File.expand_path('foo')
24
26
  end
@@ -8,13 +8,11 @@ describe DayOne::PlistReader do
8
8
  end
9
9
 
10
10
  describe "#body" do
11
- it "should give us the location of the DayOne journal file" do
12
- subject.should have_key('NSNavLastRootDirectory')
13
- end
14
-
15
11
  it "should correctly parse binary plists" do
16
12
  reader = DayOne::PlistReader.new(File.join(File.dirname(__FILE__),'data', 'sample.plist'))
17
- reader['foo'].should == 'bar'
13
+
14
+ reader.key('foo').should == 'bar'
15
+ expect(reader.key('bing')).to be_nil
18
16
  end
19
17
  end
20
18
  end
data/version.txt CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
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.4.0
4
+ version: 0.4.1
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-10-22 00:00:00.000000000 Z
12
+ date: 2012-11-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: builder