Class: ATDIS::Feed

Inherits:
Object
  • Object
show all
Defined in:
lib/atdis/feed.rb

Constant Summary collapse

VALID_OPTIONS =
[:page, :street, :suburb, :postcode, :lodgement_date_start, :lodgement_date_end, :last_modified_date_start, :last_modified_date_end]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url) ⇒ Feed

base_url - the base url from which the urls for all atdis urls are made It should be of the form: www.council.nsw.gov.au/atdis/1.0



12
13
14
# File 'lib/atdis/feed.rb', line 12

def initialize(base_url)
  @base_url = base_url
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



5
6
7
# File 'lib/atdis/feed.rb', line 5

def base_url
  @base_url
end

Class Method Details

.base_url_from_url(url) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/atdis/feed.rb', line 33

def self.base_url_from_url(url)
  u = URI.parse(url)
  options = query_to_options(u.query)
  VALID_OPTIONS.each do |o|
    options.delete(o)
  end
  u.query = options_to_query(options)
  u.fragment = nil
  u.path = "/" + u.path.split("/")[1..-2].join("/")
  u.to_s
end

.options_from_url(url) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/atdis/feed.rb', line 45

def self.options_from_url(url)
  u = URI.parse(url)
  options = query_to_options(u.query)
  [:lodgement_date_start, :lodgement_date_end, :last_modified_date_start, :last_modified_date_end].each do |k|
    options[k] = Date.parse(options[k]) if options[k]
  end
  options[:page] = options[:page].to_i if options[:page]
  # Remove invalid options
  options.keys.each do |key|
    if !VALID_OPTIONS.include?(key)
      options.delete(key)
    end
  end
  options
end

Instance Method Details

#application(id) ⇒ Object



65
66
67
# File 'lib/atdis/feed.rb', line 65

def application(id)
  Models::Application.read_url(application_url(id))
end

#application_url(id) ⇒ Object



29
30
31
# File 'lib/atdis/feed.rb', line 29

def application_url(id)
  "#{base_url}/#{CGI::escape(id)}.json"
end

#applications(options = {}) ⇒ Object



61
62
63
# File 'lib/atdis/feed.rb', line 61

def applications(options = {})
  Models::Page.read_url(applications_url(options))
end

#applications_url(options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/atdis/feed.rb', line 16

def applications_url(options = {})
  invalid_options = options.keys - VALID_OPTIONS
  if !invalid_options.empty?
    raise "Unexpected options used: #{invalid_options.join(',')}"
  end
  options[:street] = options[:street].join(",") if options[:street].respond_to?(:join)
  options[:suburb] = options[:suburb].join(",") if options[:suburb].respond_to?(:join)
  options[:postcode] = options[:postcode].join(",") if options[:postcode].respond_to?(:join)

  q = Feed.options_to_query(options)
  "#{base_url}/applications.json" + (q ? "?#{q}" : "")
end