Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Josep M. Bach committed Feb 23, 2011
0 parents commit 65fe690
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format documentation
1 change: 1 addition & 0 deletions .rvmrc
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1 @@
rvm --create use ruby-1.9.2@gram
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
source :gemcutter

gemspec
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Original file line Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions Rakefile
Original file line number Original file line Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions Readme.md
Original file line number Original file line Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions bin/gram
Original file line number Original file line Diff line number Diff line change
@@ -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)
26 changes: 26 additions & 0 deletions gram.gemspec
Original file line number Original file line Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions lib/gram.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'gram/blog'
require 'gram/blog/parser'

module Gram
COMPONENTS = %w(blog)
end
30 changes: 30 additions & 0 deletions lib/gram/blog.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions lib/gram/blog/parser.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lib/gram/version.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
module Gram
VERSION = '0.0.1'
end
30 changes: 30 additions & 0 deletions spec/gram/blog/parser_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions spec/gram/blog_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/gram_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'spec_helper'

describe Gram do
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'rspec'
require 'gram'

0 comments on commit 65fe690

Please sign in to comment.