Permalink
Browse files

initial working version

converted the gist of ask wikipedia to ruby gem with satndalone script
for command line usage.
  • Loading branch information...
emad-elsaid committed Mar 1, 2014
0 parents commit b8c9c492fb8f2add631124de18b6e0c6feb0a78e
Showing with 104 additions and 0 deletions.
  1. +10 −0 .gitignore
  2. +40 −0 README.md
  3. +14 −0 askwiki.gemspec
  4. +10 −0 bin/askwiki
  5. +30 −0 lib/askwiki.rb
@@ -0,0 +1,10 @@
.DS_Store
.rvmrc
doc
Gemfile.lock
github-test.rb
pkg
profile*.html
rdoc
*.sqlite3
*.gem
@@ -0,0 +1,40 @@
# Ask Wikipedia
Ask Wikipedia is a ruby gem for querying wikipedia in any language from within your
ruby or rails application, and also could be used from commandline directly.
## Installation
you can install latest version using
```ruby
gem install askwiki
```
## Usage
add it to your project using
```ruby
require 'askwiki'
# instantiate a new object and ask it
obj = Askwiki.new('en') # padd wikipedia language as param or leave it for english
print obj.ask('API') # to ask wikipedia for an article
# or use the class method
print Askwiki.ask('API')
```
also you can use it from commandline/terminal as follows
```bash
$ askwiki API
```
## Questions and Problems ?
open a new issue to let me know and i will response as soon as possible
## How to Help
we appreciate Star/Fork/Watch on github so show love, and if you wanna help more then fork, then add a feature or enhance it, and send me a pull request, i will review and merge your commit.
@@ -0,0 +1,14 @@
Gem::Specification.new do |s|
s.name = 'askwiki'
s.version = '0.1.0'
s.summary = 'ask wikipedia from cli'
s.description = 'get article text from wikipedia from cli'
s.authors = ['Emad Elsaid']
s.email = ['blazeeboy@gmail.com']
s.files = `git ls-files`.split("\n").sort
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.platform = Gem::Platform::RUBY
s.homepage = 'http://www.github.com/user/repo'
s.license = 'MIT'
s.require_paths = ['lib']
end
@@ -0,0 +1,10 @@
#!/usr/bin/env ruby
require 'askwiki'
query = ARGV[0] ||
begin
print 'What do you need to know? : '
gets.chomp
end
puts Askwiki.ask(query)
@@ -0,0 +1,30 @@
require 'open-uri'
require 'json'
class Askwiki
@language
def initialize( lang='en' )
@language = lang
end
def ask query
query_encoded = URI::encode(query)
request_url = "http://#{@language}.wikipedia.org/w/api.php?action=parse&page=#{query_encoded}&format=json&prop=text&section=0&redirects"
open(request_url) do |file|
puts JSON.parse(file.read())['parse']['text'].first[1]
.gsub(/<\/?[^>]+>/, '') # strip tags
.gsub(/[[:space:]]+/, ' ') # strip whitespace
.gsub(/&#[0-9]+;/,'') # strip encoded
.gsub(/\[[0-9]+\]/,'') # strip referencing
end
end
def self.ask( query, lang='en' )
ask_obj = Askwiki.new lang
ask_obj.ask query
end
end

0 comments on commit b8c9c49

Please sign in to comment.