Class: Simplenote
Constant Summary collapse
- APP_ID =
"chalk-bump-f49"
- API_KEY =
Base64.decode64("YzhjMmI4NjMzNzE1NGNkYWJjOTg5YjIzZTMwYzZiZjQ=")
- URI_API =
"https://api.simperium.com/1/" + APP_ID + "/note/"
- URI_AUTH =
"https://auth.simperium.com/1/" + APP_ID
Instance Method Summary collapse
- #authenticate ⇒ Object
- #create_note(note_or_string) ⇒ Object
- #delete_note(note) ⇒ Object
- #get_index(mark = nil, note_list = []) ⇒ Object
- #get_note(key) ⇒ Object
- #get_token ⇒ Object
- #headers_for_api ⇒ Object
-
#initialize(email, password) ⇒ Simplenote
constructor
A new instance of Simplenote.
- #trash_note(note) ⇒ Object
- #update_note(note) ⇒ Object
Constructor Details
#initialize(email, password) ⇒ Simplenote
Returns a new instance of Simplenote.
21 22 23 24 25 |
# File 'lib/simplenote_ruby.rb', line 21 def initialize(email, password) @email = email @password = password @token = nil end |
Instance Method Details
#authenticate ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/simplenote_ruby.rb', line 35 def authenticate res = Http::Exceptions.wrap_and_check do headers = { "X-Simperium-API-Key" => API_KEY, "User-Agent" => "simplenote.rb" } body = { "username" => @email, "password" => @password }.to_json self.class.base_uri(URI_AUTH) self.class.post("/authorize/", headers: headers, body: body, format: :json) end res["access_token"] rescue Http::Exceptions::HttpException => e puts "Login error: " + e. end |
#create_note(note_or_string) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/simplenote_ruby.rb', line 89 def create_note(note_or_string) if note_or_string.is_a?(String) note = { "creationDate" => Time.now.to_f, "deleted" => false, "content" => note_or_string, "tags" => [], "systemTags" => [], "shareURL" => "", "publishURL" => "" } end note["key"] = UUID.generate(:compact) update_note(note) end |
#delete_note(note) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/simplenote_ruby.rb', line 103 def delete_note(note) return false unless get_token # Note has to be trashed before delettion note = trash_note(note) key = note["key"] Http::Exceptions.wrap_and_check do self.class.base_uri(URI_API) self.class.delete("/i/#{key}", headers: headers_for_api) end return true rescue Http::Exceptions::HttpException => e puts 'Delete note error: ' + e. end |
#get_index(mark = nil, note_list = []) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/simplenote_ruby.rb', line 47 def get_index(mark = nil, note_list = []) return nil unless get_token res = Http::Exceptions.wrap_and_check do params = { "data" => true, "limit" => 100 } params.merge!("mark" => mark) unless mark.nil? self.class.base_uri(URI_API) self.class.get("/index", headers: headers_for_api, query: params, format: :json) end note_list.concat(res["index"]) res.key?("mark") ? get_index(res["mark"], note_list) : note_list rescue Http::Exceptions::HttpException => e puts "Get index error: " + e. end |
#get_note(key) ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/simplenote_ruby.rb', line 61 def get_note(key) return nil unless get_token res = Http::Exceptions.wrap_and_check do self.class.base_uri(URI_API) self.class.get("/i/#{key}", headers: headers_for_api, format: :json) end res.merge!("key" => key, "version" => res.headers["X-Simperium-Version"].to_i) rescue Http::Exceptions::HttpException => e puts "Get note #{key} error: " + e. end |
#get_token ⇒ Object
27 28 29 |
# File 'lib/simplenote_ruby.rb', line 27 def get_token @token ||= authenticate end |
#headers_for_api ⇒ Object
31 32 33 |
# File 'lib/simplenote_ruby.rb', line 31 def headers_for_api { "X-Simperium-Token" => get_token, "Content-Type" => "application/json" } end |
#trash_note(note) ⇒ Object
98 99 100 101 |
# File 'lib/simplenote_ruby.rb', line 98 def trash_note(note) note["deleted"] = true update_note(note) end |
#update_note(note) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/simplenote_ruby.rb', line 72 def update_note(note) return nil unless get_token key = note.delete("key") version = note.delete("version") note["modificationDate"] = Time.now.to_f res = Http::Exceptions.wrap_and_check do params = { "response" => true } self.class.base_uri(URI_API) self.class.post("/i/#{key}" + (version ? "/v/#{version.to_s}" : ""), headers: headers_for_api, query: params, body: note.to_json.unicode_escape, format: :json) end res.merge!("key" => key, "version" => res.headers["X-Simperium-Version"].to_i) rescue Http::Exceptions::HttpException => e puts 'Update note error: ' + e. end |