Sha256: f312a2c537f36865d8a419e1916bcbe18737b796f48b8dd89f3d91dd368a80f0

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

module Qa::Authorities
  class Crossref::GenericAuthority < Base
    include WebServiceBase
    class_attribute :label, :identifier
    attr_reader :subauthority

    def initialize(subauthority)
      super()
      @subauthority = subauthority
    end

    # Create a label from the crossref result hash
    #
    # @param item [Hash] the crossref result
    # @return [String] a label combining the name, alt-names and location
    self.label = lambda do |item|
      [item['name'],
       item['alt-names'].blank? ? nil : "(#{item['alt-names'].join(', ')})",
       item['location']].compact.join(', ')
    end

    def search(q)
      parse_authority_response(json(build_query_url(q)))
    end

    def build_query_url(q)
      query = ERB::Util.url_encode(untaint(q))
      "http://api.crossref.org/#{subauthority}?query=#{query}"
    end

    def untaint(q)
      q.gsub(/[^\w\s-]/, '')
    end

    def find(id)
      json(find_url(id))
    end

    def find_url(id)
      "http://api.crossref.org/#{subauthority}/#{id}"
    end

    private

      # Reformats the data received from the service
      def parse_authority_response(response)
        response['message']['items'].map do |result|
          case subauthority
          when 'funders'
            { id: result['id'],
              uri: result['uri'],
              label: label.call(result),
              value: result['name'] }
          when 'journals'
            { id: result['ISSN'].first,
              label: result['title'],
              publisher: result['publisher'],
              issn: result['ISSN'] }

          end
        end
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
qa-5.14.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.13.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.12.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.11.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.10.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.9.0 lib/qa/authorities/crossref/generic_authority.rb