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
Defined Under Namespace
Classes: FeaturedArticle, ImageOfTheDay, Page
Constant Summary collapse
- BASE_URL =
Base URL of the Wikipedia REST API.
"https://en.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.0"- @@options =
header options to be passed through HTTP requests.
{}
Class Method Summary collapse
-
.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).
-
.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).
-
.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).
-
.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.
-
.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.
-
.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.
-
.get_random ⇒ WikipediaRestClient::Page
This method will return a random page from Wikipedia.
-
.parsed_news(news) ⇒ JSON
Returns the parsed HTML contents inside the JSON object of get_news method.
-
.setUserAgent(username) ⇒ nil
This method will set up the User-Agent header to the HTTP request.
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
153 154 155 156 157 158 159 |
# File 'lib/wikipedia_rest_client.rb', line 153 def self.get_featured_article( date = Time.now.strftime("%Y/%m/%d") ) url = 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
196 197 198 199 200 201 |
# File 'lib/wikipedia_rest_client.rb', line 196 def self.get_image_of_the_day(date = Time.now.strftime("%Y/%m/%d") ) url = 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"
265 266 267 268 269 |
# File 'lib/wikipedia_rest_client.rb', line 265 def self.get_most_read(date = Time.now.strftime("%Y/%m/%d")) url = 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
241 242 243 244 245 246 |
# File 'lib/wikipedia_rest_client.rb', line 241 def self.get_news(date = Time.now.strftime("%Y/%m/%d")) url = 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."
222 223 224 225 226 |
# File 'lib/wikipedia_rest_client.rb', line 222 def self.get_on_this_day(date = Time.now.strftime("%Y/%m/%d")) url = 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
-
- 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
95 96 97 98 99 100 101 |
# File 'lib/wikipedia_rest_client.rb', line 95 def self.get_page(title) title = URI::encode(title) page_url = BASE_URL + PAGE_URL + title page_response = HTTParty.get(page_url, @@options) page = Page.new(page_response) page end |
.get_random ⇒ WikipediaRestClient::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>"
121 122 123 124 125 126 127 128 |
# File 'lib/wikipedia_rest_client.rb', line 121 def self.get_random random_url = 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.
273 274 275 276 277 278 279 280 |
# File 'lib/wikipedia_rest_client.rb', line 273 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 |
.setUserAgent(username) ⇒ nil
This method will set up the User-Agent header to the HTTP request.
Example:
WikipediaRestClient.setUserAgent("[email protected]")
25 26 27 28 29 |
# File 'lib/wikipedia_rest_client.rb', line 25 def self.setUserAgent(username) @@options = { headers: {"User-Agent" => username} } end |