Permalink
Browse files

Blog#upload and Blog#download

  • Loading branch information...
1 parent 563022b commit 6d61afa007bca0b624c2ddf2ca0421e0e01cd7ae @txus txus committed Mar 21, 2011
Showing with 87 additions and 15 deletions.
  1. +1 −0 .gitignore
  2. +0 −1 Gemfile.lock
  3. +6 −1 bin/gram
  4. +45 −3 lib/gram/blog.rb
  5. +1 −4 lib/gram/blog/parser.rb
  6. +5 −3 spec/gram/blog/parser_spec.rb
  7. +29 −3 spec/gram/blog_spec.rb
View
@@ -0,0 +1 @@
+pkg/
View
@@ -26,5 +26,4 @@ PLATFORMS
DEPENDENCIES
bundler (~> 1.0.7)
gram!
- rest-client
rspec (~> 2.5.0)
View
@@ -3,8 +3,13 @@ $: << 'lib'
require 'gram'
component = ARGV.shift
+action = ARGV.shift
args = ARGV
raise "Unknown Gram component. Available components are: #{Gram::COMPONENTS.join(', ')}" unless Gram::COMPONENTS.include?(component)
-eval("Gram::#{component.capitalize}").run(*args)
+component_klass = eval("Gram::#{component.capitalize}")
+
+raise "Unknown action for #{component} component.\n#{component_klass.banner}\n" unless component_klass::ACTIONS.keys.map(&:to_s).include?(action)
+
+component_klass.send(action, *args)
View
@@ -1,19 +1,61 @@
require 'rest-client'
+require 'json'
module Gram
module Blog
+
+ ACTIONS = { upload: {
+ description: "Uploads the markdown file with filename FILE",
+ arguments: %w(FILE),
+ },
+ download: {
+ description: "Downloads all blog posts to the current folder.",
+ arguments: %w(),
+ }
+ }
+
class << self
- def run(*args)
- file = args.first
+ def banner
+ out = "Available actions:\n"
+ ACTIONS.each_pair do |action, metadata|
+ out << "\n\t#{action} #{metadata[:arguments].join(' ')}\t\t#{metadata[:description]}"
+ end
+ out
+ end
+
+ # ACTIONS
+
+ def upload(file)
raise "File #{file} does not exist." unless File.exists?(file)
- puts "Gram::Blog posting..."
+ puts "Gram::Blog uploading..."
post = Parser.parse(file)
response = RestClient.post("http://codegram.com/api/posts", token: get_token, post: post )
puts "Response Code: #{response.code}"
puts "Response Body: #{response.body}"
end
+ def download
+ posts = JSON.parse(RestClient.get("http://codegram.com/api/posts?token=#{get_token}"))
+ posts.each do |post|
+ post = post["post"]
+ header = """
+---
+title: #{post["title"]}
+tagline: #{post["tagline"]}
+---
+
+""".strip
+ puts "Downloading #{post["cached_slug"]}.markdown..."
+ File.open("#{post["cached_slug"]}.markdown", 'w') do |f|
+ f.write header
+ f.write "\n\n"
+ f.write post["body"]
+ f.write "\n"
+ end
+ end
+ end
+
private
def get_token
View
@@ -10,12 +10,9 @@ def parse(file)
headers = raw_content.match(/---(.*)---/m)
yaml = YAML.load($1.strip)
- title = yaml["title"]
- tagline = yaml["tagline"]
-
content = raw_content.gsub(/---.*---/m, '').strip
- { title: title, tagline: tagline, body: content }
+ yaml.update({ 'body' => content })
end
end
@@ -11,6 +11,7 @@ module Blog
---
title: My title
tagline: My tagline
+published: false
---
#My post
@@ -19,9 +20,10 @@ module Blog
"""
File.stub(:read).with(file).and_return raw
- subject.parse(file).should == { title: 'My title',
- tagline: 'My tagline',
- body: "#My post\n#\nBlah"}
+ subject.parse(file).should == { 'title' => 'My title',
+ 'tagline' => 'My tagline',
+ 'published' => false,
+ 'body' => "#My post\n#\nBlah"}
end
end
View
@@ -7,11 +7,11 @@ module Gram
File.stub(:exists?).and_return true
end
- describe ".run" do
+ describe ".upload" do
it 'parses the file' do
subject::Parser.should_receive(:parse).with("my_post.md")
expect {
- subject.run("my_post.md")
+ subject.upload("my_post.md")
}.to raise_error(RestClient::InternalServerError)
end
it 'sends a post request' do
@@ -23,7 +23,33 @@ module Gram
RestClient.should_receive(:post).with("http://codegram.com/api/posts", token: token, post: post).and_return response
- subject.run("my_post.md")
+ subject.upload("my_post.md")
+ end
+ end
+
+ describe ".download" do
+ it 'gets the posts' do
+ RestClient.stub(:get)
+ JSON.should_receive(:parse).and_return [ { "post" => {
+ "title" => "My title",
+ "tagline" => "My tagline",
+ "cached_slug" => "my-post",
+ "body" => "#Hello world"
+ } } ]
+
+ file = double(:file)
+ File.should_receive(:open).with('my-post.markdown', 'w').and_yield file
+ file.should_receive(:write).with """
+---
+title: My title
+tagline: My tagline
+---
+ """.strip
+ file.should_receive(:write).with "\n\n"
+ file.should_receive(:write).with "#Hello world"
+ file.should_receive(:write).with "\n"
+
+ subject.download
end
end

0 comments on commit 6d61afa

Please sign in to comment.