Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 73 lines (58 sloc) 1.745 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
require 'net/http'
require 'uri'

class MediaWikiApi

  USER_AGENT = 'DbpediaLite/1'
  HTTP_TIMEOUT = 5
  NBSP = Nokogiri::HTML(" ").text
  UNSAFE_REGEXP = Regexp.new('[^-_\.!~*\'()a-zA-Z0-9;/:@&=$,]', false, 'N').freeze

  class Exception < Exception
  end

  class NotFound < MediaWikiApi::Exception
  end

  def self.escape_query(str)
    URI::escape(str, UNSAFE_REGEXP)
  end

  def self.escape_title(title)
    URI::escape(title.gsub(' ','_'), ' ?#%"+=').force_encoding('UTF-8')
  end

  def self.get(action, args={})
    items = []
    args.merge!(:action => action, :format => 'json')

    keys = args.keys.sort {|a,b| a.to_s <=> b.to_s}
    keys.each do |key|
     items << escape_query(key.to_s)+'='+escape_query(args[key].to_s)
    end

    uri = self.api_uri
    uri.query = items.join('&')
    res = Net::HTTP.start(uri.host, uri.port) do |http|
      http.read_timeout = HTTP_TIMEOUT
      http.open_timeout = HTTP_TIMEOUT
      http.get(uri.request_uri, {'User-Agent' => USER_AGENT})
    end

    # Throw exception if unsuccessful
    res.value

    # Parse the response if it is JSON
    if res.content_type == 'application/json'
      data = JSON.parse(res.body)
    else
      raise MediaWikiApi::Exception.new(
        "Response from MediaWiki API was not of type application/json."
      )
    end

    # Check for errors in the response
    if data.nil?
      raise MediaWikiApi::Exception.new('Empty response')
    elsif data.has_key?('error')
      if data['error']['code'] == 'nosuchpageid'
        raise MediaWikiApi::NotFound.new(
          data['error']['info']
        )
      else
        raise MediaWikiApi::Exception.new(
          data['error']['info']
        )
      end
    end

    return data
  end

end
Something went wrong with that request. Please try again.