public
Description: Publish to enki with VIM. That's pretty neat.
Homepage: http://enkiblog.com
Clone URL: git://github.com/xaviershay/enki-vim.git
enki-vim / lib / remote_enki_blog.rb
100644 43 lines (33 sloc) 0.791 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
require 'net/http'
require 'digest/sha1'
require 'yaml'
require 'time'
 
class RemoteEnkiBlog
  attr_accessor :url
  attr_accessor :key
 
  def initialize(url, key)
    self.url = url
    self.key = key
    url = URI.parse(url)
    self.connection = Net::HTTP.new(url.host, url.port)
  end
 
  def posts
    fetch("/admin/posts.yaml")["posts"]
  end
 
  def post(id)
    fetch("/admin/posts/#{id}.yaml")["post"]
  end
 
  def pages
    fetch("/admin/pages.yaml")
  end
 
  protected
 
  attr_accessor :connection
 
  def fetch(url)
    resp, data = connection.get(url, 'X-EnkiHash' => hash_request(''), 'Content-Type' => 'application/x-yaml')
    raise("Not Successful") if resp.code != '200'
    YAML.load(data)
  end
 
  def hash_request(data)
    Digest::SHA1.hexdigest(data + self.key)
  end
end