Class: Localwiki::Client
- Inherits:
-
Object
- Object
- Localwiki::Client
- Defined in:
- lib/localwiki/client.rb
Overview
A client that wraps the localwiki api for a given server instance
Direct Known Subclasses
Instance Attribute Summary collapse
-
#hostname ⇒ Object
hostname of the server we’d like to point at.
-
#language_code ⇒ Object
readonly
site resource - language code of the server, example: ‘en-us’.
-
#site_name ⇒ Object
readonly
site resource - display name of wiki.
-
#time_zone ⇒ Object
readonly
site resource - time zone of server, example: ‘America/Chicago’.
Instance Method Summary collapse
-
#count(resource) ⇒ Fixnum
Request total count of given resource.
-
#create(resource, json) ⇒ HTTPResponse
create a specific resource.
-
#delete(resource, identifier) ⇒ HTTPResponse
delete a specific resource.
-
#fetch(resource, identifier, params = {}) ⇒ Hash
(also: #read)
fetch a specific resource.
-
#fetch_version(resource, identifier, params = {}) ⇒ Hash
fetch version information for a resource.
-
#initialize(hostname, user = nil, apikey = nil) ⇒ Client
constructor
Create a LocalWikiClient instance.
-
#list(resource, limit = 0, params = {}) ⇒ Hash
list of a specific type of resource.
-
#page_by_name(name) ⇒ Hash
fetch a page by name (“The Page Name” or “The_Page_Name” or “the page name”).
-
#unique_authors(resource, identifier, params = {}) ⇒ Fixnum
number of unique authors that have modified a resource.
-
#update(resource, identifier, json) ⇒ HTTPResponse
update a specific resource.
Constructor Details
#initialize(hostname, user = nil, apikey = nil) ⇒ Client
Create a LocalWikiClient instance
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
#hostname ⇒ Object
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_code ⇒ Object (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_name ⇒ Object (readonly)
site resource - display name of wiki
15 16 17 |
# File 'lib/localwiki/client.rb', line 15 def site_name @site_name end |
#time_zone ⇒ Object (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
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
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
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
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
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
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”)
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
97 98 99 100 |
# File 'lib/localwiki/client.rb', line 97 def (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
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 |