Module: DataPackage::Helpers
Instance Method Summary collapse
- #base_path(path_or_url) ⇒ Object
-
#dereference_descriptor(resource, base_path: nil, reference_fields: nil) ⇒ Object
Dereference a resource that can be a URL or path to a JSON file or a hash Returns a Hash with all values that are URLs or paths dereferenced.
- #is_fully_qualified_url?(string) ⇒ Boolean
- #is_safe_path?(string) ⇒ Boolean
- #join_paths(base_path, resource) ⇒ Object
-
#load_json(reference) ⇒ Object
Load JSON from path or URL; Raises: Errno::ENOENT, OpenURI::HTTPError, SocketError, JSON::ParserError.
-
#resolve_json_reference(reference, deep_dereference: false, base_path: nil) ⇒ Object
Resolve a reference to a JSON file; Returns the JSON as hash Raises JSON::ParserError, OpenURI::HTTPError, SocketError or TypeError for invalid references.
Instance Method Details
#base_path(path_or_url) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/datapackage/helpers.rb', line 53 def base_path(path_or_url) path_or_url = path_or_url.to_s if path_or_url.empty? nil elsif path_or_url =~ /\A#{URI::regexp}\z/ uri = URI.parse path_or_url return "#{uri.scheme}://#{uri.host}#{File.dirname uri.path}".chomp('/') else if File.directory?(path_or_url) return path_or_url else return File. File.dirname path_or_url end end end |
#dereference_descriptor(resource, base_path: nil, reference_fields: nil) ⇒ Object
Dereference a resource that can be a URL or path to a JSON file or a hash Returns a Hash with all values that are URLs or paths dereferenced
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/datapackage/helpers.rb', line 6 def dereference_descriptor(resource, base_path: nil, reference_fields: nil) = { base_path: base_path, reference_fields: reference_fields, } case resource when Hash resource.inject({}) do |new_resource, (key, val)| if reference_fields.nil? || reference_fields.include?(key) new_resource[key] = dereference_descriptor(val, **) else new_resource[key] = val end new_resource end when Enumerable resource.map{ |el| dereference_descriptor(el, **)} when String begin resolve_json_reference(resource, deep_dereference: true, base_path: base_path) rescue Errno::ENOENT resource end else resource end end |
#is_fully_qualified_url?(string) ⇒ Boolean
83 84 85 86 87 88 |
# File 'lib/datapackage/helpers.rb', line 83 def is_fully_qualified_url?(string) uri = URI.parse(string) uri.is_a?(URI::HTTP) && !uri.host.nil? rescue URI::InvalidURIError false end |
#is_safe_path?(string) ⇒ Boolean
90 91 92 93 94 95 |
# File 'lib/datapackage/helpers.rb', line 90 def is_safe_path?(string) path = Pathname.new(string) return false if path.absolute? return false unless /^\.+$/.match(path.to_s.split('/').first).nil? true end |
#join_paths(base_path, resource) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/datapackage/helpers.rb', line 69 def join_paths(base_path, resource) if base_path.nil? || base_path.empty? resource elsif base_path =~ /\A#{URI::regexp}\z/ URI.join(base_path, resource).to_s elsif File.directory?(base_path) File.join(base_path, resource).to_s elsif File.file?(base_path) base_path else resource end end |
#load_json(reference) ⇒ Object
Load JSON from path or URL; Raises: Errno::ENOENT, OpenURI::HTTPError, SocketError, JSON::ParserError
49 50 51 |
# File 'lib/datapackage/helpers.rb', line 49 def load_json(reference) JSON.parse open(reference).read end |
#resolve_json_reference(reference, deep_dereference: false, base_path: nil) ⇒ Object
Resolve a reference to a JSON file; Returns the JSON as hash Raises JSON::ParserError, OpenURI::HTTPError, SocketError or TypeError for invalid references
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/datapackage/helpers.rb', line 36 def resolve_json_reference(reference, deep_dereference: false, base_path: nil) # Try to extract JSON from file or webpage reference = join_paths(base_path, reference) resolved_reference = load_json(reference) if deep_dereference == true dereference_descriptor(resolved_reference, base_path: base_path) else resolved_reference end end |