Module: Gumdrop

Defined in:
lib/gumdrop/site.rb,
lib/gumdrop.rb,
lib/gumdrop/server.rb,
lib/gumdrop/content.rb,
lib/gumdrop/context.rb,
lib/gumdrop/version.rb,
lib/gumdrop/generator.rb,
lib/gumdrop/hash_object.rb,
lib/gumdrop/data_manager.rb,
lib/gumdrop/view_helpers.rb,
lib/gumdrop/proxy_handler.rb

Overview

WORK IN PROGRESS!

Defined Under Namespace

Modules: ViewHelpers Classes: Config, Content, Context, DataManager, GeneratedContent, Generator, HashObject, Pager, Server, Site, SitefileDSL

Constant Summary collapse

DEFAULT_OPTIONS =
{
  relative_paths: true,
  proxy_enabled: true,
  output_dir: "./output",
  source_dir: "./source",
  data_dir: './data',
  log: './logs/build.log',
  ignore: %w(.DS_Store .gitignore .git .svn .sass-cache),
  server_timeout: 15,
  # server_port: 4567,
  env: 'production'
}
VERSION =
"0.6.2"
DATA_DOC_EXTS =

Supported Data File Types:

%w(json yml yaml ymldb yamldb yamldoc ymldoc)

Class Method Summary collapse

Class Method Details

.fetch_site_file(filename = "Gumdrop") ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gumdrop.rb', line 44

def fetch_site_file(filename="Gumdrop")
  here= Dir.pwd
  found= File.file? File.join( here, filename )
  while !found and File.directory?(here) and File.dirname(here).length > 3
    here= File.expand_path File.join(here, '../')
    found= File.file? File.join( here, filename )
  end
  if found
    File.expand_path File.join(here, filename)
  else
    nil
  end
end

.handle_proxy(proxy, proxy_url, env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gumdrop/proxy_handler.rb', line 26

def self.handle_proxy(proxy, proxy_url, env)
  if proxy[:secure] && !HTTPS_ENABLED
    $stderr.puts "~ WARNING: HTTPS is not supported on your system, using HTTP instead.\n"
    $stderr.puts"    If you are using Ubuntu, you can run `apt-get install libopenssl-ruby`\n"
    proxy[:secure] = false
  end

  origin_host = env['SERVER_NAME'] # capture the origin host for cookies
  http_method = env['REQUEST_METHOD'].to_s.downcase
  url = proxy[:path_info] #proxy_url #env['PATH_INFO']
  params = env['QUERY_STRING']
  
  puts "PROXY: -> #{url}"

  # collect headers...
  headers = {}
  env.each do |key, value|
    next unless key =~ /^HTTP_/
    key = key.gsub(/^HTTP_/,'').downcase.sub(/^\w/){|l| l.upcase}.gsub(/_(\w)/){|l| "-#{$1.upcase}"} # remove HTTP_, dasherize and titleize
    if !key.eql? "Version"
      headers[key] = value
    end
  end

  # Rack documentation says CONTENT_TYPE and CONTENT_LENGTH aren't prefixed by HTTP_
  headers['Content-Type'] = env['CONTENT_TYPE'] if env['CONTENT_TYPE']

  length = env['CONTENT_LENGTH']
  headers['Content-Length'] = length if length

  http_host, http_port = proxy[:to].split(':')
  http_port = proxy[:secure] ? '443' : '80' if http_port.nil?

  # added 4/23/09 per Charles Jolley, corrects problem
  # when making requests to virtual hosts
  headers['Host'] = "#{http_host}:#{http_port}"

  if proxy[:url]
    url = url.sub(/^#{Regexp.escape proxy_url}/, proxy[:url])
  end

  http_path = [url]
  http_path << params if params && params.size>0
  http_path = http_path.join('?')

  response = nil
  no_body_method = %w(get copy head move options trace)

  done = false
  tries = 0
  until done
    http = ::Net::HTTP.new(http_host, http_port)

    if proxy[:secure]
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    http.start do |web|
      if no_body_method.include?(http_method)
        response = web.send(http_method, http_path, headers)
      else
        http_body = env['rack.input']
        http_body.rewind # May not be necessary but can't hurt

        req = Net::HTTPGenericRequest.new(http_method.upcase,
                                            true, true, http_path, headers)
        req.body_stream = http_body if length.to_i > 0
        response = web.request(req)
      end
    end

    status = response.code # http status code
    protocol = proxy[:secure] ? 'https' : 'http'

    $stderr.puts "~ PROXY: #{http_method.upcase} #{status} #{url} -> #{protocol}://#{http_host}:#{http_port}#{http_path}\n"

    # display and construct specific response headers
    response_headers = {}
    ignore_headers = ['transfer-encoding', 'keep-alive', 'connection']
    response.each do |key, value|
      next if ignore_headers.include?(key.downcase)
      # If this is a cookie, strip out the domain.  This technically may
      # break certain scenarios where services try to set cross-domain
      # cookies, but those services should not be doing that anyway...
      value.gsub!(/domain=[^\;]+\;? ?/,'') if key.downcase == 'set-cookie'
      # Location headers should rewrite the hostname if it is included.
      value.gsub!(/^http:\/\/#{http_host}(:[0-9]+)?\//, "http://#{http_host}/") if key.downcase == 'location'
      # content-length is returning char count not bytesize
      if key.downcase == 'content-length'
        if response.body.respond_to?(:bytesize)
          value = response.body.bytesize.to_s
        elsif response.body.respond_to?(:size)
          value = response.body.size.to_s
        else
          value = '0'
        end
      end

      $stderr << "   #{key}: #{value}\n"
      response_headers[key] = value
    end

    if [301, 302, 303, 307].include?(status.to_i) && proxy[:redirect] != false
      $stderr.puts '~ REDIRECTING: '+response_headers['location']+"\n"

      uri = URI.parse(response_headers['location']);
      http_host = uri.host
      http_port = uri.port
      http_path = uri.path
      http_path += '?'+uri.query if uri.query

      tries += 1
      if tries > 10
        raise "Too many redirects!"
      end
    else
      done = true
    end
  end

  # Thin doesn't like null bodies
  response_body = response.body || ''
  #::Rack::Utils::HeaderHash.new(response_headers)
  return [status.to_i, response_headers, response_body.to_s]
end

.in_site_folder?(filename = "Gumdrop") ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/gumdrop.rb', line 40

def in_site_folder?(filename="Gumdrop")
  !fetch_site_file(filename).nil?
end

.run(opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gumdrop.rb', line 22

def run(opts={})
  site_file= Gumdrop.fetch_site_file
  unless site_file.nil?
    site= Site.new site_file, opts

    old= Dir.pwd
    Dir.chdir site.root_path

    site.build
    
    Dir.chdir old
    
    puts "Done."
  else
    puts "Not in a valid Gumdrop site directory."
  end
end

.site_dirname(filename = "Gumdrop") ⇒ Object



58
59
60
# File 'lib/gumdrop.rb', line 58

def site_dirname(filename="Gumdrop")
  File.dirname( fetch_site_file( filename ) )
end