Sha256: 4517c6e6dc52c90c75d5e90bd5b63ac0a14a62f60a4bccc63ab0b2f1738e3319

Contents?: true

Size: 1.58 KB

Versions: 25

Compression:

Stored size: 1.58 KB

Contents

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

    def initialize(subauthority)
      @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

25 entries across 25 versions & 1 rubygems

Version Path
qa-5.8.1 lib/qa/authorities/crossref/generic_authority.rb
qa-5.8.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.7.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.6.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.5.2 lib/qa/authorities/crossref/generic_authority.rb
qa-4.3.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.5.1 lib/qa/authorities/crossref/generic_authority.rb
qa-5.5.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.4.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.3.1 lib/qa/authorities/crossref/generic_authority.rb
qa-5.3.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.2.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.1.0 lib/qa/authorities/crossref/generic_authority.rb
qa-5.0.0 lib/qa/authorities/crossref/generic_authority.rb
qa-4.2.4 lib/qa/authorities/crossref/generic_authority.rb
qa-4.2.3 lib/qa/authorities/crossref/generic_authority.rb
qa-4.2.2 lib/qa/authorities/crossref/generic_authority.rb
qa-4.2.1 lib/qa/authorities/crossref/generic_authority.rb
qa-4.2.0 lib/qa/authorities/crossref/generic_authority.rb
qa-4.1.1 lib/qa/authorities/crossref/generic_authority.rb