Sha256: 183df4c4d460cfbe198fb2b18098af16d6276ea23e93b8a73412d96d9ca5ecd0
Contents?: true
Size: 1.91 KB
Versions: 1
Compression:
Stored size: 1.91 KB
Contents
# encoding: utf-8 # module Mocrata # @attr_reader [String] original the original Socrata dataset URL # class DatasetUrl attr_reader :original # Construct a new DatasetUrl instance # # @param original [String] the original Socrata dataset URL # # @return [Mocrata::DatasetUrl] the instance # # @example # url = Mocrata::DatasetUrl.new('http://data.sfgov.org/resource/funx-qxxn') # def initialize(original) @original = original end # Normalize a Socrata dataset URL. Ensures https protocol. Removes query # string and fragment, if any. # # @return [String] the normalized URL # def normalize uri = URI(self.class.ensure_protocol(original)) uri.scheme = 'https' uri.fragment = nil uri.query = nil self.class.strip_format(uri.to_s) end # Validate the original URL against the expected Socrata dataset URL # pattern # # @raise [Mocrata::DatasetUrl::InvalidError] if the URL is invalid # def validate! unless original =~ VALID_PATTERN raise InvalidError.new("Invalid URL: #{original.inspect}") end true end class << self # Ensure that a URL has a valid protocol # # @param url [String] the url with or without protocol # # @return [String] the url with protocol # def ensure_protocol(url) if url =~ /\A\/\// url = "https:#{url}" elsif url !~ /\Ahttps?:\/\// url = "https://#{url}" end url end # Strip explicit format from a given URL if present # # @param url [String] the url with or without format # # @return [String] the url without format # def strip_format(url) url.gsub(/\.[a-zA-Z]+\Z/, '') end end private VALID_PATTERN = /\/resource\// class InvalidError < StandardError; end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mocrata-0.0.1 | lib/mocrata/dataset_url.rb |