Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 65fe690082587f1ee80b0e08df7cbf9e0f08e88b @txus txus committed Feb 23, 2011
Showing with 234 additions and 0 deletions.
  1. +2 −0 .rspec
  2. +1 −0 .rvmrc
  3. +3 −0 Gemfile
  4. +30 −0 Gemfile.lock
  5. +8 −0 Rakefile
  6. +28 −0 Readme.md
  7. +10 −0 bin/gram
  8. +26 −0 gram.gemspec
  9. +6 −0 lib/gram.rb
  10. +30 −0 lib/gram/blog.rb
  11. +24 −0 lib/gram/blog/parser.rb
  12. +3 −0 lib/gram/version.rb
  13. +30 −0 spec/gram/blog/parser_spec.rb
  14. +27 −0 spec/gram/blog_spec.rb
  15. +4 −0 spec/gram_spec.rb
  16. +2 −0 spec/spec_helper.rb
2 .rspec
@@ -0,0 +1,2 @@
+--color
+--format documentation
1 .rvmrc
@@ -0,0 +1 @@
+rvm --create use ruby-1.9.2@gram
@@ -0,0 +1,3 @@
+source :gemcutter
+
+gemspec
@@ -0,0 +1,30 @@
+PATH
+ remote: .
+ specs:
+ gram (0.0.1)
+ rest-client
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ diff-lcs (1.1.2)
+ mime-types (1.16)
+ rest-client (1.6.1)
+ mime-types (>= 1.16)
+ rspec (2.5.0)
+ rspec-core (~> 2.5.0)
+ rspec-expectations (~> 2.5.0)
+ rspec-mocks (~> 2.5.0)
+ rspec-core (2.5.1)
+ rspec-expectations (2.5.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.5.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ bundler (~> 1.0.7)
+ gram!
+ rest-client
+ rspec (~> 2.5.0)
@@ -0,0 +1,8 @@
+require 'rubygems'
+require 'rake'
+
+require 'rspec/core/rake_task'
+desc "Run the specs under spec"
+RSpec::Core::RakeTask.new
+
+task :default => :spec
@@ -0,0 +1,28 @@
+#gram
+
+Gram is an internal administration tool for Codegram.
+
+It is composed of independent components. For now the only available component is Blog. To install and setup Gram, just do the following:
+
+ $ echo "token: MY_CODEGRAM_TOKEN" > ~/.gramrc
+ $ gem install gram
+
+##Blog component
+
+To publish a blogpost, type this:
+
+ $ gram blog my_blogpost.markdown
+
+The `my_blogpost.markdown` file should be a regular markdown file with some headers in it, like this:
+
+ ---
+ title: My blog post title
+ tagline: Stranger than fiction
+ ---
+
+ # My awesome header
+ ## More markdown goodness, etc.
+
+## Copyright
+
+Copyright (c) 2011 Codegram. See LICENSE for details.
@@ -0,0 +1,10 @@
+#!/usr/bin/env ruby
+$: << 'lib'
+require 'gram'
+
+component = 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)
@@ -0,0 +1,26 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "gram/version"
+
+Gem::Specification.new do |s|
+ s.name = "gram"
+ s.version = Gram::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Josep M. Bach", "Josep Jaume Rey", "Oriol Gual"]
+ s.email = ["info@codegram.com"]
+ s.homepage = "http://github.com/codegram/gram"
+ s.summary = %q{Internal client for Codegram administration}
+ s.description = %q{Internal client for Codegram administration}
+
+ s.rubyforge_project = "gram"
+
+ s.add_runtime_dependency 'rest-client'
+
+ s.add_development_dependency 'bundler', '~> 1.0.7'
+ s.add_development_dependency 'rspec', '~> 2.5.0'
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
@@ -0,0 +1,6 @@
+require 'gram/blog'
+require 'gram/blog/parser'
+
+module Gram
+ COMPONENTS = %w(blog)
+end
@@ -0,0 +1,30 @@
+require 'rest-client'
+
+module Gram
+ module Blog
+ class << self
+
+ def run(*args)
+ file = args.first
+ raise "File #{file} does not exist." unless File.exists?(file)
+ puts "Gram::Blog posting..."
+ 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
+
+ private
+
+ def get_token
+ if File.exists?(File.join(File.expand_path('~'), '.gramrc'))
+ YAML.load(File.read(File.join(File.expand_path('~'), '.gramrc')))["token"]
+ else
+ puts "Can't get Gram token. Please create a ~/.gramrc YAML file with a token key."
+ exit(1)
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,24 @@
+require 'yaml'
+
+module Gram
+ module Blog
+ module Parser
+ class << self
+
+ def parse(file)
+ raw_content = File.read(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 }
+ end
+
+ end
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module Gram
+ VERSION = '0.0.1'
+end
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+module Gram
+ module Blog
+ describe Parser do
+
+ describe ".parse" do
+ it 'converts a file into a hash' do
+ file = double :file
+ raw = """
+---
+title: My title
+tagline: My tagline
+---
+
+#My post
+#
+Blah
+ """
+ File.stub(:read).with(file).and_return raw
+
+ subject.parse(file).should == { title: 'My title',
+ tagline: 'My tagline',
+ body: "#My post\n#\nBlah"}
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,27 @@
+require 'spec_helper'
+
+module Gram
+ describe Blog do
+
+ describe ".run" do
+ it 'parses the file' do
+ subject::Parser.should_receive(:parse).with("my_post.md")
+ expect {
+ subject.run("my_post.md")
+ }.to raise_error(RestClient::InternalServerError)
+ end
+ it 'sends a post request' do
+ post = double :post
+ token = double :token
+ response = double :response, code: 201, body: 'ok'
+ subject.stub(:get_token).and_return token
+ subject::Parser.stub(:parse).with("my_post.md").and_return post
+
+ RestClient.should_receive(:post).with("http://codegram.com/api/posts", token: token, post: post).and_return response
+
+ subject.run("my_post.md")
+ end
+ end
+
+ end
+end
@@ -0,0 +1,4 @@
+require 'spec_helper'
+
+describe Gram do
+end
@@ -0,0 +1,2 @@
+require 'rspec'
+require 'gram'

0 comments on commit 65fe690

Please sign in to comment.