duck_map 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ require 'active_support/concern'
2
+
3
+ module DuckMap
4
+ module Handlers
5
+
6
+ ##################################################################################
7
+ # get default values and assign to local variable containing all values from a single url node
8
+ # and meta tag values for the index action. the local variable is referred to as "values".
9
+ # - user can override method and manipulate the default values...
10
+ # index action is called.
11
+ # - uses existing values generated via the index method
12
+ # - gives the developer a chance to set instance values to handle special cases
13
+ # lastmod is obtained from locale and merged with "values".
14
+ # call sitemap_capture_attributes on the controller to capture values of the current state and merge them with "values".
15
+ #
16
+ module Index
17
+ extend ActiveSupport::Concern
18
+
19
+ ##################################################################################
20
+ def sitemap_new(options = {})
21
+ return []
22
+ end
23
+
24
+ ##################################################################################
25
+ def sitemap_edit(options = {})
26
+ return []
27
+ end
28
+
29
+ ##################################################################################
30
+ def sitemap_index(options = {})
31
+ rows = []
32
+
33
+ # always start off with default values from config, then, simply overwrite them as we progress.
34
+ values = sitemap_defaults(options)
35
+
36
+ # if the source is meta_data, that means the call to sitemap_index is the result of a user
37
+ # requesting an index page. Therefore, there would be no reason to call the index action, because,
38
+ # it has already been called and any instance variable have been set.
39
+ # Otherwise, if this is the result of a sitemap request, then, call the index action to simulate
40
+ # as if it were a user making the request.
41
+ if options[:source] == :sitemap
42
+
43
+ begin
44
+
45
+ index
46
+
47
+ rescue Exception => e
48
+ # expect this exception to occur EVERYTIME...
49
+ end
50
+
51
+ end
52
+
53
+
54
+ lastmod = self.sitemap_static_lastmod(options[:controller_name], options[:action_name])
55
+ unless lastmod.blank?
56
+ values[:lastmod] = lastmod
57
+ end
58
+
59
+ # first, capture the values from the controller.
60
+ attributes = self.sitemap_stripped_attributes(options[:action_name])
61
+ values = values.merge(self.sitemap_capture_attributes(attributes))
62
+
63
+ data_rows = []
64
+
65
+ if !options[:handler][:block].blank?
66
+
67
+ data_rows = options[:handler][:block].call(options)
68
+
69
+ elsif !options[:handler][:model].blank?
70
+
71
+ data_rows = options[:handler][:model].send(:all)
72
+
73
+ else
74
+
75
+ # capture values from the first available model unless disabled.
76
+ model_object = sitemap_first_model(options[:handler])
77
+ unless model_object.blank?
78
+ model_attributes = attributes
79
+ if model_object.is_sitemap_attributes_defined?
80
+ model_attributes = model_object.sitemap_stripped_attributes(options[:action_name])
81
+ end
82
+
83
+ values = values.merge(model_object.sitemap_capture_attributes(model_attributes))
84
+ end
85
+
86
+ end
87
+
88
+ # data_rows may have changed from an Array to a model object.
89
+ # Make data_rows an Array if it has been switched to an model object.
90
+ if data_rows.kind_of?(ActiveRecord::Base)
91
+ data_rows = [data_rows]
92
+ end
93
+
94
+ # only process the first row since we are processing the index action of the controller
95
+ if data_rows.kind_of?(Array) && data_rows.length > 0
96
+
97
+ data_row = data_rows.first
98
+
99
+ model_attributes = attributes
100
+ if data_row.is_sitemap_attributes_defined?
101
+ model_attributes = data_row.sitemap_stripped_attributes(options[:action_name])
102
+ end
103
+
104
+ values = values.merge(data_row.sitemap_capture_attributes(model_attributes))
105
+
106
+ end
107
+
108
+ if values[:canonical].blank?
109
+
110
+ route = options[:source] == :meta_data ? Rails.application.routes.find_route_via_path(request.path) : options[:route]
111
+
112
+ url_options = sitemap_url_options(values)
113
+
114
+ # forgive me father for I have hacked...
115
+ # i'm really being lazy here, but, if the current route is
116
+ # the root, then, reassign the url to eliminate ?format=html, etc.
117
+ # from the url.
118
+ if !route.name.blank? && route.name.eql?("root")
119
+ url_options.delete(:format)
120
+ values[:canonical] = root_url(url_options)
121
+ else
122
+ values[:canonical] = self.send("#{route.name}_url", url_options)
123
+ end
124
+
125
+ values[:loc] = values[:canonical]
126
+ else
127
+ values[:loc] = values[:canonical]
128
+ end
129
+
130
+ rows.push(values)
131
+
132
+ self.sitemap_meta_data = values
133
+
134
+ return rows
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+ end
@@ -0,0 +1,231 @@
1
+ require 'active_support/concern'
2
+
3
+ module DuckMap
4
+ module Handlers
5
+
6
+ ##################################################################################
7
+ module Show
8
+ extend ActiveSupport::Concern
9
+
10
+ ##################################################################################
11
+ def sitemap_show(options = {})
12
+ rows = []
13
+ puts YAML.dump(options)
14
+
15
+ # always start off with default values from config, then, simply overwrite them as we progress.
16
+ values = sitemap_defaults(options)
17
+
18
+ # show action never needs to call the existing show action.
19
+
20
+ lastmod = self.sitemap_static_lastmod(options[:controller_name], options[:action_name])
21
+ unless lastmod.blank?
22
+ values[:lastmod] = lastmod
23
+ end
24
+
25
+ # first, capture the values from the controller.
26
+ attributes = self.sitemap_stripped_attributes(options[:action_name])
27
+ puts "............................ attributes"
28
+ puts YAML.dump(attributes)
29
+
30
+ values = values.merge(self.sitemap_capture_attributes(attributes))
31
+ puts "---------------------------------==== values"
32
+ puts YAML.dump(values)
33
+
34
+ if options[:source] == :meta_data
35
+ puts " !!!!!!!!!!!!! going for it..."
36
+ # capture values from the first available model unless disabled.
37
+ model_object = sitemap_first_model(options[:handler])
38
+ puts "###########################3 model_object: #{model_object}"
39
+ unless model_object.blank?
40
+ model_attributes = attributes
41
+ if model_object.is_sitemap_attributes_defined?
42
+ model_attributes = model_object.sitemap_stripped_attributes(options[:action_name])
43
+ end
44
+ puts "**********************************************==== model_attributes"
45
+ puts YAML.dump(model_attributes)
46
+
47
+ puts "wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf wtf "
48
+ puts YAML.dump(model_object.sitemap_capture_attributes(model_attributes))
49
+
50
+ values = values.merge(model_object.sitemap_capture_attributes(model_attributes))
51
+ puts "---------------------------------==== values again"
52
+ puts YAML.dump(values)
53
+ end
54
+
55
+ if values[:canonical].blank?
56
+ route = Rails.application.routes.find_route_via_path(request.path)
57
+ url_options = sitemap_url_options(values)
58
+ values[:canonical] = self.send("#{route.name}_url", url_options)
59
+ values[:loc] = values[:canonical]
60
+ else
61
+ values[:loc] = values[:canonical]
62
+ end
63
+ puts "............................ values"
64
+ puts YAML.dump(values)
65
+
66
+ rows.push(values)
67
+
68
+ self.sitemap_meta_data = values
69
+
70
+ else
71
+
72
+ data_rows = []
73
+
74
+ if !options[:handler][:block].blank?
75
+
76
+ data_rows = options[:handler][:block].call(options)
77
+
78
+ elsif !options[:handler][:model].blank?
79
+
80
+ data_rows = options[:handler][:model].send(:all)
81
+
82
+ elsif !options[:model].blank?
83
+
84
+ # this model is not set by developer
85
+ # it is set in code in sitemap_build using get_model_class
86
+ data_rows = options[:model].send(:all)
87
+
88
+ end
89
+
90
+ # data_rows may have changed from an Array to a model object.
91
+ # Make data_rows an Array if it has been switched to an model object.
92
+ if data_rows.kind_of?(ActiveRecord::Base)
93
+ data_rows = [data_rows]
94
+ end
95
+
96
+ # only process the first row since we are processing the index action of the controller
97
+ if data_rows.kind_of?(Array) && data_rows.length > 0
98
+
99
+ data_rows.each do |data_row|
100
+
101
+ # this is how we are overriding attributes defined on the controller with attributes
102
+ # defined on the model. The model attributes win!
103
+ # same thing for segments below.
104
+ model_attributes = attributes
105
+ if data_row.is_sitemap_attributes_defined?
106
+ model_attributes = data_row.sitemap_stripped_attributes(options[:action_name])
107
+ end
108
+
109
+ row_values = values.merge(data_row.sitemap_capture_attributes(model_attributes))
110
+
111
+ url_options = sitemap_url_options(row_values)
112
+
113
+ if row_values[:canonical].blank?
114
+
115
+ segments = data_row.sitemap_capture_segments(model_attributes, options[:route].segments)
116
+ row_values[:canonical] = self.send("#{options[:route].name}_url", url_options.merge(segments))
117
+ row_values[:loc] = row_values[:canonical]
118
+
119
+ else
120
+ row_values[:loc] = row_values[:canonical]
121
+ end
122
+
123
+ rows.push(row_values)
124
+
125
+ end
126
+
127
+ end
128
+
129
+ end
130
+
131
+ return sitemap_url_limit(rows, options[:handler])
132
+ end
133
+
134
+ ###################################################################################
135
+ #def sitemap_show(options = {})
136
+ #rows = []
137
+
138
+ ## always start off with default values from config, then, simply overwrite them as we progress.
139
+ #values = sitemap_defaults
140
+
141
+ #if options[:source] == :meta_data
142
+
143
+ ## if the source is meta_data, that means this method call is the result of a user
144
+ ## requesting an index page. Therefore, the controller action_name variable should be set.
145
+ ## so, go ahead and use it.
146
+
147
+ #lastmod = self.sitemap_static_lastmod(controller_name, action_name)
148
+ #unless lastmod.blank?
149
+ #values[:lastmod] = lastmod
150
+ #end
151
+
152
+ ## first, capture the values from the controller.
153
+ #attributes = self.sitemap_stripped_attributes(action_name)
154
+ #values = values.merge(self.sitemap_capture_attributes(attributes))
155
+
156
+ #if options[:handler][:first_model]
157
+ #model_object = sitemap_first_model(options[:handler])
158
+ #unless model_object.blank?
159
+ #values = values.merge(model_object.sitemap_capture_attributes(attributes))
160
+ #end
161
+ #end
162
+
163
+ #route = Rails.application.routes.find_route_via_path(request.path)
164
+ #values[:loc] = self.send("#{route.name}_url", params.merge({format: values[:url_format]}))
165
+ #values[:canonical] = values[:loc]
166
+
167
+ #rows.push(values)
168
+
169
+ #self.sitemap_meta_data = values
170
+
171
+ #elsif options[:source] == :sitemap
172
+
173
+ #begin
174
+
175
+ #route = options[:route]
176
+
177
+ #lastmod = self.sitemap_static_lastmod(route.controller_name, route.action_name)
178
+ #unless lastmod.blank?
179
+ #values[:lastmod] = lastmod
180
+ #end
181
+
182
+ #attributes = self.sitemap_stripped_attributes(options[:action_name])
183
+ #values = values.merge(self.sitemap_capture_attributes(attributes))
184
+
185
+ #data_rows = []
186
+
187
+ #if !options[:handler][:block].blank?
188
+
189
+ #data_rows = options[:handler][:block].call(options)
190
+
191
+ #elsif !options[:handler][:model].blank?
192
+
193
+ #data_rows = options[:handler][:model].send(:all)
194
+
195
+ #elsif !options[:model].blank?
196
+
197
+ #data_rows = options[:model].send(:all)
198
+
199
+ #end
200
+
201
+ #unless data_rows.kind_of?(Array)
202
+ #if data_rows.kind_of?(ActiveRecord::Base)
203
+ #data_rows = [data_rows]
204
+ #end
205
+ #end
206
+
207
+ #data_rows.each do |data_row|
208
+
209
+ #row_values = values.merge(data_row.sitemap_capture_attributes(attributes))
210
+
211
+ #segments = data_row.sitemap_capture_segments(attributes, route.segments)
212
+
213
+ #row_values[:canonical] = self.send("#{route.name}_url", {format: row_values[:url_format]}.merge(segments))
214
+ #row_values[:loc] = row_values[:canonical]
215
+ #rows.push(row_values)
216
+
217
+ #end
218
+
219
+ #rescue Exception => e
220
+ #DuckMap.logger.exception(e)
221
+ #end
222
+
223
+ #end
224
+
225
+ #return sitemap_url_limit(rows, values)
226
+ #end
227
+
228
+ end
229
+
230
+ end
231
+ end
@@ -0,0 +1,45 @@
1
+ # DONE
2
+ module DuckMap
3
+
4
+ ##################################################################################
5
+ # LastMod is a Date/Time parsing class.
6
+ class LastMod
7
+
8
+ ##################################################################################
9
+ # Parses a DateTime represented as a String into a DateTime value. If value is String, then, it is parsed
10
+ # and returns a DateTime Object if valid. Otherwise, it returns nil. If the value passed is a Date, Time, or DateTime, then,
11
+ # it is returned as is.
12
+ # @param [String] A DateTime represented as a String to parse or can be a Date, Time, or DateTime value.
13
+ # @return [DateTime] DateTime value.
14
+ def self.to_date(value)
15
+ date_value = nil
16
+
17
+ if value.kind_of?(String)
18
+
19
+ formats = ["%m/%d/%Y %H:%M:%S", "%m-%d-%Y %H:%M:%S", "%Y-%m-%d %H:%M:%S", "%m/%d/%Y", "%m-%d-%Y", "%Y-%m-%d"]
20
+ Time::DATE_FORMATS.each_value { |x| formats.push(x)}
21
+
22
+ formats.each do |format|
23
+
24
+ begin
25
+
26
+ date_value = Time.strptime(value, format)
27
+ break
28
+
29
+ rescue Exception => e
30
+ date_value = nil
31
+ end
32
+
33
+ end
34
+
35
+ elsif value.kind_of?(Date) || value.kind_of?(Time) || value.kind_of?(DateTime)
36
+ date_value = value
37
+
38
+ end
39
+
40
+ return date_value
41
+ end
42
+
43
+ end
44
+
45
+ end
@@ -0,0 +1,82 @@
1
+ module DuckMap
2
+
3
+ ##################################################################################
4
+ # Lists all of the sitemaps defined in config/routes.rb
5
+ class List
6
+
7
+ ##################################################################################
8
+ def build(options = {})
9
+
10
+ key = options[:key].blank? ? :all : options[:key].to_s.downcase.to_sym
11
+
12
+ contents = options.has_key?(:contents) ? true : false
13
+
14
+ contents = key.eql?(:all) ? contents : true
15
+
16
+ puts "Searching for route: #{key}"
17
+
18
+ routes = []
19
+ if key.eql?(:all)
20
+
21
+ routes = Rails.application.routes.sitemap_routes_only
22
+
23
+ else
24
+
25
+ route = Rails.application.routes.find_route_via_name("#{key}_sitemap")
26
+ if route.blank?
27
+ puts "Unable to find route: #{key}"
28
+ else
29
+ routes.push(route)
30
+ end
31
+
32
+ end
33
+
34
+ if routes.length > 0
35
+
36
+ puts "\r\n\r\nSitemap routes"
37
+ puts %(#{"route name".ljust(40)} controller_name#action_name)
38
+ puts %(#{"".ljust(40)} path)
39
+ puts "---------------------------------------------------------------------------------------------------------------"
40
+
41
+ routes.each do |route|
42
+
43
+ default_route = !route.name.blank? && route.name.to_sym.eql?(:sitemap_sitemap) ? " (DEFAULT)" : ""
44
+
45
+ puts %(\r\n#{(route.name.blank? ? "".ljust(40) : "#{route.name}#{default_route}".ljust(40))} #{route.controller_name}##{route.action_name})
46
+
47
+ puts %(#{"".ljust(40)} #{route.path.spec})
48
+
49
+ if contents
50
+
51
+ sitemap_routes = Rails.application.routes.sitemap_routes(route)
52
+ if sitemap_routes.length > 0
53
+ #puts %(\r\n#{"".ljust(5)} route name controller_name#action_name)
54
+ #puts %(#{"".ljust(5)} path)
55
+ puts %(#{"".ljust(5)} --------------------------------------------------------------)
56
+
57
+ sitemap_routes.each do |sitemap_route|
58
+
59
+ puts %(#{"".ljust(5)} #{(sitemap_route.name.blank? ? "".ljust(34) : "#{sitemap_route.name}".ljust(34))} #{sitemap_route.controller_name}##{sitemap_route.action_name})
60
+
61
+ puts %(#{"".ljust(40)} #{sitemap_route.path.spec})
62
+
63
+ end
64
+
65
+ else
66
+ puts %(#{"".ljust(10)} No routes)
67
+ puts %(#{"".ljust(10)} ----------------------------------------------)
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ else
75
+ puts "Sorry, no routes found..."
76
+ end
77
+
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -0,0 +1,193 @@
1
+ # DONE
2
+ module DuckMap
3
+
4
+ # Logger class generates logging information in log/duckmap.log
5
+ class Logger < ::Logger
6
+
7
+ CLEAR = "\e[0m"
8
+ BOLD = "\e[1m"
9
+
10
+ # Colors
11
+ BLACK = "\e[30m"
12
+ RED = "\e[31m"
13
+ GREEN = "\e[32m"
14
+ YELLOW = "\e[33m"
15
+ BLUE = "\e[34m"
16
+ MAGENTA = "\e[35m"
17
+ CYAN = "\e[36m"
18
+ WHITE = "\e[37m"
19
+
20
+ ##################################################################################
21
+ # Instance of DuckMap::Logger
22
+ def self.logger
23
+ dir = defined?(Rails) ? Rails.root : "."
24
+ return @@logger ||= self.new("#{dir}/log/duckmap.log", INFO)
25
+ end
26
+
27
+ ##################################################################################
28
+ # Converts a Symbol to a valid log level and vise versa.
29
+ # @return [Object] The return value is based on the argument :key.
30
+ # - If you pass a Symbol, you get a log level.
31
+ # - If you pass a log level, you get a Symbol.
32
+ def self.to_severity(key)
33
+ key = key.kind_of?(Fixnum) || key.kind_of?(Symbol) ? key : :badkey
34
+ values = {debug: DEBUG, info: INFO, warn: WARN, error: ERROR, fatal: FATAL, unknown: UNKNOWN}
35
+ value = values.map.find {|value| value[key.kind_of?(Symbol) ? 0 : 1].eql?(key)}
36
+ return value.blank? ? nil : value[key.kind_of?(Symbol) ? 1 : 0]
37
+ end
38
+
39
+ def to_severity(key)
40
+ return self.class.to_severity(key)
41
+ end
42
+
43
+ ##################################################################################
44
+ # Sets the logging level.
45
+ # @param [Symbol, Number] key A value representing the desired logging level.
46
+ # Valid values: :debug, :info, :warn, :error, :fatal, :unknown
47
+ # DEBUG, INFO, WARN, ERROR, FATAL, UNKNOWN
48
+ # @return [Number]
49
+ def self.log_level=(key)
50
+ value = self.to_severity(key)
51
+
52
+ unless value.blank?
53
+ @@log_level = value
54
+ self.logger.level = value
55
+ end
56
+ return self.logger.level
57
+ end
58
+
59
+ def log_level=(key)
60
+ self.class.log_level = key
61
+ end
62
+
63
+ ##################################################################################
64
+ # Gets the current logging level.
65
+ # @return [Number]
66
+ def self.log_level
67
+ @@log_level ||= self.logger.level
68
+ return @@log_level
69
+ end
70
+
71
+ def log_level
72
+ return self.class.log_level
73
+ end
74
+
75
+ ##################################################################################
76
+ def self.full_exception=(value)
77
+ @@full_exception = value
78
+ return @@full_exception
79
+ end
80
+
81
+ def full_exception=(value)
82
+ self.class.full_exception = value
83
+ end
84
+
85
+ ##################################################################################
86
+ def self.full_exception
87
+ unless defined?(@@full_exception)
88
+ @@full_exception = false
89
+ end
90
+ return @@full_exception
91
+ end
92
+
93
+ def full_exception
94
+ return self.class.full_exception
95
+ end
96
+
97
+ ##################################################################################
98
+ def console(msg = nil, progname = nil, &block)
99
+ if self.is_show_console?
100
+ STDOUT.puts msg
101
+ end
102
+ info(msg, progname, &block)
103
+ return nil
104
+ end
105
+
106
+ ##################################################################################
107
+ def self.show_console
108
+ unless defined?(@@show_console)
109
+ @@show_console = true
110
+ end
111
+ return @@show_console
112
+ end
113
+
114
+ def self.show_console=(value)
115
+ @@show_console = value
116
+ end
117
+
118
+ def self.is_show_console?
119
+ return self.show_console
120
+ end
121
+
122
+ def show_console
123
+ return self.class.show_console
124
+ end
125
+
126
+ def show_console=(value)
127
+ self.class.show_console = value
128
+ end
129
+
130
+ def is_show_console?
131
+ return self.class.is_show_console?
132
+ end
133
+
134
+ ##################################################################################
135
+ def exception(exception, progname = nil, &block)
136
+
137
+ self.error(exception.message, progname, &block)
138
+
139
+ base_dir = Rails.root.to_s
140
+ exception.backtrace.each do |x|
141
+ if x.include?(base_dir) || self.full_exception
142
+ self.error(x, progname, &block)
143
+ end
144
+ end
145
+
146
+ return nil
147
+ end
148
+
149
+ ##################################################################################
150
+ def debug(msg = nil, progname = nil, &block)
151
+ add(DEBUG, color("#{msg}", CYAN), progname, &block)
152
+ return nil
153
+ end
154
+
155
+ ##################################################################################
156
+ def error(msg = nil, progname = nil, &block)
157
+ add(ERROR, color("#{msg}", RED), progname, &block)
158
+ return nil
159
+ end
160
+
161
+ ##################################################################################
162
+ def fatal(msg = nil, progname = nil, &block)
163
+ add(FATAL, color("#{msg}", RED), progname, &block)
164
+ return nil
165
+ end
166
+
167
+ ##################################################################################
168
+ def info(msg = nil, progname = nil, &block)
169
+ add(INFO, "#{msg}", progname, &block)
170
+ return nil
171
+ end
172
+
173
+ ##################################################################################
174
+ def warn(msg = nil, progname = nil, &block)
175
+ add(WARN, color("#{msg}", YELLOW), progname, &block)
176
+ return nil
177
+ end
178
+
179
+ ##################################################################################
180
+ def unknown(msg = nil, progname = nil, &block)
181
+ add(INFO, color("#{msg}", WHITE), progname, &block)
182
+ return nil
183
+ end
184
+
185
+ #############################################################################################
186
+ def color(text, color, bold=false)
187
+ color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
188
+ bold = bold ? BOLD : ""
189
+ return "#{bold}#{color}#{text}#{CLEAR}"
190
+ end
191
+
192
+ end
193
+ end