Class: Wikipedia::Client
- Inherits:
-
Object
- Object
- Wikipedia::Client
- Defined in:
- lib/wikipedia/client.rb
Constant Summary collapse
- BASE_URL =
"http://:domain/:path?action=:action&format=json"
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
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #request(options) ⇒ Object
-
#request_image(title, options = {}) ⇒ Object
.
- #request_page(title, options = {}) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
8 9 10 |
# File 'lib/wikipedia/client.rb', line 8 def initialize self.follow_redirects = true end |
Instance Attribute Details
#follow_redirects ⇒ Object
Returns the value of attribute follow_redirects.
6 7 8 |
# File 'lib/wikipedia/client.rb', line 6 def follow_redirects @follow_redirects end |
Instance Method Details
#find(title, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/wikipedia/client.rb', line 12 def find( title, = {} ) title = Url.new(title).title rescue title page = Page.new( request_page( title, ) ) while follow_redirects and page.redirect? page = Page.new( request_page( page.redirect_title, )) end page end |
#find_image(title, options = {}) ⇒ Object
21 22 23 24 |
# File 'lib/wikipedia/client.rb', line 21 def find_image( title, = {} ) title = Url.new(title).title rescue title Page.new( request_image( title, ) ) end |
#request(options) ⇒ Object
46 47 48 49 |
# File 'lib/wikipedia/client.rb', line 46 def request( ) require 'open-uri' URI.parse( url_for( ) ).read( "User-Agent" => "Ruby/#{RUBY_VERSION}" ) end |
#request_image(title, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/wikipedia/client.rb', line 37 def request_image( title, = {} ) request( { :action => "query", :prop => "imageinfo", :iiprop => "url", :titles => title }.merge( ) ) end |
#request_page(title, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/wikipedia/client.rb', line 27 def request_page( title, = {} ) request( { :action => "query", :prop => %w{ revisions links images categories }, :rvprop => "content", :titles => title }.merge( ) ) end |