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



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

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



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

def hostname
  @hostname
end

#siteObject (readonly)

localwiki site resource



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

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



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

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



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

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



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

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:



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

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



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

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



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

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



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

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



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

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



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

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