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

Examples:

LocalwikiClient.new ‘seattlewiki.net’



28
29
30
31
32
33
34
# File 'lib/localwiki/client.rb', line 28

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



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

def hostname
  @hostname
end

#language_codeObject (readonly)

site resource - language code of the server, example: ‘en-us’



21
22
23
# File 'lib/localwiki/client.rb', line 21

def language_code
  @language_code
end

#site_nameObject (readonly)

site resource - display name of wiki



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

def site_name
  @site_name
end

#time_zoneObject (readonly)

site resource - time zone of server, example: ‘America/Chicago’



18
19
20
# File 'lib/localwiki/client.rb', line 18

def time_zone
  @time_zone
end

Instance Method Details

#count(resource) ⇒ Fixnum

Request total count of given resource

Examples:

wiki.count(‘user’)

Parameters:

  • resource

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

Returns:

  • (Fixnum)

    resource count



41
42
43
# File 'lib/localwiki/client.rb', line 41

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

#create(resource, json) ⇒ HTTPResponse

create a specific resource

Examples:

wiki.create(‘page’, <json object containing the page tag>)

Parameters:

  • resource

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

  • json

    is a json object

Returns:

  • (HTTPResponse)

    the http response object



108
109
110
111
# File 'lib/localwiki/client.rb', line 108

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

#delete(resource, identifier) ⇒ HTTPResponse

delete a specific resource

Examples:

wiki.delete(‘page’, ‘<page tag>’)

Parameters:

  • resource

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

  • identifier

    is id, pagename, slug, etc.

Returns:

  • (HTTPResponse)

    the http response object



131
132
133
134
# File 'lib/localwiki/client.rb', line 131

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

#fetch(resource, identifier, params = {}) ⇒ Hash Also known as: read

fetch a specific resource

Examples:

wiki.update(‘page’, ‘<page tag>’)

Parameters:

  • resource

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

  • identifier

    is id, pagename, slug, etc.

  • params (defaults to: {})

    is a hash of query string params

Returns:

  • (Hash)

    the parsed JSON object from the response body, otherwise the whole http response object



72
73
74
75
# File 'lib/localwiki/client.rb', line 72

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

#fetch_version(resource, identifier, params = {}) ⇒ Hash

fetch version information for a resource

Examples:

wiki.fetch_version(‘page’, ‘First Page’)

Parameters:

  • resource

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

  • identifier

    is id, pagename, slug, etc.

  • params (defaults to: {})

    is a hash of query string params

Returns:

  • (Hash)

    the parsed JSON object from the response body, otherwise the whole http response object



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

def fetch_version(resource,identifier,params={})
  uri = '/api/' + resource.to_s + '_version?name=' + identifier
  http_get(uri,params)
end

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

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

  • params (defaults to: {})

    is a hash of query string params

Returns:

  • (Hash)

    the parsed JSON object from the response body, otherwise the whole http response object



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

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) ⇒ Hash

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”

Returns:

  • (Hash)

    the parsed JSON object from the response body, otherwise the whole http response object



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

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

#unique_authors(resource, identifier, params = {}) ⇒ Fixnum

number of unique authors that have modified a resource

Examples:

wiki.unique_authors(‘page’, ‘First Page’)

Parameters:

  • resource

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

  • identifier

    is id, pagename, slug, etc.

  • params (defaults to: {})

    is a hash of query string params

Returns:

  • (Fixnum)


97
98
99
100
# File 'lib/localwiki/client.rb', line 97

def unique_authors(resource,identifier,params={})
  json = fetch_version(resource,identifier,params)
  json['objects'].map {|entry| entry['history_user']}.uniq.length
end

#update(resource, identifier, json) ⇒ HTTPResponse

update a specific resource

Examples:

wiki.update(‘page’, ‘<page tag>’, <json object>)

Parameters:

  • resource

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

  • identifier

    is id, pagename, slug, etc.

  • json

    is a json object

Returns:

  • (HTTPResponse)

    the http response object



120
121
122
123
# File 'lib/localwiki/client.rb', line 120

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