Class: Wikipedia::Client
- Inherits:
-
Object
- Object
- Wikipedia::Client
- Defined in:
- lib/wikipedia/client.rb
Constant Summary collapse
- BASE_URL_TEMPLATE =
'%{protocol}://%{domain}/%{path}?action=%{action}&format=json'.freeze
- BASE_URL_OPTIONS =
Set.new([:protocol, :domain, :path, :action])
Instance Attribute Summary collapse
-
#follow_redirects ⇒ Object
Returns the value of attribute follow_redirects.
Instance Method Summary collapse
- #find(title, options = {}) ⇒ Object
- #find_image(title, options = {}) ⇒ Object
- #find_random(options = {}) ⇒ Object
-
#initialize(configuration = Wikipedia::Configuration.new) ⇒ Client
constructor
A new instance of Client.
- #request(options) ⇒ Object
-
#request_image(title, options = {}) ⇒ Object
.
- #request_page(title, options = {}) ⇒ Object
- #request_random(options = {}) ⇒ Object
Constructor Details
#initialize(configuration = Wikipedia::Configuration.new) ⇒ Client
Returns a new instance of Client.
14 15 16 17 |
# File 'lib/wikipedia/client.rb', line 14 def initialize(configuration = Wikipedia::Configuration.new) @configuration = configuration self.follow_redirects = true end |
Instance Attribute Details
#follow_redirects ⇒ Object
Returns the value of attribute follow_redirects.
12 13 14 |
# File 'lib/wikipedia/client.rb', line 12 def follow_redirects @follow_redirects end |
Instance Method Details
#find(title, options = {}) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/wikipedia/client.rb', line 19 def find( title, = {} ) title = Url.new(title).title rescue title page = Page.new( request_page( title, ) ) while follow_redirects && page.redirect? page = Page.new( request_page( page.redirect_title, ) ) end page end |
#find_image(title, options = {}) ⇒ Object
28 29 30 31 |
# File 'lib/wikipedia/client.rb', line 28 def find_image( title, = {} ) title = Url.new(title).title rescue title Page.new( request_image( title, ) ) end |
#find_random(options = {}) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/wikipedia/client.rb', line 33 def find_random( = {} ) require 'json' data = JSON.parse( request_random( ) ) title = data['query']['pages'].values[0]['title'] find( title, ) end |
#request(options) ⇒ Object
75 76 77 |
# File 'lib/wikipedia/client.rb', line 75 def request( ) URI.parse( url_for( ) ).read( headers ) end |
#request_image(title, options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 |
# File 'lib/wikipedia/client.rb', line 55 def request_image( title, = {} ) request( { action: 'query', prop: 'imageinfo', iiprop: 'url', iiurlwidth: && [:iiurlwidth] ? [:iiurlwidth] : 200, titles: title }.merge( ) ) end |
#request_page(title, options = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/wikipedia/client.rb', line 41 def request_page( title, = {} ) request( { action: 'query', prop: %w[info revisions links extlinks images categories coordinates templates extracts pageimages langlinks], rvprop: 'content', inprop: 'url', pithumbsize: 200, explaintext: '', lllimit: 500, titles: title }.merge( ) ) end |
#request_random(options = {}) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'lib/wikipedia/client.rb', line 66 def request_random( = {} ) request( { action: 'query', generator: 'random', grnnamespace: '0', prop: 'info' }.merge( ) ) end |