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.
-
#site ⇒ Object
readonly
localwiki site resource.
Instance Method Summary collapse
-
#count(resource_type) ⇒ Fixnum
Request total count of given resource.
-
#create(resource_type, json) ⇒ Faraday::Response
create a specific resource.
-
#delete(resource_type, identifier) ⇒ Faraday::Response
delete a specific resource.
-
#fetch(resource_type, identifier, params = {}) ⇒ Localwiki::Resource, Faraday::Response
(also: #read)
fetch a specific resource.
-
#fetch_version(resource_type, identifier, params = {}) ⇒ Hash, Faraday::Response
fetch version information for a resource.
-
#initialize(hostname, user = nil, apikey = nil) ⇒ Client
constructor
Create a LocalWikiClient instance.
-
#list(resource_type, limit = 0, params = {}) ⇒ Array, Faraday::Response
list of a specific type of resource.
-
#page_by_name(name) ⇒ Localwiki::Page
fetch a page by name (“The Page Name” or “The_Page_Name” or “the page name”).
-
#unique_authors(resource_type, identifier, params = {}) ⇒ Fixnum
number of unique authors that have modified a resource.
-
#update(resource_type, identifier, json) ⇒ Faraday::Response
update a specific resource.
Constructor Details
#initialize(hostname, user = nil, apikey = nil) ⇒ Client
Create a LocalWikiClient instance
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
#hostname ⇒ Object
hostname of the server we’d like to point at
13 14 15 |
# File 'lib/localwiki/client.rb', line 13 def hostname @hostname end |
#site ⇒ Object (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
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.
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.
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
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
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
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”)
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
106 107 108 109 |
# File 'lib/localwiki/client.rb', line 106 def (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.
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 |