Class: Synclenote::Command

Inherits:
Thor
  • Object
show all
Defined in:
lib/synclenote/command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Command

Returns a new instance of Command.



16
17
18
19
# File 'lib/synclenote/command.rb', line 16

def initialize(*args, &block)
  super
  self.logger = Logger.new(STDOUT)
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



14
15
16
# File 'lib/synclenote/command.rb', line 14

def logger
  @logger
end

Instance Method Details

#initObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/synclenote/command.rb', line 22

def init
  if profile_path.exist?
    $stderr.puts("Profile directory \"\#{profile_path}\" is already exist.\n\nPlease check it and rename/remove it.\n")
    exit(1)
  end
  profile_path.mkpath
  config_path.open("w") do |f|
    f.write("# -*- mode: ruby -*-\n# vi: set ft=ruby :\n\nSynclenote.configure(1) do |config|\n# TODO: Replace your notes directory.\nconfig.local.directory = \"~/notes\"\nconfig.local.pattern = \"**/*.{md,txt}\"\n# You can use either whitelist_tags or blacklist_tags.\n# config.local.whitelist_tags = %w(smartphone tablet)\n# config.local.blacklist_tags = %w(noremote supersecret)\n\nconfig.remote.type = :evernote\n# You must fill your developer token.\n# See https://www.evernote.com/api/DeveloperToken.action\nconfig.remote.developer_token = \"TODO: Write your developer token.\"\nend\n")
  end
  sync_statuses_path.mkpath
  last_sync_path.open("w") do |f|
    f.puts(YAML_HEADER)
    f.puts({last_sync_datetime: Time.at(0)}.to_yaml)
  end
  puts("Creating new profile directory is succeeded.\n\nPlease check TODO in configuration file.\n$ vi \#{config_path}\n")
end

#syncObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/synclenote/command.rb', line 66

def sync
  logger.debug("loading configuration file: %s" % config_path)
  load(config_path)
  c = Synclenote::Configuration.data
  token = c.remote.developer_token
  logger.debug("loaded configuration file: %s" % config_path)

  logger.debug("invoking client.")
  client = EvernoteOAuth::Client.new(token: token,
                                     sandbox: !!c.remote.sandbox)
  logger.debug("invoked client.")

  logger.debug("invoking user_store.")
  user_store = client.user_store
  logger.debug("invoked user_store.")
  if !user_store.version_valid?
    raise "Invalid Evernote API version. Please update evernote_oauth.gem and synclenote.gem."
  end
  logger.debug("done EvernoteOAuth version check.")
  logger.debug("loading note_store.")
  note_store = client.note_store
  logger.debug("loaded note_store.")

  local_top_path = Pathname(c.local.directory).expand_path
  last_sync_status = load_yaml_path(last_sync_path)
  last_sync_datetime = last_sync_status[:last_sync_datetime]
  if Time.now - last_sync_datetime <= min_sync_interval
    allowed_sync_time = last_sync_datetime + min_sync_interval
    raise "too less interval sync after #{allowed_sync_time}."
  end

  processed = []
  Pathname.glob(local_top_path + c.local.pattern) do |note_path|
    relative_note_path = note_path.relative_path_from(local_top_path)
    note_sync_status_path = sync_statuses_path + relative_note_path
    processed << note_sync_status_path

    # create note
    if !note_sync_status_path.exist?
      create_remote_note(token, note_store, note_path, note_sync_status_path)
      next
    end

    # update note
    note_sync_status = load_yaml_path(note_sync_status_path)
    if note_sync_status[:last_sync_datetime] < note_path.mtime
      update_remote_note(token, note_store, note_path, note_sync_status_path,
                         note_sync_status[:guid])
      next
    end

    logger.debug("nothing: %s" % note_path)
  rescue Evernote::EDAM::Error::EDAMUserException => e
    logger.error(e.inspect)
    raise
  end

  # remove note
  removed = Pathname.glob(sync_statuses_path + c.local.pattern) - processed
  removed.each do |note_sync_status_path|
    remove_remote_note(token, note_store, note_sync_status_path)
  end
end