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:

Connect to http://seattlewiki.net

wiki = LocalwikiClient.new 'seattlewiki.net'

Connect to http://seattlewiki.net with user and apikey for write access

wiki = LocalwikiClient.new 'seattlewiki.net', 'myusername', '89f17088f43b5ae22779365b8d1ff0ed076'

Parameters:

  • hostname

    domain host of localwiki server

  • user (defaults to: nil)

    localwiki username

  • apikey (defaults to: nil)

    localwiki user’s apikey provided by the localwiki server administrator



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

def initialize hostname, user=nil, apikey=nil
  @user = user
  @apikey = apikey
  @hostname = hostname
  initialize_connection @hostname
end

Instance Attribute Details

#hostnameObject

hostname of the server we’d like to point at



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

def hostname
  @hostname
end

#siteObject (readonly)

localwiki site resource



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

def site
  @site
end

Instance Method Details

#count(resource_type) ⇒ Fixnum

Request total count of given resource

Examples:

Get the number of users

wiki.count('user')

Parameters:

  • resource_type,,,,,, ("site""page""user""file""map""tag""page_tags")

    esource_type “site”, “page”, “user”, “file”, “map”, “tag”, “page_tags”

Returns:

  • (Fixnum)

    resource count



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

def count(resource_type)
  path = '/api/' + resource_type.to_s
  response = http_get(path,{limit: '1'})
  response["meta"]["total_count"]
end

#create(resource_type, json) ⇒ Faraday::Response

create a specific resource. LocalwikiClient must have been initialized with user and apikey.

Examples:

Create a page from the json string

wiki.create(:page, {name: 'New Page', content: '<p>A New Page!</p>'}.to_json)

Parameters:

  • resource_type,,,,,, ("site""page""user""file""map""tag""page_tags")

    esource_type “site”, “page”, “user”, “file”, “map”, “tag”, “page_tags”

  • json

    json string

Returns:

  • (Faraday::Response)

    http response object



118
119
120
121
# File 'lib/localwiki/client.rb', line 118

def create(resource_type, json)
  path = '/api/' + resource_type.to_s + '/'
  http_post(path, json)
end

#delete(resource_type, identifier) ⇒ Faraday::Response

delete a specific resource. LocalwikiClient must have been initialized with user and apikey.

Examples:

Delete the tag ‘library’

wiki.delete(:tag, 'library')

Parameters:

  • resource_type,,,,,, ("site""page""user""file""map""tag""page_tags")

    esource_type “site”, “page”, “user”, “file”, “map”, “tag”, “page_tags”

  • identifier

    id, pagename, slug, etc.

Returns:

  • (Faraday::Response)

    http response object



143
144
145
146
# File 'lib/localwiki/client.rb', line 143

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

#fetch(resource_type, identifier, params = {}) ⇒ Localwiki::Resource, Faraday::Response Also known as: read

fetch a specific resource

Examples:

Get the page with the name ‘Schedule’

wiki.fetch(:page, 'Schedule')
#=> #<Localwiki::Page>

Parameters:

  • resource_type,,,,,, ("site""page""user""file""map""tag""page_tags")

    esource_type “site”, “page”, “user”, “file”, “map”, “tag”, “page_tags”

  • identifier

    id, pagename, slug, etc.

  • params (defaults to: {})

    hash of query string params

Returns:



79
80
81
82
# File 'lib/localwiki/client.rb', line 79

def fetch(resource_type,identifier,params={})
  path = '/api/' + resource_type.to_s + '/' + slugify(identifier)
  hydrate(resource_type, http_get(path,params))
end

#fetch_version(resource_type, identifier, params = {}) ⇒ Hash, Faraday::Response

fetch version information for a resource

Examples:

Get the version history for page called ‘First Page’

wiki.fetch_version(:page, 'First Page')

Parameters:

  • resource_type,,,,,, ("site""page""user""file""map""tag""page_tags")

    esource_type “site”, “page”, “user”, “file”, “map”, “tag”, “page_tags”

  • identifier

    id, pagename, slug, etc.

  • params (defaults to: {})

    hash of query string params

Returns:

  • (Hash, Faraday::Response)

    hash from json response body, otherwise the http response object



93
94
95
96
# File 'lib/localwiki/client.rb', line 93

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

#list(resource_type, limit = 0, params = {}) ⇒ Array, Faraday::Response

list of a specific type of resource

Examples:

Get the first 10 files

wiki.list(:file, 10)
#=> [ #<Localwiki::File>, ... ]

Parameters:

  • resource_type,,,,,, ("site""page""user""file""map""tag""page_tags")

    esource_type “site”, “page”, “user”, “file”, “map”, “tag”, “page_tags”

  • limit (defaults to: 0)

    maximum number to return

  • params (defaults to: {})

    hash of query string params

Returns:

  • (Array, Faraday::Response)

    array of Localwiki::Resource objects, or the http response object



63
64
65
66
67
68
# File 'lib/localwiki/client.rb', line 63

def list(resource_type,limit=0,params={})
  path = '/api/' + resource_type.to_s
  params.merge!({limit: limit.to_s})
  response = http_get(path,params)
  hydrate_list(resource_type, response['objects'])
end

#page_by_name(name) ⇒ Localwiki::Page

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:

  • (Localwiki::Page)

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



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

def page_by_name(name)
  fetch(:page, name)
end

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

number of unique authors that have modified a resource

Examples:

Get number of unique authors for the page ‘First Page’

wiki.unique_authors(:page, 'First Page')

Parameters:

  • resource_type,,,,,, ("site""page""user""file""map""tag""page_tags")

    esource_type “site”, “page”, “user”, “file”, “map”, “tag”, “page_tags”

  • identifier

    id, pagename, slug, etc.

  • params (defaults to: {})

    hash of query string params

Returns:

  • (Fixnum)

    number of unique authors



106
107
108
109
# File 'lib/localwiki/client.rb', line 106

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

#update(resource_type, identifier, json) ⇒ Faraday::Response

update a specific resource. LocalwikiClient must have been initialized with user and apikey.

Examples:

Update an existing page with json string

wiki.update(:page, {name: 'Existing Page', content: '<p>Updated Page!</p>'}.to_json)

Parameters:

  • resource_type,,,,,, ("site""page""user""file""map""tag""page_tags")

    esource_type “site”, “page”, “user”, “file”, “map”, “tag”, “page_tags”

  • identifier

    id, pagename, slug, etc.

  • json

    json string

Returns:

  • (Faraday::Response)

    http response object



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

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