Module: Gonzui::URIMaker

Included in:
GonzuiAbstractServlet, SnippetMaker
Defined in:
lib/gonzui/webapp/uri.rb

Constant Summary collapse

ParamTable =
{
  :display_language      => QueryValue.new("dl", nil,   :to_s),
  :from                  => QueryValue.new("f",  0,     :to_i),
  :format                => QueryValue.new("fm", "all",  :to_s),
  :grep_pattern          => QueryValue.new("g",  nil,   :to_s),
  :license               => QueryValue.new("l",  "all",  :to_s),
  :nresults_per_page     => QueryValue.new("n",  10,    :to_i),
  :phrase_query          => QueryValue.new("pq", nil,   :to_s),
  :basic_query           => QueryValue.new("q",  "",    :to_s),
  :target_type           => QueryValue.new("tt", nil,   :to_s),
}

Instance Method Summary collapse

Instance Method Details

#decompose_search_query(search_query) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/gonzui/webapp/uri.rb', line 28

def decompose_search_query(search_query)
  options = {}
  options[:basic_query] = search_query.string
  options[:format] = search_query.format
  options[:license] = search_query.license
  return options
end

#escape_path(path) ⇒ Object



106
107
108
109
110
# File 'lib/gonzui/webapp/uri.rb', line 106

def escape_path(path)
  path.gsub(%r{[^A-Za-z0-9\-._~!$&'()*+,;=:@/]}) {
    '%' + $&.unpack("H2")[0].upcase
  }
end

#get_default_query_value(long_name) ⇒ Object



36
37
38
# File 'lib/gonzui/webapp/uri.rb', line 36

def get_default_query_value(long_name)
  ParamTable[long_name]
end

#get_query_value(query, long_name) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/gonzui/webapp/uri.rb', line 40

def get_query_value(query, long_name)
  qv = get_default_query_value(long_name)
  assert(qv)
  value = query[qv.short_name]
  if value and not value.empty?
    return value.send(qv.conversion_method)
  else
    return qv.default_value
  end
end

#get_short_name(long_name) ⇒ Object



51
52
53
54
55
# File 'lib/gonzui/webapp/uri.rb', line 51

def get_short_name(long_name)
  qv = ParamTable[long_name]
  raise "unknown variable name" if qv.nil?
  return qv.short_name
end

#make_advanced_search_uri(search_query) ⇒ Object



57
58
59
# File 'lib/gonzui/webapp/uri.rb', line 57

def make_advanced_search_uri(search_query)
  make_uri_general(AdvancedSearchServlet)
end

#make_doc_uri(path = nil) ⇒ Object



61
62
63
# File 'lib/gonzui/webapp/uri.rb', line 61

def make_doc_uri(path = nil)
  make_uri_general(FileHandler, path)
end

#make_google_uri(search_query) ⇒ Object



65
66
67
68
# File 'lib/gonzui/webapp/uri.rb', line 65

def make_google_uri(search_query)
  sprintf("http://www.google.com/search?q=%s", 
          HTTPUtils.escape_form(search_query.string_without_properties))
end

#make_lineno_uri(markup_uri, lineno) ⇒ Object



70
71
72
# File 'lib/gonzui/webapp/uri.rb', line 70

def make_lineno_uri(markup_uri, lineno)
  sprintf("%s#l%d", markup_uri, lineno)
end

#make_markup_uri(path = "", search_query = nil, options = {}) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/gonzui/webapp/uri.rb', line 74

def make_markup_uri(path = "", search_query = nil, options = {})
  if search_query
    decompose_search_query(search_query).each {|k, v|
      options[k] = v
    }
  end
  return make_uri_with_options(MarkupServlet, path, options)
end

#make_search_uri(search_query, options = {}) ⇒ Object



83
84
85
86
# File 'lib/gonzui/webapp/uri.rb', line 83

def make_search_uri(search_query, options = {})
  options.merge!(decompose_search_query(search_query))
  make_uri_with_options(SearchServlet, "", options)
end

#make_search_uri_partial(query_string) ⇒ Object



88
89
90
91
92
# File 'lib/gonzui/webapp/uri.rb', line 88

def make_search_uri_partial(query_string)
  sprintf("%s?%s=", 
          make_uri_general(SearchServlet),
          get_short_name(:basic_query))
end

#make_source_uri(path = nil) ⇒ Object



94
95
96
# File 'lib/gonzui/webapp/uri.rb', line 94

def make_source_uri(path = nil)
  make_uri_general(SourceServlet, path)
end

#make_stat_uri(path = nil) ⇒ Object



98
99
100
# File 'lib/gonzui/webapp/uri.rb', line 98

def make_stat_uri(path = nil)
  make_uri_general(StatisticsServlet, path)
end

#make_top_uriObject



102
103
104
# File 'lib/gonzui/webapp/uri.rb', line 102

def make_top_uri
  make_uri_general(TopPageServlet)
end

#make_uri_general(klass, path = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
# File 'lib/gonzui/webapp/uri.rb', line 112

def make_uri_general(klass, path = nil)
  assert_non_nil(@config)
  if path
    return URI.path_join(@config.base_mount_point,
                         klass.mount_point,
                         escape_path(path))
  else
    return URI.path_join(@config.base_mount_point, klass.mount_point)
  end
end

#make_uri_with_options(klass, path, options) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gonzui/webapp/uri.rb', line 123

def make_uri_with_options(klass, path, options)
  params = []
  options.each {|name, value|
    next if value.nil?
    qv = get_default_query_value(name)
    if qv and value != qv.default_value
      param = sprintf("%s=%s", qv.short_name, HTTPUtils.escape_form(value.to_s))
      params.push(param)
    end
  }
  assert_non_nil(@config)
  uri = URI.path_join(@config.base_mount_point, klass.mount_point)
  uri = URI.path_join(uri, escape_path(path)) unless path.empty?
  uri << "?" + params.join("&") unless params.empty?
  return uri
end