Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: cb65b7b60a
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 35 lines (29 sloc) 0.837 kb
1 2 3 4 5 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 33 34
require 'faraday_middleware'

module Wikipedia
  def self.client
    @client ||= Faraday.new('http://en.wikipedia.org/w/api.php?format=json') do |client|
      if user_agent = Movies::Application.config.user_agent
        client.headers[:user_agent] = Movies::Application.config.user_agent
      end

      client.response :json
      client.use :instrumentation
      client.response :raise_error
      client.adapter :net_http
    end
  end

  def self.perform_search(query)
    client.get do |req|
      req.params[:action] = 'query'
      req.params[:list] = 'search'
      req.params[:srsearch] = query
    end
  end

  def self.search(query)
    response = perform_search(query)
    response.body['query']['search']
  end

  def self.find_title(query)
    result = search(query).first
    result['title'] if result
  end
end
Something went wrong with that request. Please try again.