Module: WikipediaRestClient

Defined in:
lib/wikipedia_rest_client.rb,
lib/wikipedia_rest_client/page.rb,
lib/wikipedia_rest_client/utils.rb,
lib/wikipedia_rest_client/version.rb,
lib/wikipedia_rest_client/featured_article.rb,
lib/wikipedia_rest_client/image_of_the_day.rb

Overview

Author:

  • Balaji

Defined Under Namespace

Classes: FeaturedArticle, ImageOfTheDay, Page

Constant Summary collapse

PROTOCOL =

Protocol of the URL

"https://"
BASE_URL =

Base URL of the Wikipedia REST API.

".wikipedia.org/api/rest_v1"
PAGE_URL =

URL parameters that are used to fetch the page content.

"/page/summary/"
RANDOM_PAGE_URL =

URL parameters that are used to fetch random page from Wikipedia.

"/page/random/summary"
FEATURED_ARTICLE =

URL parameters that are used to get featured articles.

"/feed/featured/"
VERSION =

Version number of the gem

"0.1.4"
@@options =

header options to be passed through HTTP requests.

{}
@@language_code =

language code

"en"

Class Method Summary collapse

Class Method Details

.get_featured_article(date = Time.now.strftime("%Y/%m/%d")) ⇒ WikipediaRestClient::Page

This method will return the featured article of the specified date from Wikipedia(Default parameter is current date).

Example:

old_article = WikipediaRestClient.get_featured_article("2018/05/10")
=> Returns featured article dated (2018/05/10)
article.title
=> "Russian_battleship_Pobeda"
article.image_url
=> "<Returns the URL of the image>"
article.text
=> "Pobeda was the last of the three Peresvet-class pre-dreadnought battleships built for the Imperial Russian Navy at the end of the nineteenth century. The ship was assigned to the Pacific Squadron upon completion and based at Port Arthur from 1903. During the Russo-Japanese War of 1904–1905, she participated in the battles of Port Arthur and the Yellow Sea. Having escaped serious damage in these engagements, Pobeda was sunk by gunfire during the Siege of Port Arthur, and then salvaged by the Japanese and placed into service under the name Suwo (周防)."
today_article = WikipediaRestClient.get_featured_article
=> Returns featured article of the current day
today_article.title
=> "Arlington,_Washington"
today_article.url
=> "https://en.wikipedia.org/wiki/Arlington,_Washington"
today_article.pageid
=> 138192

Parameters:

  • date (String) (defaults to: Time.now.strftime("%Y/%m/%d"))

    The string must be a date in YYYY/MM/DD format. The default parameter is the current date.

Returns:



170
171
172
173
174
175
176
# File 'lib/wikipedia_rest_client.rb', line 170

def self.get_featured_article( date = Time.now.strftime("%Y/%m/%d") )
	url = PROTOCOL + @@language_code + BASE_URL + FEATURED_ARTICLE + date
	response = HTTParty.get(url, @@options)
	article = FeaturedArticle.new.featured_article(response)
	page = Page.new(article)
	page
end

.get_image_of_the_day(date = Time.now.strftime("%Y/%m/%d")) ⇒ WikipediaRestClient::IageOfTheDay

This method will return ‘Image of the day’ content of the specified date from Wikipedia(Default parameter is current date).

Example:

picture_of_the_day = WikipediaRestClient.get_image_of_the_day
=> Returns picture of the day from Wikipedia.
picture_of_the_day.title
=> "File:Haus der Kulturen der Welt, Blaue Stunde, Berlin, 160521, ako.jpg"
picture_of_the_day.image_url
=> "<Returns the URL of the image>"
picture_of_the_day.image_height
=> 3648
picture_of_the_day.thumbnail
=> "<Returns the thumbnail URL of the image>"
picture_of_the_day_for_a_date = WikipediaRestClient.get_image_of_the_day("2017/03/12")
=> Returns pictured of the day for the specified date ("YYYY/MM/DD")
picture_of_the_day_for_a_date.image_url 
=> "<Returns the URL of the image>"
picture_of_the_day_for_a_date.description_text
=>  "<i><a rel=\"mw:WikiLink/Interwiki\" href=\"https://en.wikipedia.org/wiki/Cyclosia%20papilionaris\" title=\"en:Cyclosia papilionaris\">Cyclosia papilionaris</a></i>, Drury's Jewel, is a moth in the <a rel=\"mw:WikiLink/Interwiki\" href=\"https://en.wikipedia.org/wiki/Zygaenidae\" title=\"en:Zygaenidae\">Zygaenidae</a> family. There are many subspecies and this is <i>Cyclosia papilionaris australinda</i> found in <a rel=\"mw:WikiLink/Interwiki\" href=\"https://en.wikipedia.org/wiki/South%20India\" title=\"en:South India\">South India</a>."

It will have the following information,

- title
- image_url
- image_width
- image_height
- thumbnail
- thumbnail_width
- thumbnail_height
- description_text

Parameters:

  • date (String) (defaults to: Time.now.strftime("%Y/%m/%d"))

    The string must be a date in YYYY/MM/DD format. default parameter is current date.

Returns:

  • (WikipediaRestClient::IageOfTheDay)

    An object of WikipediaRestClient::ImageOfTheDay.



213
214
215
216
217
218
# File 'lib/wikipedia_rest_client.rb', line 213

def self.get_image_of_the_day(date = Time.now.strftime("%Y/%m/%d") )
	url = PROTOCOL + @@language_code + BASE_URL + FEATURED_ARTICLE + date
	response = HTTParty.get(url, @@options)
	image = ImageOfTheDay.new(response)
	image
end

.get_most_read(date = Time.now.strftime("%Y/%m/%d")) ⇒ JSON

This method will return the contents of mostread pages of the specified date(Default is current date).

Example:

most_read = WikipediaRestClient.get_most_read
=> Returns Hash value
most_read["articles"][0]["views"]
=> 709029
most_read["articles"][0]["title"]
=> "Elizabeth_II"
most_read["articles"][0]["extract"]
=> "Elizabeth II is Queen of the United Kingdom and fifteen other Commonwealth realms."
most_read["articles"][0]["content_urls"]["desktop"]["page"]
=> "https://en.wikipedia.org/wiki/Elizabeth_II"

Parameters:

  • date (String) (defaults to: Time.now.strftime("%Y/%m/%d"))

    The string must be a date in YYYY/MM/DD format. The default parameter is the current date.

Returns:

  • (JSON)

    an JSON object that contains the most-read contents of the specified date



282
283
284
285
286
# File 'lib/wikipedia_rest_client.rb', line 282

def self.get_most_read(date = Time.now.strftime("%Y/%m/%d"))
	url = PROTOCOL + @@language_code + BASE_URL + FEATURED_ARTICLE + date
	response = HTTParty.get(url, @@options)
	response["mostread"]
end

.get_news(date = Time.now.strftime("%Y/%m/%d")) ⇒ JSON

This method will return the list of news that is in the form of JSON object. Each news content will contain news and their related links.

Example:

news = WikipediaRestClient.get_news
=> Returns Array of Hash values which contains news contents
news[0]["story"]
=> "Incumbent Nicolás Maduro is re-elected President of Venezuela."
news[0]["links"]
=> Returns Array of link pages related to that news

Parameters:

  • date (String) (defaults to: Time.now.strftime("%Y/%m/%d"))

    The string must be a date in YYYY/MM/DD format. The default parameter is the current date.

Returns:

  • (JSON)

    JSON object that contains news contents from Wikipedia page.



258
259
260
261
262
263
# File 'lib/wikipedia_rest_client.rb', line 258

def self.get_news(date = Time.now.strftime("%Y/%m/%d"))
	url = PROTOCOL + @@language_code + BASE_URL + FEATURED_ARTICLE + date
	response = HTTParty.get(url, @@options)
	news = WikipediaRestClient.parsed_news(response["news"])
	news
end

.get_on_this_day(date = Time.now.strftime("%Y/%m/%d")) ⇒ JSON

This method will be used to get the ‘On this day’ content from Wikipedia. The default parameter is the current date. If you need you can pass a date in YYYY/MM/DD format and get the “on this day ” content for that specified date.

Example:

on_this_day_content = WikipediaRestClient.get_on_this_day
=> Returns Array of Hash values which contains 'on this day' contents
on_this_day_content[0]["text"]
=> "A corroded oil pipeline in Santa Barbara County, California, burst, spilling 142,800 U.S. gallons (3,400 barrels) of crude oil onto one of the most biologically diverse coastlines of the U.S. West Coast."
on_this_day_content[0]["year"]
=> 2013
on_this_day_content[0]["pages"]
=> Returns Array of Wikipedia pages related to that topic
on_that_day = WikipediaRestClient.get_on_this_day("2018/03/11")
=> Returns 'on this day' contents for the specified date
on_that_day[0]["text"]
=>  "United States Army officer Robert Bales murdered sixteen civilians and wounded six others in the Panjwayi District of Kandahar Province, Afghanistan."

Parameters:

  • date (String) (defaults to: Time.now.strftime("%Y/%m/%d"))

    The string must be a date in YYYY/MM/DD format.The default parameter is the current date.

Returns:

  • (JSON)

    “on this day” content from Wikipedia.



239
240
241
242
243
# File 'lib/wikipedia_rest_client.rb', line 239

def self.get_on_this_day(date = Time.now.strftime("%Y/%m/%d"))
	url = PROTOCOL + @@language_code + BASE_URL + FEATURED_ARTICLE + date
	response = HTTParty.get(url, @@options)
	response["onthisday"]
end

.get_page(title) ⇒ WikipediaRestClient::Page

This method gets search query as a parameter and returns an object that contains the data about the specified Wikipedia page.

Example:

page = WikipediaRestClient.get_page("Indian President")
=> "<Returns an object that contains contents of the required page>"
page.title
=> "President of India"
page.text
=> "The President of the Republic of India is the head of state of India and the commander-in-chief of the Indian Armed Forces."
page.image_url
=> "<Returns the image url of the page>"
The Page class contains the following information,
page.url 
=> "https://en.wikipedia.org/wiki/President_of_India"
page.pageid
=> 141896
page.description
=> "executive head of state of the Republic of India"

Page class contains the following methods,

- type
- title
- display_title
- namespace_id
- namespace_text
- titles_canonical
- titles_normalized
- titles_display
- pageid
- thumbnail_source
- thumbnail_width   
- thumbnail_height
- lang
- dir
- revision
- tid
- timestamp
- description
- image_url
- image_width
- image_height
- url_desktop_page
- url_desktop_revisions
- url_desktop_edit
- url_desktop_talk
- url_mobile_page
- url_mobile_revisions
- url_mobile_edit
- url_mobile_talk
- api_urls_summary
- 
- api_urls_references
- api_urls_media
- api_urls_edit_html
- api_urls_talk_page_html
- text
- text_html
- normalized_title
- url

Parameters:

  • title (String)

    A Wikipedia page title that has to be fetched.

Returns:



112
113
114
115
116
117
118
# File 'lib/wikipedia_rest_client.rb', line 112

def self.get_page(title)
	title = URI::encode(title)
	page_url = PROTOCOL + @@language_code + BASE_URL + PAGE_URL + title
	page_response = HTTParty.get(page_url, @@options)
	page = Page.new(page_response)
	page
end

.get_randomWikipediaRestClient::Page

This method will return a random page from Wikipedia.

Example:

page = WikipediaRestClient.get_random
=> Return a random page from wikipedia 
page.title
=> "Adams Glacier (Victoria Land)"
page.text
=> "Adams Glacier is a small glacier immediately south of Miers Glacier in Victoria Land. The heads of Adams and Miers glaciers, both located in the Miers Valley, are separated by a low ridge, and the east end of this ridge is almost completely surrounded by the snouts of the two glaciers, which nearly meet in the bottom of the valley, about 1 mile (1.6 km) above Lake Miers, into which they drain. It was named by the New Zealand Northern Survey Party of the Commonwealth Trans-Antarctic Expedition (1956–58) after Lieutenant Jameson Adams, second in command of the shore party of the British Antarctic Expedition (1907–09), who was one of the men to accompany Ernest Shackleton to within 97 miles (156 km) of the South Pole."
page.url
=> "https://en.wikipedia.org/wiki/Adams_Glacier_(Victoria_Land)"
page.pageid
=> 16524953
page.image_url
=> "<Returns the URL of the image>"

Parameters:

  • null

Returns:



139
140
141
142
143
144
145
# File 'lib/wikipedia_rest_client.rb', line 139

def self.get_random
	random_url = PROTOCOL + @@language_code + BASE_URL + RANDOM_PAGE_URL
	random_page_response = JSON.parse(HTTParty.get(random_url, @@options))
	random_title = random_page_response["uri"].gsub("/en.wikipedia.org/v1/page/random/../summary/","")
	random_page = get_page(random_title)
	random_page
end

.parsed_news(news) ⇒ JSON

Returns the parsed HTML contents inside the JSON object of get_news method.

Parameters:

  • news (JSON)

    The JSON object from the get_news method will be used here.

Returns:

  • (JSON)

    Returns the parsed HTML contents inside the JSON object of get_news method.



290
291
292
293
294
295
296
297
# File 'lib/wikipedia_rest_client.rb', line 290

def self.parsed_news(news)
	news.each do |news|
		data = news["story"]
		parsed_data = Nokogiri::HTML(data)
		news["story"] = parsed_data.text
	end
	news
end

.set_language(language_code) ⇒ nil

By using this method you can set the language in the REST API (Default lanuage is English).

Example:

WikipediaRestClient.set_language("de")
=> Sets the language as German
WikipediaRestClient.set_language("fr")
=> Sets the language as French

Parameters:

  • language_code (String)

    The Language code

Returns:

  • (nil)


44
45
46
# File 'lib/wikipedia_rest_client.rb', line 44

def self.set_language(language_code)
	@@language_code = language_code
end

.setUserAgent(username) ⇒ nil

This method will set up the User-Agent header to the HTTP request.

Example:

WikipediaRestClient.setUserAgent("[email protected]")

Parameters:

  • username (String)

    Email address or URLs of contact pages

Returns:

  • (nil)


27
28
29
30
31
# File 'lib/wikipedia_rest_client.rb', line 27

def self.setUserAgent(username)
	@@options = {
		headers: {"User-Agent" => username}
	}
end