Module: TogoWS::URLBuilder

Defined in:
lib/togows/url_builder.rb

Class Method Summary collapse

Class Method Details

.convert(source, format = nil) ⇒ Object



26
27
28
29
# File 'lib/togows/url_builder.rb', line 26

def convert(source, format = nil)
  data_source, target_format = split_source_format(source, format)
  "/convert/#{escape_segment(data_source)}.#{escape_segment(target_format)}"
end

.databases(kind) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/togows/url_builder.rb', line 31

def databases(kind)
  case kind
  when "entry" then "/entry"
  when "search" then "/search"
  else
    raise Error, "database list must be entry or search"
  end
end

.entry(database, ids, field = nil, format = nil) ⇒ Object



9
10
11
12
13
14
# File 'lib/togows/url_builder.rb', line 9

def entry(database, ids, field = nil, format = nil)
  id_list = Array(ids).join(",")
  parts = ["entry", escape_segment(database), escape_segment(id_list)]
  parts << escape_path(field) if field && !field.empty?
  with_format("/#{parts.join('/')}", format)
end

.escape_path(value) ⇒ Object



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

def escape_path(value)
  value.to_s.split("/", -1).map { |part| escape_segment(part) }.join("/")
end

.escape_segment(value) ⇒ Object



54
55
56
# File 'lib/togows/url_builder.rb', line 54

def escape_segment(value)
  URI.encode_www_form_component(value.to_s)
end

.full_url(base_url, path) ⇒ Object



49
50
51
52
# File 'lib/togows/url_builder.rb', line 49

def full_url(base_url, path)
  base = base_url.to_s.sub(%r{/*\z}, "")
  base + path
end

.search(database, query, offset = nil, limit = nil, count = false, format = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/togows/url_builder.rb', line 16

def search(database, query, offset = nil, limit = nil, count = false, format = nil)
  parts = ["search", escape_segment(database), escape_segment(query)]
  if count
    parts << "count"
  elsif offset && limit
    parts << "#{offset},#{limit}"
  end
  with_format("/#{parts.join('/')}", format)
end

.split_source_format(source, format) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/togows/url_builder.rb', line 68

def split_source_format(source, format)
  if format && !format.empty?
    [source, format]
  elsif source.to_s.include?(".")
    source.to_s.split(".", 2)
  else
    raise Error, "missing target format; use SOURCE.FORMAT or --to FORMAT"
  end
end

.ucsc(database = nil, table = nil, query = nil, offset = nil, limit = nil, format = nil) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/togows/url_builder.rb', line 40

def ucsc(database = nil, table = nil, query = nil, offset = nil, limit = nil, format = nil)
  parts = %w[api ucsc]
  parts << escape_segment(database) if database && !database.empty?
  parts << escape_segment(table) if table && !table.empty?
  parts << escape_path(query) if query && !query.empty?
  parts << "#{offset},#{limit}" if offset && limit
  with_format("/#{parts.join('/')}", format)
end

.with_format(path, format) ⇒ Object



62
63
64
65
66
# File 'lib/togows/url_builder.rb', line 62

def with_format(path, format)
  return path if format.nil? || format.empty?

  "#{path}.#{escape_segment(format)}"
end