forked from HamptonMakes/wikimedia-mobile
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Hampton Catlin
committed
Dec 22, 2008
1 parent
73dc6cf
commit 97a69a4
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Server is a model class that represents a media wiki server. | ||
# This is the base model for getting articles and eventually login-logout stuff. | ||
class Server | ||
attr :host | ||
attr :port | ||
|
||
# Whenever you create a new article | ||
# you need to give it a host and a port | ||
def initialize(host, port) | ||
@host, @port = host, port | ||
end | ||
|
||
# What is the base URL for this server? | ||
def base_url | ||
"http://#{@host}:#{@port}" | ||
end | ||
|
||
# Find an article on this server | ||
def find_article(title) | ||
path_to_article "/wiki/Special:Search?search=#{title}" | ||
end | ||
|
||
# Grab a random article from this server | ||
def random_article | ||
path_to_article "/wiki/Special:Random" | ||
end | ||
|
||
private | ||
|
||
# This internal method is used to go out and fetch the file. | ||
# In the future, this method might use a cache... | ||
# | ||
# paths must start with a / | ||
def fetch(path) | ||
(Curl::Easy.perform(base_url + path) do |curl| | ||
# This configures Curl::Easy to follow redirects | ||
curl.follow_location = true | ||
end).body_str | ||
end | ||
|
||
# This method uses the fetch method to get the string representing | ||
# the contents of an article and then puts that in a shiney new | ||
# Article object made just for you. | ||
def path_to_article(path) | ||
article = Article.new(self) | ||
article.parse(fetch(path)) | ||
return article | ||
end | ||
end |