Class: ATDIS::Feed

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

Constant Summary collapse

VALID_OPTIONS =
%i[page street suburb postcode lodgement_date_start
lodgement_date_end last_modified_date_start last_modified_date_end].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, timezone, ignore_ssl_certificate = false) ⇒ 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 timezone - a string (e.g. “Sydney”) for the timezone in which times are returned (Note: times in the feeds that have timezones specified get converted to the timezone given while times in the feed which don’t have a timezone specified get interpreted in the given timezone) See api.rubyonrails.org/classes/ActiveSupport/TimeZone.html for the list of possible timezone strings



21
22
23
24
25
# File 'lib/atdis/feed.rb', line 21

def initialize(base_url, timezone, ignore_ssl_certificate = false)
  @base_url = base_url
  @timezone = timezone
  @ignore_ssl_certificate = ignore_ssl_certificate
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



7
8
9
# File 'lib/atdis/feed.rb', line 7

def base_url
  @base_url
end

#ignore_ssl_certificateObject (readonly)

Returns the value of attribute ignore_ssl_certificate.



7
8
9
# File 'lib/atdis/feed.rb', line 7

def ignore_ssl_certificate
  @ignore_ssl_certificate
end

#timezoneObject (readonly)

Returns the value of attribute timezone.



7
8
9
# File 'lib/atdis/feed.rb', line 7

def timezone
  @timezone
end

Class Method Details

.base_url_from_url(url) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/atdis/feed.rb', line 44

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("/").to_s
  u.to_s
end

.escape(value) ⇒ Object

Escape but leave commas unchanged (which are valid in query strings)



90
91
92
# File 'lib/atdis/feed.rb', line 90

def self.escape(value)
  CGI.escape(value.to_s).gsub("%2C", ",")
end

.options_from_url(url) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/atdis/feed.rb', line 56

def self.options_from_url(url)
  u = URI.parse(url)
  options = query_to_options(u.query)
  %i[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.each_key do |key|
    options.delete(key) unless VALID_OPTIONS.include?(key)
  end
  options
end

.options_to_query(options) ⇒ Object

Turn an options hash of the form “bar”, hello: “sir” into a query string of the form “foo=bar&hello=sir”



96
97
98
99
100
101
102
103
104
# File 'lib/atdis/feed.rb', line 96

def self.options_to_query(options)
  if options.empty?
    nil
  else
    options.sort { |a, b| a.first.to_s <=> b.first.to_s }
           .map { |k, v| "#{k}=#{escape(v)}" }
           .join("&")
  end
end

.query_to_options(query) ⇒ Object

Turn a query string of the form “foo=bar&hello=sir” to “bar”, hello: sir“



80
81
82
83
84
85
86
87
# File 'lib/atdis/feed.rb', line 80

def self.query_to_options(query)
  options = {}
  (query || "").split("&").each do |t|
    key, value = t.split("=")
    options[key.to_sym] = (CGI.unescape(value) if value)
  end
  options
end

Instance Method Details

#application(id) ⇒ Object



75
76
77
# File 'lib/atdis/feed.rb', line 75

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

#application_url(id) ⇒ Object



40
41
42
# File 'lib/atdis/feed.rb', line 40

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

#applications(options = {}) ⇒ Object



71
72
73
# File 'lib/atdis/feed.rb', line 71

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

#applications_url(options = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/atdis/feed.rb', line 27

def applications_url(options = {})
  invalid_options = options.keys - VALID_OPTIONS

  raise "Unexpected options used: #{invalid_options.join(',')}" unless invalid_options.empty?

  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