Class: Bookmarkeron::Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/bookmarkeron/merger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Merger

Returns a new instance of Merger.



10
11
12
13
# File 'lib/bookmarkeron/merger.rb', line 10

def initialize(opts = {})
  @target = opts[:target] || CHROME_BOOKMARKS_PATH
  @source = opts[:source]
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



8
9
10
# File 'lib/bookmarkeron/merger.rb', line 8

def target
  @target
end

Instance Method Details

#bookmarks_from_sourceObject



15
16
17
18
# File 'lib/bookmarkeron/merger.rb', line 15

def bookmarks_from_source
  yaml = YAML.load_file @source
  yaml["bookmarks"]
end

#bookmarks_from_targetObject



40
41
42
# File 'lib/bookmarkeron/merger.rb', line 40

def bookmarks_from_target
  @target_json ||= JSON.parse(File.read(@target))
end

#resultObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bookmarkeron/merger.rb', line 20

def result
  bookmarks = bookmarks_from_target["roots"]["bookmark_bar"]["children"]
  bookmark_next_id = bookmarks_from_source.size
  bookmarks_from_source.each do |bookmark|
    next if bookmarks.find {|existing| existing["url"] == bookmark["url"]}
    bookmarks << bookmark.merge({
      "type"=> "url",
      "date" => "13010285566708500",
      "id" => bookmark_next_id
    })

    bookmark_next_id = bookmark_next_id + 1
  end
  bookmarks_from_target
end

#result_jsonObject



36
37
38
# File 'lib/bookmarkeron/merger.rb', line 36

def result_json
  JSON.pretty_generate result
end