Class: DuckMap::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/duck_map/static.rb

Instance Method Summary collapse

Instance Method Details

#build(options = {}) ⇒ Object



21
22
23
24
25
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
# File 'lib/duck_map/static.rb', line 21

def build(options = {})
  key = options[:key].blank? ? :all : options[:key].to_s.downcase.to_sym

  begin

    attributes = DuckMap::Config.attributes

    compressed = attributes[:compression].blank? ? :compressed : attributes[:compression]

    # command line override config
    compressed = options[:compression].blank? ? compressed : options[:compression]

    compressed = compressed.blank? ? :compressed : compressed.to_s.downcase.to_sym
    compressed = compressed.eql?(:compressed) ? true : false

    static_host = attributes[:static_host].blank? ? attributes[:canonical_host] : attributes[:static_host]

    # command line override config
    static_host = options[:static_host].blank? ? static_host : options[:static_host]

    static_port = attributes[:static_port].blank? ? attributes[:canonical_port] : attributes[:static_port]

    # command line override config
    static_port = options[:static_port].blank? ? static_port : options[:static_port]

    static_target = attributes[:static_target].blank? ? File.join(Rails.root, "public") : attributes[:static_target]

    # command line override config
    static_target = options[:static_target].blank? ? static_target : options[:static_target]

    if !static_host.blank?

      request_options = {
                          "rack.input" => StringIO.new,
                          "rack.errors" => StringIO.new,
                          "rack.multithread" => true,
                          "rack.multiprocess" => true,
                          "rack.run_once" => false,
                          "CONTENT_TYPE" => "application/xml",
                          "HTTP_HOST" => static_host,
                          "SERVER_PORT" => static_port}

      DuckMap.console "Searching for route: #{key}"

      routes = []
      if key.eql?(:all)

        routes = Rails.application.routes.sitemap_routes_only

      else

        route = Rails.application.routes.find_route_via_name("#{key}_sitemap")
        if route.blank?
          DuckMap.console "Unable to find route: #{key}"
        else
          routes.push(route)
        end

      end

      routes.each do |route|

        DuckMap.console "Processing route: #{route.name}"

        clazz = ClassHelpers.get_controller_class(route.controller_name)

        if clazz.blank?
          DuckMap.logger.debug "sorry, could not determine controller class...: route name: #{route.name} controller: #{route.controller_name}  action: #{route.action_name}"
        else

          controller = clazz.new

          controller.request = SitemapStaticRequest.new(request_options)
          controller.response = SitemapStaticResponse.new

          # man, I am getting lazy...
          request_path = route.path.spec.to_s.gsub(":format", "xml")

          controller.sitemap_build(request_path)

          view = ActionViewTestObject.new(File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "app", "views")))

          view.controller = controller

          view.assign(sitemap_model: controller.sitemap_model)

          parts = request_path.split("/")
          parts.insert(0, static_target)

          file_spec = File.join(parts)

          File.open(file_spec, "w") do |file|

            file.write view.render(:template => "sitemap/default_template.xml.erb")

          end

          if compressed

            Zlib::GzipWriter.open("#{file_spec}.gz") do |gz|
              gz.mtime = File.mtime(file_spec)
              gz.orig_name = file_spec
              gz.write IO.binread(file_spec)
            end

            FileUtils.rm_rf file_spec

          end

        end

      end

    else
      DuckMap.console "Sorry, cannot build sitemap.  You need to set static_host in config/routes.rb or on the command-line..."
    end

  rescue Exception => e
    puts "#{e.message}"
    puts e.backtrace
  end

end