Class: ATDIS::Feed
- Inherits:
-
Object
- Object
- ATDIS::Feed
- 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
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#ignore_ssl_certificate ⇒ Object
readonly
Returns the value of attribute ignore_ssl_certificate.
-
#timezone ⇒ Object
readonly
Returns the value of attribute timezone.
Class Method Summary collapse
- .base_url_from_url(url) ⇒ Object
-
.escape(value) ⇒ Object
Escape but leave commas unchanged (which are valid in query strings).
- .options_from_url(url) ⇒ Object
-
.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”.
-
.query_to_options(query) ⇒ Object
Turn a query string of the form “foo=bar&hello=sir” to “bar”, hello: sir“.
Instance Method Summary collapse
- #application(id) ⇒ Object
- #application_url(id) ⇒ Object
- #applications(options = {}) ⇒ Object
- #applications_url(options = {}) ⇒ Object
-
#initialize(base_url, timezone, ignore_ssl_certificate = false) ⇒ Feed
constructor
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.
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_url ⇒ Object (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_certificate ⇒ Object (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 |
#timezone ⇒ Object (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) = (u.query) VALID_OPTIONS.each do |o| .delete(o) end u.query = () 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.(url) u = URI.parse(url) = (u.query) %i[lodgement_date_start lodgement_date_end last_modified_date_start last_modified_date_end].each do |k| [k] = Date.parse([k]) if [k] end [:page] = [:page].to_i if [:page] # Remove invalid options .each_key do |key| .delete(key) unless VALID_OPTIONS.include?(key) end 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.() if .empty? nil else .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) = {} (query || "").split("&").each do |t| key, value = t.split("=") [key.to_sym] = (CGI.unescape(value) if value) end 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( = {}) Models::Page.read_url(applications_url(), 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( = {}) = .keys - VALID_OPTIONS raise "Unexpected options used: #{.join(',')}" unless .empty? [:street] = [:street].join(",") if [:street].respond_to?(:join) [:suburb] = [:suburb].join(",") if [:suburb].respond_to?(:join) [:postcode] = [:postcode].join(",") if [:postcode].respond_to?(:join) q = Feed.() "#{base_url}/applications.json" + (q ? "?#{q}" : "") end |