Class: Localwiki::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/localwiki/client.rb

Overview

A client that wraps the localwiki api for a given server instance

Direct Known Subclasses

LocalwikiClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hostname, user = nil, apikey = nil) ⇒ Client

Create a LocalWikiClient instance

@example LocalwikiClient.new 'seattlewiki.net'


21
22
23
24
25
26
27
# File 'lib/localwiki/client.rb', line 21

def initialize hostname, user=nil, apikey=nil
  @hostname = hostname
  @user = user
  @apikey = apikey
  create_connection
  collect_site_details
end

Instance Attribute Details

#hostnameObject

hostname of the server we’d like to point at



11
12
13
# File 'lib/localwiki/client.rb', line 11

def hostname
  @hostname
end

#language_codeObject (readonly)

site resource - language code of the server, e.g. ‘en-us’



14
15
16
# File 'lib/localwiki/client.rb', line 14

def language_code
  @language_code
end

#site_nameObject (readonly)

site resource - display name of wiki



12
13
14
# File 'lib/localwiki/client.rb', line 12

def site_name
  @site_name
end

#time_zoneObject (readonly)

site resource - time zone of server, e.g. ‘America/Chicago’



13
14
15
# File 'lib/localwiki/client.rb', line 13

def time_zone
  @time_zone
end

Instance Method Details

#count(resource) ⇒ Object

Request total count of given resource



32
33
34
# File 'lib/localwiki/client.rb', line 32

def count(resource)
  list(resource.to_s,1)["meta"]["total_count"]
end

#create(resource, json) ⇒ Object

create a specific resource resources are “site”, “page”, “user”, “file”, “map”, “tag”, “page_tag”



67
68
69
70
# File 'lib/localwiki/client.rb', line 67

def create(resource, json)
  uri = '/api/' + resource.to_s + '/'
  http_post(uri, json)
end

#delete(resource, identifier) ⇒ Object

delete a specific resource resources are “site”, “page”, “user”, “file”, “map”, “tag”, “page_tag” identifier is id, pagename, slug, etc.



85
86
87
88
# File 'lib/localwiki/client.rb', line 85

def delete(resource,identifier)
  uri = '/api/' + resource.to_s + '/' + identifier
  http_delete(uri)
end

#fetch(resource, identifier, params = {}) ⇒ Object

fetch a specific resource resources are “site”, “page”, “user”, “file”, “map”, “tag”, “page_tag” identifier is id, pagename, slug, etc. params is a hash of query string params



59
60
61
62
# File 'lib/localwiki/client.rb', line 59

def fetch(resource,identifier,params={})
  uri = '/api/' + resource.to_s + '/' + identifier
  http_get(uri,params)
end

#list(resource, limit = 0, params = {}) ⇒ Object

list of a specific type of resource

Parameters:

  • resource

    are “site”, “page”, “user”, “file”, “map”, “tag”, “page_tag”

  • limit (defaults to: 0)

    is an integer

  • is

    a hash of query string params



48
49
50
51
52
# File 'lib/localwiki/client.rb', line 48

def list(resource,limit=0,params={})
  uri = '/api/' + resource.to_s
  params.merge!({limit: limit.to_s})
  http_get(uri,params)
end

#page_by_name(name) ⇒ Object

fetch a page by name (“The Page Name” or “The_Page_Name” or “the page name”)

Parameters:

  • nameoror ("The Page Name""The_Page_Name""the page name")

    ame “The Page Name” or “The_Page_Name” or “the page name”



39
40
41
# File 'lib/localwiki/client.rb', line 39

def page_by_name(name)
  fetch(:page,"#{name.gsub!(/\s/, '_')}")
end

#update(resource, identifier, json) ⇒ Object

update a specific resource resources are “site”, “page”, “user”, “file”, “map”, “tag”, “page_tag” identifier is id, pagename, slug, etc.



76
77
78
79
# File 'lib/localwiki/client.rb', line 76

def update(resource,identifier,json)
  uri = '/api/' + resource.to_s + '/' + identifier
  http_put(uri, json)  
end