marginalia-io 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +62 -0
- data/Rakefile +2 -0
- data/bin/marginalia +13 -0
- data/lib/marginalia-io/api.rb +44 -0
- data/lib/marginalia-io/auth.rb +70 -0
- data/lib/marginalia-io/cli.rb +107 -0
- data/lib/marginalia-io/version.rb +5 -0
- data/lib/marginalia-io.rb +5 -0
- data/marginalia-io.gemspec +21 -0
- metadata +107 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Pete Keen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Marginalia CLI
|
2
|
+
|
3
|
+
[Marginalia](https://www.marginalia.io/) is a web-based journaling and
|
4
|
+
note taking app using Markdown. It also has a full featured [RESTful API](https://www.marginalia.io/apidocs), and this is the Ruby API client for it, as well as
|
5
|
+
an example command line application.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'marginalia-io'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install marginalia-io
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```
|
24
|
+
Tasks:
|
25
|
+
marginalia append ID # Append to the given note
|
26
|
+
marginalia edit ID # Edit the given note in the editor
|
27
|
+
marginalia help [TASK] # Describe available tasks or one specific task
|
28
|
+
marginalia list # List all notes
|
29
|
+
marginalia login # Login to Marginalia
|
30
|
+
marginalia logout # Logout of Marginalia
|
31
|
+
marginalia search QUERY # List all notes matching a given query
|
32
|
+
marginalia show ID # Show the given note in the pager
|
33
|
+
|
34
|
+
Options:
|
35
|
+
[--host=HOST] # Marginalia hostname
|
36
|
+
# Default: www.marginalia.io
|
37
|
+
```
|
38
|
+
|
39
|
+
The API is pretty simple right now:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
require 'marginalia-io'
|
43
|
+
|
44
|
+
api = Marginalia::IO::API.new # will prompt for credentials if not logged in
|
45
|
+
|
46
|
+
all = api.all # Get all of the notes
|
47
|
+
blurb = api.search('blurb') # Get all of the notes matching 'blurb'
|
48
|
+
note_5 = api.get(5) # Get the note with id 5
|
49
|
+
|
50
|
+
api.append(5, "Hi There") # Append "Hi There" with a timestamp to the end of not 5
|
51
|
+
|
52
|
+
api.update(5, "Hello There") # replace the body of note 5 with "Hello There"
|
53
|
+
```
|
54
|
+
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/marginalia
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "rubygems"
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'marginalia-io'
|
7
|
+
rescue LoadError => e
|
8
|
+
path = File.expand_path '../../lib', __FILE__
|
9
|
+
$:.unshift(path) if File.directory?(path) && !$:.include?(path)
|
10
|
+
require 'marginalia-io'
|
11
|
+
end
|
12
|
+
|
13
|
+
Marginalia::IO::CLI.start()
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'httparty'
|
3
|
+
require 'netrc'
|
4
|
+
require 'uri'
|
5
|
+
|
6
|
+
module Marginalia
|
7
|
+
module IO
|
8
|
+
|
9
|
+
class NetRCMissingError < StandardError; end
|
10
|
+
|
11
|
+
class API
|
12
|
+
|
13
|
+
include HTTParty
|
14
|
+
|
15
|
+
def initialize(host='www.marginalia.io')
|
16
|
+
credentials = Marginalia::IO::Auth.new(host).get_credentials
|
17
|
+
self.class.base_uri "https://#{host}"
|
18
|
+
self.class.basic_auth credentials[0], credentials[1]
|
19
|
+
end
|
20
|
+
|
21
|
+
def append(id, body)
|
22
|
+
self.class.post("/notes/#{id}/append.json", :body => {:body => body})
|
23
|
+
end
|
24
|
+
|
25
|
+
def all
|
26
|
+
self.class.get("/notes.json").parsed_response
|
27
|
+
end
|
28
|
+
|
29
|
+
def search(query)
|
30
|
+
escaped = URI.escape(query)
|
31
|
+
self.class.get("/notes/search.json?q=#{escaped}").parsed_response
|
32
|
+
end
|
33
|
+
|
34
|
+
def get(id)
|
35
|
+
self.class.get("/notes/#{id}.json")
|
36
|
+
end
|
37
|
+
|
38
|
+
def update(id, body)
|
39
|
+
puts self.class.put("/notes/#{id}.json", :body => {:note => {:body => body}}).parsed_response
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'netrc'
|
2
|
+
|
3
|
+
class Marginalia::IO::Auth
|
4
|
+
attr_accessor :credentials, :host
|
5
|
+
|
6
|
+
def initialize(host="www.marginalia.io")
|
7
|
+
@host = host
|
8
|
+
end
|
9
|
+
|
10
|
+
def netrc_path
|
11
|
+
default = Netrc.default_path
|
12
|
+
encrypted = default + ".gpg"
|
13
|
+
if File.exists?(encrypted)
|
14
|
+
encrypted
|
15
|
+
else
|
16
|
+
default
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def get_credentials # :nodoc:
|
21
|
+
@credentials ||= (read_credentials || login)
|
22
|
+
end
|
23
|
+
|
24
|
+
def netrc # :nodoc:
|
25
|
+
@netrc ||= begin
|
26
|
+
File.exists?(netrc_path) && Netrc.read(netrc_path)
|
27
|
+
rescue => error
|
28
|
+
if error.message =~ /^Permission bits for/
|
29
|
+
perm = File.stat(netrc_path).mode & 0777
|
30
|
+
abort("Permissions #{perm} for '#{netrc_path}' are too open. You should run `chmod 0600 #{netrc_path}` so that your credentials are NOT accessible by others.")
|
31
|
+
else
|
32
|
+
raise error
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def delete_credentials
|
38
|
+
if netrc
|
39
|
+
netrc.delete(host)
|
40
|
+
netrc.save
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def read_credentials
|
45
|
+
if netrc
|
46
|
+
netrc[host]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def write_credentials
|
51
|
+
FileUtils.mkdir_p(File.dirname(netrc_path))
|
52
|
+
FileUtils.touch(netrc_path)
|
53
|
+
FileUtils.chmod(0600, netrc_path)
|
54
|
+
netrc[host] = self.credentials
|
55
|
+
netrc.save
|
56
|
+
end
|
57
|
+
|
58
|
+
def login
|
59
|
+
print "Email address: "
|
60
|
+
username = gets
|
61
|
+
print "Password: "
|
62
|
+
system "stty -echo"
|
63
|
+
password = gets
|
64
|
+
system "stty echo"
|
65
|
+
|
66
|
+
self.credentials = [username, password]
|
67
|
+
write_credentials
|
68
|
+
self.credentials
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'open3'
|
3
|
+
|
4
|
+
class Marginalia::IO::CLI < Thor
|
5
|
+
include Thor::Actions
|
6
|
+
|
7
|
+
class_option :host, :type => :string, :desc => "Marginalia hostname", :default => 'www.marginalia.io'
|
8
|
+
|
9
|
+
desc "login", "Login to Marginalia"
|
10
|
+
def login
|
11
|
+
auth = Marginalia::IO::Auth.new(options[:host])
|
12
|
+
auth.delete_credentials
|
13
|
+
auth.login
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "logout", "Logout of Marginalia"
|
17
|
+
def logout
|
18
|
+
auth = Marginalia::IO::Auth.new(options[:host])
|
19
|
+
auth.delete_credentials
|
20
|
+
end
|
21
|
+
|
22
|
+
desc "list", "List all notes"
|
23
|
+
def list
|
24
|
+
api.all.sort_by{ |n| n['updated_at'] }.reverse.each do |note|
|
25
|
+
puts "#{note['id'].to_s.rjust(4)} #{note['updated_at'][0,10]} #{note['title']}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
desc "search QUERY", "List all notes matching a given query"
|
30
|
+
def search(query)
|
31
|
+
api.search(query).sort_by{|n|n['updated_at']}.reverse.each do |note|
|
32
|
+
puts "#{note['id'].to_s.rjust(4)} #{note['updated_at'][0,10]} #{note['title']}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "show ID", "Show the given note in the pager"
|
37
|
+
def show(id)
|
38
|
+
note = api.get(id)
|
39
|
+
body = """title: #{note['title']}
|
40
|
+
id: #{note['id']}
|
41
|
+
created: #{note['created_at']}
|
42
|
+
updated: #{note['updated_at']}
|
43
|
+
|
44
|
+
#{note['body'].delete("\C-M")}
|
45
|
+
"""
|
46
|
+
|
47
|
+
temp = Tempfile.new(["notes", ".md"])
|
48
|
+
temp.write body
|
49
|
+
temp.flush
|
50
|
+
system(ENV['PAGER'], temp.path)
|
51
|
+
end
|
52
|
+
|
53
|
+
desc "edit ID", "Edit the given note in the editor"
|
54
|
+
def edit(id)
|
55
|
+
note = api.get(id)
|
56
|
+
if note.nil?
|
57
|
+
raise Thor::Error.new "Unknown note id #{id}"
|
58
|
+
end
|
59
|
+
body = """title: #{note['title']}
|
60
|
+
id: #{note['id']}
|
61
|
+
created: #{note['created_at']}
|
62
|
+
updated: #{note['updated_at']}
|
63
|
+
|
64
|
+
#{note['body'].delete("\C-M")}
|
65
|
+
"""
|
66
|
+
temp = Tempfile.new(["note", ".md"])
|
67
|
+
temp.write body
|
68
|
+
temp.flush
|
69
|
+
|
70
|
+
system("#{ENV['EDITOR']} #{temp.path}")
|
71
|
+
|
72
|
+
temp.rewind
|
73
|
+
contents = temp.read
|
74
|
+
|
75
|
+
headers, body = contents.split(/\n\n/, 2)
|
76
|
+
|
77
|
+
api.update(id, body)
|
78
|
+
end
|
79
|
+
|
80
|
+
desc "append ID", "Append to the given note"
|
81
|
+
def append(*args)
|
82
|
+
id = args.shift
|
83
|
+
raise Thor::Error.new "Expected an id" unless id
|
84
|
+
|
85
|
+
body = args.join(" ")
|
86
|
+
if body.strip == ""
|
87
|
+
temp = Tempfile.new(['journal', '.md'])
|
88
|
+
system(ENV['EDITOR'], temp.path)
|
89
|
+
body = temp.read
|
90
|
+
end
|
91
|
+
|
92
|
+
if body.strip == ""
|
93
|
+
exit 0
|
94
|
+
end
|
95
|
+
|
96
|
+
api.append(id, body)
|
97
|
+
end
|
98
|
+
|
99
|
+
default_task :list
|
100
|
+
|
101
|
+
no_tasks do
|
102
|
+
def api
|
103
|
+
Marginalia::IO::API.new(options[:host])
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/marginalia-io/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Pete Keen"]
|
6
|
+
gem.email = ["pete@marginalia.io"]
|
7
|
+
gem.description = "API and Command Line Client for Marginalia.io"
|
8
|
+
gem.summary = "Marginalia.io is a web-based note taking and journalling app using Markdown. This is the Ruby API for it."
|
9
|
+
gem.homepage = "https://www.marginalia.io"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "marginalia-io"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Marginalia::IO::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "netrc", "~> 0.7.7"
|
19
|
+
gem.add_dependency "httparty", "~> 0.8.3"
|
20
|
+
gem.add_dependency "thor", "~> 0.16.0"
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: marginalia-io
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Pete Keen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: netrc
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.7.7
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.7.7
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: httparty
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.8.3
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.8.3
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: thor
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.16.0
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.16.0
|
62
|
+
description: API and Command Line Client for Marginalia.io
|
63
|
+
email:
|
64
|
+
- pete@marginalia.io
|
65
|
+
executables:
|
66
|
+
- marginalia
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- bin/marginalia
|
76
|
+
- lib/marginalia-io.rb
|
77
|
+
- lib/marginalia-io/api.rb
|
78
|
+
- lib/marginalia-io/auth.rb
|
79
|
+
- lib/marginalia-io/cli.rb
|
80
|
+
- lib/marginalia-io/version.rb
|
81
|
+
- marginalia-io.gemspec
|
82
|
+
homepage: https://www.marginalia.io
|
83
|
+
licenses: []
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ! '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 1.8.24
|
103
|
+
signing_key:
|
104
|
+
specification_version: 3
|
105
|
+
summary: Marginalia.io is a web-based note taking and journalling app using Markdown.
|
106
|
+
This is the Ruby API for it.
|
107
|
+
test_files: []
|