class SearchCrossref
  def initialize(query, opts = {})
    @query = query[:everything]
  end

  def run
    response = conn.get "/works", {
      query: @query,
    }

    results = response.body["message"]["items"].map do |result|
      SearchResult.new(result)
    end

    total_results = response.body["message"]["total-results"]

    return results, total_results
  end

  def conn
    @conn ||= Faraday.new(url: "http://api.crossref.org") do |faraday|
      faraday.request  :url_encoded
      faraday.response :logger
      faraday.response :json
      faraday.adapter  Faraday.default_adapter
    end
  end
end

class SearchCrossref
  def initialize(query, opts = {})
    @query = query[:everything]
    @page = query[:current_page] || 1
    @rows = APP_CONFIG["results_per_page"]
  end

  def run
    response = conn.get "/works", {
      query: @query,
      rows: @rows,
      offset: offset,
    }

    results = response.body["message"]["items"].map do |result|
      SearchResult.new(result)
    end

    total_results = response.body["message"]["total-results"]

    return results, total_results
  end

  def offset
    @rows * (@page.to_i - 1)
  end

  def conn
    @conn ||= Faraday.new(url: "http://api.crossref.org") do |faraday|
      faraday.request  :url_encoded
      faraday.response :logger
      faraday.response :json
      faraday.adapter  Faraday.default_adapter
    end
  end
end

Add pagination support to CrossRef's API wrapper.
Add pagination support to CrossRef's API wrapper.
