Skip to content

Commit

Permalink
Add compliance for Wikipedia's API
Browse files Browse the repository at this point in the history
Add support for custom user agents, and add a more descriptive default user agent.
  • Loading branch information
potatodiet committed Jun 24, 2015
1 parent e1fc61a commit a1680cd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
15 changes: 9 additions & 6 deletions lib/wikipedia.rb
Expand Up @@ -3,13 +3,13 @@
require 'uri'

module Wikipedia
# Examples :
# page = Wikipedia.find('Rails')
# Examples :
# page = Wikipedia.find('Rails')
# => #<Wikipedia:0x123102>
# page.content
# => wiki content appears here
# basically just a wrapper for doing

# basically just a wrapper for doing
# client = Wikipedia::Client.new
# client.find('Rails')
#
Expand All @@ -19,15 +19,18 @@ def self.find( page, options = {} )
def self.find_image( title, options = {} )
client.find_image( title, options )
end

def self.Configure(&block)
Configuration.instance.instance_eval(&block)
end

Configure {
protocol 'http'
domain 'en.wikipedia.org'
path 'w/api.php'
user_agent(
'wikipedia-client/1.3 (https://github.com/manuisfunny/wikipedia-client)'
)
}

private
Expand Down
8 changes: 4 additions & 4 deletions lib/wikipedia/client.rb
Expand Up @@ -46,15 +46,15 @@ def request_image( title, options = {} )

def request( options )
require 'open-uri'
URI.parse( url_for( options ) ).read( "User-Agent" => "Ruby/#{RUBY_VERSION}" )
URI.parse( url_for( options ) ).read( "User-Agent" => Configuration[:user_agent] )
end

protected
def configuration_options
{
:protocol => Configuration[:protocol],
:domain => Configuration[:domain],
:path => Configuration[:path]
:protocol => Configuration[:protocol],
:domain => Configuration[:domain],
:path => Configuration[:path],
}
end

Expand Down
6 changes: 3 additions & 3 deletions lib/wikipedia/configuration.rb
Expand Up @@ -15,11 +15,11 @@ def self.directives(*directives)
end
end
end

def self.[](directive)
instance.send(directive)
end
directives :protocol, :domain, :path

directives :protocol, :domain, :path, :user_agent
end
end

0 comments on commit a1680cd

Please sign in to comment.