Module: Noteman::Dropbox

Includes:
Config
Defined in:
lib/noteman/state.rb,
lib/noteman/dropbox.rb

Defined Under Namespace

Classes: Node

Constant Summary collapse

APP_KEY =
''
APP_SECRET =
''

Constants included from Config

Config::NOTEMAN_CONFIG

Instance Attribute Summary

Attributes included from Config

#config

Instance Method Summary collapse

Methods included from Config

#read_from_file

Instance Method Details

#load_stateObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/noteman/state.rb', line 9

def load_state
  if File.exist? state_file
    state = JSON.parse(File.read(state_file), :max_nesting => false)
    state['tree'] = Node.from_json_content(state['tree'])
  else
    state = {
      'tree' => {}
    }

    save_state state
  end
  state
end

#map_hash_values(h) ⇒ Object

Run a mapping function over every value in a Hash, returning a new Hash.



67
68
69
70
71
# File 'lib/noteman/state.rb', line 67

def map_hash_values(h)
  new = {}
  h.each { |k,v| new[k] = yield v }
  new
end

#save_state(state) ⇒ Object



23
24
25
26
27
28
# File 'lib/noteman/state.rb', line 23

def save_state(state)
  state['tree'] = Node.to_json_content(state['tree'])
  File.open(state_file,"w") do |f|
    f.write(JSON.pretty_generate(state, :max_nesting => false))
  end
end

#state_fileObject



5
6
7
# File 'lib/noteman/state.rb', line 5

def state_file
  File.join File.expand_path(config['home']), config['state_file']
end

#syncObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/noteman/dropbox.rb', line 11

def sync
  state = load_state
  # auth
  if not state['access_token']
    web_auth = DropboxOAuth2FlowNoRedirect.new(APP_KEY, APP_SECRET)
    authorize_url = web_auth.start()
    puts "1. Go to: #{authorize_url}"
    puts "2. Click \"Allow\" (you might have to log in first)."
    puts "3. Copy the authorization code."

    print "Enter the authorization code here: "
    STDOUT.flush
    auth_code = STDIN.gets.strip

    access_token, user_id = web_auth.finish(auth_code)
    puts "Link successful."
    state['access_token'] = access_token
  end

  cursor = state['cursor']
  access_token = state['access_token']
  c = DropboxClient.new(access_token)
  result = c.delta cursor, config['dropbox_path']
  pp result
  #save_state state
end