Class: Server

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/gitstuff-preview/server.rb

Instance Method Summary collapse

Instance Method Details

#basic_metadataObject



65
66
67
68
69
70
71
# File 'lib/gitstuff-preview/server.rb', line 65

def 
  Hashie::Mash.new({
    :root_path => request.url.match(/(^.*\/{2}[^\/]*)/)[1],
    :asset_path => '/assets',
    :search_form => search_form    
  })
end

#default_metadataObject



73
74
75
76
77
78
79
# File 'lib/gitstuff-preview/server.rb', line 73

def 
  hash = {
    :previous_page => '#',
    :next_page => '#',
  }
  hash.merge()
end

#get_post_content(slug, path = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/gitstuff-preview/server.rb', line 38

def get_post_content(slug, path=nil)
  path ||= File.join("#{BASE_PATH}posts", slug) + ".yml"
  begin
    post_data = Hashie::Mash.new YAML.load_file(path)
    post_data.content = File.read(path).sub /---.*---\n/m, ''
    post_data.author = Hashie::Mash.new :name => "Author Name", :email => "[email protected]"
    post_data.gravatar = "http://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(post_data.author.email)}"
    post_data.created_at = Time.now
    post_data.modified_at = Time.now
    post_data
  rescue => e
    puts "!! Error loading #{path}"
    # raise e
    Hashie::Mash.new :content => "Error in #{slug}.yml: #{e.message}"
  end
end

#render_index(context = {}, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/gitstuff-preview/server.rb', line 18

def render_index(context={}, options={})
  context[:posts] = []
  Dir["#{BASE_PATH}/posts/*.yml"][0,10].each do |file|
    slug = File.basename(file, ".yml")
    post = get_post_content(slug, file)
    post.url = "/#{slug}"
    post.content = RDiscount.new(post.content).to_html
    context[:posts] << post.to_hash
  end
  render_page(context, options)
end

#render_page(context, options = {}) ⇒ Object



55
56
57
58
59
# File 'lib/gitstuff-preview/server.rb', line 55

def render_page(context, options={})
  layout = options[:layout] || 'index.html.liquid'
  layout = Liquid::Template.parse(File.read File.join(BASE_PATH, 'templates', layout))
  layout.render Hashie::Mash.new context
end

#render_post(slug, context = {}) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/gitstuff-preview/server.rb', line 30

def render_post(slug, context={})
  post = get_post_content(slug)
  post.content = RDiscount.new(post.content).to_html
  context[:posts] = [post.to_hash]
  context[:single_post] = true
  render_page(context)
end

#search_formObject



61
62
63
# File 'lib/gitstuff-preview/server.rb', line 61

def search_form
  '<form action="/search" method="get"><input class="search" type="text" name="q"/></form>'
end