Sha256: 84cecb5be557bcfb056e5f06a760cb17f213819eac06029237e877b0c9b2a5b7

Contents?: true

Size: 921 Bytes

Versions: 3

Compression:

Stored size: 921 Bytes

Contents

require 'uri'

module Indieweb
  module PostTypes
    module Identifier
      module Base
        def type_from(data)
          data = entry_data_from(data)
          identifier_from(data)
        end

        def valid_url?(url)
          uri = URI(actual_url_from(url))
          %w[http https].include?(uri.scheme)
        rescue URI::InvalidURIError
          false
        end

        private

        def entry_data_from(data)
          return data['properties'] if data['properties']
          return data unless data.key?('items')
          data['items'].first['properties']
        end

        def actual_url_from(url)
          return url if url.is_a?(String)
          return url[0] unless url[0].is_a?(Hash)
          url_from_citation(url[0])
        end

        def url_from_citation(data)
          data['properties'] ? data['properties']['url'][0] : data['url']
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
indieweb-post_types-0.3.2 lib/indieweb/post_types/identifier/base.rb
indieweb-post_types-0.3.1 lib/indieweb/post_types/identifier/base.rb
indieweb-post_types-0.3.0 lib/indieweb/post_types/identifier/base.rb