ernest 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -0
- data/ernest.gemspec +0 -1
- data/lib/ernest.rb +9 -22
- data/lib/ernest/api.rb +43 -0
- data/lib/ernest/metadata_parser.rb +22 -0
- data/lib/ernest/post.rb +52 -0
- data/lib/ernest/update_post_id.rb +48 -0
- data/lib/ernest/version.rb +1 -1
- metadata +6 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 134682b2b5ffe4bb731178849c8462f099025d48
|
4
|
+
data.tar.gz: d0f89e65bec4a738ada54655447234845d4206ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef3025ba8eae891b987c5fa386ed0a274a9037a65515c03805ff7cb07172029dcbc4e884017d829ffc4b58667bb466445f5260c9fbbcbabf5be10fb86d113197
|
7
|
+
data.tar.gz: ccb8616d903604aebe9912bac2555451c08075d905e9937dddb00c8d8ba9297c484fe26a748f2b0e86afc33b62d5feadbaaef8917b0b9f8380c3b7807c0deadd
|
data/README.md
CHANGED
@@ -47,6 +47,25 @@ When you're done with the writing, create a draft:
|
|
47
47
|
$ ernest post-name.markdown
|
48
48
|
```
|
49
49
|
|
50
|
+
### Updates
|
51
|
+
|
52
|
+
If you already have a post created and want to update it, you just need to add
|
53
|
+
its id to the metadata:
|
54
|
+
|
55
|
+
```markdown
|
56
|
+
---
|
57
|
+
id: 28
|
58
|
+
title: "A title"
|
59
|
+
---
|
60
|
+
|
61
|
+
... Rest of post ...
|
62
|
+
```
|
63
|
+
|
64
|
+
Now just call ernest again and it will do a `PUT` instead of a `POST`.
|
65
|
+
|
66
|
+
If you create your post with ernest, it will add the id for you (the server must
|
67
|
+
respond with the post id upon creation).
|
68
|
+
|
50
69
|
## Contributing
|
51
70
|
|
52
71
|
1. Fork it ( https://github.com/[my-github-username]/ernest/fork )
|
data/ernest.gemspec
CHANGED
data/lib/ernest.rb
CHANGED
@@ -1,39 +1,26 @@
|
|
1
1
|
require 'ernest/version'
|
2
|
-
require '
|
3
|
-
require '
|
4
|
-
require 'dotenv'
|
5
|
-
Dotenv.load
|
2
|
+
require 'ernest/post'
|
3
|
+
require 'ernest/update_post_id'
|
6
4
|
|
7
5
|
module Ernest
|
8
6
|
class CreatesDrafts
|
9
|
-
def initialize(file_path)
|
7
|
+
def initialize(file_path, response_handler = UpdatePostId, post_class = Post)
|
10
8
|
@file_path = file_path
|
9
|
+
@response_handler = response_handler
|
10
|
+
@post_class = post_class
|
11
11
|
end
|
12
12
|
|
13
13
|
def call
|
14
|
-
response =
|
15
|
-
|
16
|
-
body: post,
|
17
|
-
headers: { 'Authorization' => 'Token token="' + ENV['TOKEN'] + '"' }
|
18
|
-
)
|
19
|
-
|
20
|
-
puts response unless response.code == 200
|
14
|
+
response = post.save
|
15
|
+
response_handler.after_saving_post(response, post).call
|
21
16
|
end
|
22
17
|
|
23
18
|
private
|
24
19
|
|
25
|
-
attr_reader :file_path
|
20
|
+
attr_reader :file_path, :response_handler, :post_class
|
26
21
|
|
27
22
|
def post
|
28
|
-
|
29
|
-
post: {
|
30
|
-
body: data.output
|
31
|
-
}.merge(data.metadata)
|
32
|
-
}
|
33
|
-
end
|
34
|
-
|
35
|
-
def data
|
36
|
-
@_data ||= Metadown.render(File.open(file_path, 'rb').read)
|
23
|
+
@_post ||= post_class.parse_from_file(file_path)
|
37
24
|
end
|
38
25
|
end
|
39
26
|
end
|
data/lib/ernest/api.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'httparty'
|
2
|
+
require 'dotenv'
|
3
|
+
Dotenv.load
|
4
|
+
|
5
|
+
module Ernest
|
6
|
+
class API
|
7
|
+
def self.with_data(data)
|
8
|
+
new(data)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(data)
|
12
|
+
@data = data
|
13
|
+
end
|
14
|
+
|
15
|
+
def post
|
16
|
+
HTTParty.post(
|
17
|
+
ENV['API_ENDPOINT'],
|
18
|
+
body: http_data,
|
19
|
+
headers: { 'Authorization' => 'Token token="' + ENV['TOKEN'] + '"' }
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
def put(id)
|
24
|
+
HTTParty.put(
|
25
|
+
ENV['API_ENDPOINT'] + "/#{id}",
|
26
|
+
body: http_data,
|
27
|
+
headers: { 'Authorization' => 'Token token="' + ENV['TOKEN'] + '"' }
|
28
|
+
)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
attr_reader :data
|
34
|
+
|
35
|
+
def http_data
|
36
|
+
{
|
37
|
+
post: {
|
38
|
+
body: data.output
|
39
|
+
}.merge(data.metadata)
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module Ernest
|
4
|
+
Data = Struct.new(:metadata, :output)
|
5
|
+
|
6
|
+
class MetadataParser
|
7
|
+
attr_reader :text, :metadata
|
8
|
+
|
9
|
+
def initialize(text)
|
10
|
+
@text = text
|
11
|
+
@metadata = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def parse
|
15
|
+
if @text =~ /^(---\s*\n.*?\n?^---\s*$\n?)/m
|
16
|
+
@metadata = YAML.load($1)
|
17
|
+
@text.sub!($1, '')
|
18
|
+
end
|
19
|
+
Data.new(metadata, text)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/ernest/post.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'ernest/metadata_parser'
|
2
|
+
require 'ernest/api'
|
3
|
+
|
4
|
+
module Ernest
|
5
|
+
class Post
|
6
|
+
def self.parse_from_file(file_path, parser = MetadataParser, api = API)
|
7
|
+
parsed_data = parser.new(File.open(file_path, 'rb').read).parse
|
8
|
+
new(parsed_data, file_path, api)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(data, file_path, api)
|
12
|
+
@data = data
|
13
|
+
@file_path = file_path
|
14
|
+
@api = api
|
15
|
+
end
|
16
|
+
|
17
|
+
def save
|
18
|
+
if id?
|
19
|
+
update
|
20
|
+
else
|
21
|
+
create
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def update_id(new_id)
|
26
|
+
unless id?
|
27
|
+
new_file = File.read(file_path).sub("---\n", "---\nid: #{new_id}\n")
|
28
|
+
File.write(file_path, new_file)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
attr_reader :data, :file_path, :api
|
35
|
+
|
36
|
+
def id?
|
37
|
+
!!id
|
38
|
+
end
|
39
|
+
|
40
|
+
def id
|
41
|
+
data.metadata['id']
|
42
|
+
end
|
43
|
+
|
44
|
+
def create
|
45
|
+
api.with_data(data).post
|
46
|
+
end
|
47
|
+
|
48
|
+
def update
|
49
|
+
api.with_data(data).put(id)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Ernest
|
2
|
+
class UpdatePostId
|
3
|
+
def self.after_saving_post(response, post)
|
4
|
+
new(response, post)
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(response, post)
|
8
|
+
@response = response
|
9
|
+
@post = post
|
10
|
+
end
|
11
|
+
|
12
|
+
def call
|
13
|
+
if everything_ok?
|
14
|
+
update_post_id
|
15
|
+
else
|
16
|
+
puts response
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
attr_reader :response, :post
|
23
|
+
|
24
|
+
def update_post_id
|
25
|
+
if new_post_id
|
26
|
+
post.update_id(new_post_id)
|
27
|
+
else
|
28
|
+
puts did_not_receive_id_error_message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def new_post_id
|
33
|
+
response.parsed_response["id"]
|
34
|
+
end
|
35
|
+
|
36
|
+
def everything_ok?
|
37
|
+
response.code == 200
|
38
|
+
end
|
39
|
+
|
40
|
+
def did_not_receive_id_error_message
|
41
|
+
<<-ERR_MSG.gsub(/^ {8}/, '').gsub("\n", '')
|
42
|
+
The server did not respond with an id,
|
43
|
+
therefore I will not be able to update this post
|
44
|
+
in the next run, unless you add it manually.
|
45
|
+
ERR_MSG
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/ernest/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ernest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zamith
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dotenv
|
@@ -38,20 +38,6 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: metadown
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
41
|
- !ruby/object:Gem::Dependency
|
56
42
|
name: bundler
|
57
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -97,6 +83,10 @@ files:
|
|
97
83
|
- ernest.gemspec
|
98
84
|
- examples/post-1.markdown.example
|
99
85
|
- lib/ernest.rb
|
86
|
+
- lib/ernest/api.rb
|
87
|
+
- lib/ernest/metadata_parser.rb
|
88
|
+
- lib/ernest/post.rb
|
89
|
+
- lib/ernest/update_post_id.rb
|
100
90
|
- lib/ernest/version.rb
|
101
91
|
homepage: ''
|
102
92
|
licenses:
|