Class: NotehubAPI
- Inherits:
-
Object
- Object
- NotehubAPI
- Defined in:
- lib/notehub/notehub.rb
Constant Summary collapse
- API_VERSION =
"1.4"
Instance Attribute Summary collapse
-
#default_font ⇒ Object
readonly
Returns the value of attribute default_font.
-
#default_header_font ⇒ Object
readonly
Returns the value of attribute default_header_font.
-
#default_password ⇒ Object
readonly
Returns the value of attribute default_password.
-
#default_theme ⇒ Object
readonly
Returns the value of attribute default_theme.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
Instance Method Summary collapse
- #choose_note(term = ".*") ⇒ Object
- #find_notes(term = ".*") ⇒ Object
- #get_api(params) ⇒ Object
-
#initialize(opts = {}) ⇒ NotehubAPI
constructor
A new instance of NotehubAPI.
- #list_notes(term = ".*") ⇒ Object
- #new_note(text, pass = false, options = {}) ⇒ Object
- #post_api(params, action = "post") ⇒ Object
- #read_note(id) ⇒ Object
- #store_note(note) ⇒ Object
- #tag_file(id, short) ⇒ Object
- #update_note(id, text, pass = false) ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ NotehubAPI
Returns a new instance of NotehubAPI.
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/notehub/notehub.rb', line 31 def initialize(opts={}) opts['config_location'] ||= "#{ENV['HOME']}/.notehub" opts['config_file'] ||= "#{opts['config_location']}/config.yml" opts['notes_db'] ||= "#{opts['config_location']}/notes.db" config_file = opts['config_file'] @notes_db = opts['notes_db'] # Set up config FileUtils.mkdir_p(opts['config_location'],:mode => 0755) unless File.directory? opts['config_location'] unless File.exists?(config_file) new_config = { 'publisher_id' => "your_publisher_id", 'secret_key' => "your_secret_key", 'default_password' => "default password for editing notes", 'default_theme' => "light", 'default_font' => 'Georgia' } File.open(config_file, 'w') { |yf| YAML::dump(new_config, yf) } end config = YAML.load_file(config_file) @pid = config['publisher_id'] @psk = config['secret_key'] if config['default_password'] && config['default_password'].length > 0 @default_password = config['default_password'] else @default_password = false end @default_theme = config['default_theme'] || 'light' @default_font = config['default_font'] || 'Georgia' @default_header_font = config['default_header_font'] || 'Georgia' # verify config if @pid == "your_publisher_id" || @psk == "your_secret_key" puts "Please edit #{config_file} and run again" Process.exit 1 end # set up notes database unless File.exists?(@notes_db) new_db = {'notes' => {}} File.open(@notes_db, 'w') { |yf| YAML::dump(new_db, yf) } end # load existing notes @notes = YAML.load_file(@notes_db) end |
Instance Attribute Details
#default_font ⇒ Object (readonly)
Returns the value of attribute default_font.
29 30 31 |
# File 'lib/notehub/notehub.rb', line 29 def default_font @default_font end |
#default_header_font ⇒ Object (readonly)
Returns the value of attribute default_header_font.
29 30 31 |
# File 'lib/notehub/notehub.rb', line 29 def default_header_font @default_header_font end |
#default_password ⇒ Object (readonly)
Returns the value of attribute default_password.
29 30 31 |
# File 'lib/notehub/notehub.rb', line 29 def default_password @default_password end |
#default_theme ⇒ Object (readonly)
Returns the value of attribute default_theme.
29 30 31 |
# File 'lib/notehub/notehub.rb', line 29 def default_theme @default_theme end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
29 30 31 |
# File 'lib/notehub/notehub.rb', line 29 def notes @notes end |
Instance Method Details
#choose_note(term = ".*") ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/notehub/notehub.rb', line 257 def choose_note(term=".*") puts "Choose a note:" list = find_notes(term) list.each_with_index { |note, i| puts "% 3d: %s" % [i+1, note['title']] } # list.each_with_index { |f,i| puts "% 3d: %s" % [i+1, f] } num = ask("Which note? ", Integer) { |q| q.in = 1..list.length } return false if num =~ /^[a-z ]*$/i list[num.to_i - 1] end |
#find_notes(term = ".*") ⇒ Object
247 248 249 250 251 252 253 254 255 |
# File 'lib/notehub/notehub.rb', line 247 def find_notes(term=".*") term.gsub!(/\s+/,".*?") found_notes = [] @notes['notes'].each {|k, v| v['id'] = k found_notes.push(v) if v['title'] =~ /#{term}/i } found_notes end |
#get_api(params) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/notehub/notehub.rb', line 117 def get_api(params) params['version'] = API_VERSION uri = URI("http://www.notehub.org/api/note#{params.to_query}") Net::HTTP.start(uri.host, uri.port) do |http| req = Net::HTTP::Get.new uri res = http.request req if res && res.code == "200" json = JSON.parse(res.body) if json['status']['success'] return json else raise "GET request returned error: #{json['status']['message']}" end else p res.body if res raise "Error retrieving GET request to API" end end end |
#list_notes(term = ".*") ⇒ Object
240 241 242 243 244 245 |
# File 'lib/notehub/notehub.rb', line 240 def list_notes(term=".*") notes = find_notes(term) notes.each {|note| puts "> #{note['title']} [ #{note['id']} ]" } end |
#new_note(text, pass = false, options = {}) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/notehub/notehub.rb', line 139 def new_note(text, pass=false, ={}) [:theme] ||= nil [:font] ||= nil params = {} params['note'] = text.strip params['pid'] = @pid params['signature'] = Digest::MD5.hexdigest(@pid + @psk + text.strip) params['password'] = Digest::MD5.hexdigest(pass) if pass params['version'] = API_VERSION params['theme'] = [:theme] unless [:theme].nil? params['text-font'] = [:font] unless [:font].nil? params['header-font'] = [:header_font] unless [:header_font].nil? res = post_api(params) if res && res['status']['success'] note_data = read_note(res['noteID']) note = { 'title' => note_data['title'][0..80], 'id' => res['noteID'], 'url' => res['longURL'], 'short' => res['shortURL'], 'stats' => note_data['statistics'], 'pass' => pass ? pass : "", 'file' => [:file] ? [:file] : "" } store_note(note) return note else if res raise "Failed: #{res['status']['comment']} " else raise "Failed to create note" end end end |
#post_api(params, action = "post") ⇒ Object
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 |
# File 'lib/notehub/notehub.rb', line 87 def post_api(params, action="post") uri = URI("http://www.notehub.org/api/note") if action == "put" req = Net::HTTP::Put.new(uri) else req = Net::HTTP::Post.new(uri) end req.set_form_data(params) res = Net::HTTP.start(uri.hostname, uri.port) do |http| http.request(req) end case res when Net::HTTPSuccess, Net::HTTPRedirection json = JSON.parse(res.body) if json['status']['success'] return json else raise "POST request returned error: #{json['status']['message']}" end else # res.value p res.body if res raise "Error retrieving POST request to API" end # res = Net::HTTP.post_form(uri, params) end |
#read_note(id) ⇒ Object
235 236 237 238 |
# File 'lib/notehub/notehub.rb', line 235 def read_note(id) params = {'noteID' => id} get_api(params) end |
#store_note(note) ⇒ Object
82 83 84 85 |
# File 'lib/notehub/notehub.rb', line 82 def store_note(note) @notes['notes'][note['id']] = note File.open(@notes_db, 'w') { |yf| YAML::dump(@notes, yf) } end |
#tag_file(id, short) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/notehub/notehub.rb', line 271 def tag_file(id,short) if @notes['notes'].has_key?([id]) note = @notes['notes'][id] file = note.has_key?('file') ? note['file'] : false return false unless file content = IO.read(File.(file)) output = "\n\n<!-- <#{short}> #{id} -->\n" File.open(File.(file),'w+') do |f| f.puts content f.puts output end end end |
#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.(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 |