Method: NotehubAPI#update_note

Defined in:
lib/notehub/notehub.rb

#update_note(id, text, pass = false) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/notehub/notehub.rb', line 178

def update_note(id, text, pass=false)
  note = @notes['notes'][id]
  if note.has_key?('file')
    if text.nil? || text == "use_previous"
      file = File.expand_path(note['file'])
      if File.exists?(file)
        unless text == "use_previous"
          puts "File: #{file}"
          res = ask("Read from file? (Y/n) ", String)
          if res.strip =~ /^y?$/i
            text = IO.read(file)
          end
        else
          text = IO.read(file)
        end
      end
    end
  end

  return false unless text

  params = {}
  pass ||= @default_password
  # raise "Password required for update" unless pass

  md5_pass = Digest::MD5.hexdigest(pass)

  params['password'] =  md5_pass
  params['noteID'] = id
  params['note'] = text.strip
  params['pid'] = @pid
  sig = @pid + @psk + id + text.strip + md5_pass
  params['signature'] = Digest::MD5.hexdigest(sig)
  params['version'] = API_VERSION
  res = post_api(params,"put")

  if res && res['status']['success']
    note_data = read_note(id)
    note = {
      'title' => note_data['title'][0..80].strip,
      'id' => id,
      'url' => res['longURL'],
      'short' => res['shortURL'],
      'stats' => note_data['statistics'],
      'pass' => pass || ""
    }
    store_note(note)
    return note
  else
    if res
      raise "Failed: #{res['status']['comment']} "
    else
      raise "Failed to update note"
    end
  end
end