Class: Wikiwiki::API

Inherits:
Object
  • Object
show all
Defined in:
lib/wikiwiki/api.rb

Overview

Handles HTTP communication with the Wikiwiki REST API

Examples:

auth = Wikiwiki::Auth.password(password: "secret")
api = Wikiwiki::API.new(wiki_id: "my-wiki", auth:)
pages = api.get_pages

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wiki_id:, auth:, logger:, rate_limiter: RateLimiter.default) ⇒ API

Initialize a new API client and authenticate

Parameters:

  • wiki_id (String)

    the wiki identifier

  • auth (Auth::Password, Auth::ApiKey)

    authentication credentials

  • logger (Logger)

    logger instance for request/response logging

  • rate_limiter (RateLimiter) (defaults to: RateLimiter.default)

    rate limiter instance (default: RateLimiter.default)

Raises:

  • (Error)

    if authentication fails



29
30
31
32
33
34
# File 'lib/wikiwiki/api.rb', line 29

def initialize(wiki_id:, auth:, logger:, rate_limiter: RateLimiter.default)
  @wiki_id = wiki_id
  @rate_limiter = rate_limiter
  @logger = logger
  @token = authenticate(auth)
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



16
17
18
# File 'lib/wikiwiki/api.rb', line 16

def logger
  @logger
end

#tokenObject (readonly)

Returns the value of attribute token.



17
18
19
# File 'lib/wikiwiki/api.rb', line 17

def token
  @token
end

Instance Method Details

#delete_attachment(encoded_page_name:, encoded_attachment_name:) ⇒ void

This method returns an undefined value.

Delete an attachment from a page

Parameters:

  • encoded_page_name (String)

    the URL-encoded page name

  • encoded_attachment_name (String)

    the URL-encoded attachment file name

Raises:

  • (Error)

    if request fails



120
121
122
123
124
125
# File 'lib/wikiwiki/api.rb', line 120

def delete_attachment(encoded_page_name:, encoded_attachment_name:)
  uri = BASE_URL + "/#{wiki_id}/page/#{encoded_page_name}/attachment/#{encoded_attachment_name}"
  response = request(:delete, uri)

  parse_json_response(response)
end

#get_attachment(encoded_page_name:, encoded_attachment_name:, rev: nil) ⇒ Hash

Get a specific attachment

Parameters:

  • encoded_page_name (String)

    the URL-encoded page name

  • encoded_attachment_name (String)

    the URL-encoded attachment file name

  • rev (String, nil) (defaults to: nil)

    optional MD5 hash for specific revision

Returns:

  • (Hash)

    file info with Base64-encoded “src” data

Raises:

  • (Error)

    if request fails



92
93
94
95
96
97
98
# File 'lib/wikiwiki/api.rb', line 92

def get_attachment(encoded_page_name:, encoded_attachment_name:, rev: nil)
  uri = BASE_URL + "/#{wiki_id}/page/#{encoded_page_name}/attachment/#{encoded_attachment_name}"
  uri.query = "rev=#{rev}" if rev
  response = request(:get, uri)

  parse_json_response(response)
end

#get_attachments(encoded_page_name:) ⇒ Hash

Get list of attachments on a page

Parameters:

  • encoded_page_name (String)

    the URL-encoded page name

Returns:

  • (Hash)

    response with “attachments” key containing hash of file info

Raises:

  • (Error)

    if request fails



78
79
80
81
82
83
# File 'lib/wikiwiki/api.rb', line 78

def get_attachments(encoded_page_name:)
  uri = BASE_URL + "/#{wiki_id}/page/#{encoded_page_name}/attachments"
  response = request(:get, uri)

  parse_json_response(response)
end

#get_page(encoded_page_name:) ⇒ Hash

Get a specific page

Parameters:

  • encoded_page_name (String)

    the URL-encoded page name

Returns:

  • (Hash)

    response with “page”, “source”, and “timestamp” keys

Raises:

  • (Error)

    if request fails



52
53
54
55
56
57
# File 'lib/wikiwiki/api.rb', line 52

def get_page(encoded_page_name:)
  uri = BASE_URL + "/#{wiki_id}/page/#{encoded_page_name}"
  response = request(:get, uri)

  parse_json_response(response)
end

#get_pagesHash

Get list of all pages

Returns:

  • (Hash)

    response with “pages” key containing array of page info

Raises:

  • (Error)

    if request fails



40
41
42
43
44
45
# File 'lib/wikiwiki/api.rb', line 40

def get_pages
  uri = BASE_URL + "/#{wiki_id}/pages"
  response = request(:get, uri)

  parse_json_response(response)
end

#put_attachment(encoded_page_name:, attachment_name:, encoded_content:) ⇒ void

This method returns an undefined value.

Upload an attachment to a page

Parameters:

  • encoded_page_name (String)

    the URL-encoded page name

  • attachment_name (String)

    the attachment file name (not URL-encoded; sent in JSON body)

  • encoded_content (String)

    Base64-encoded file data

Raises:

  • (Error)

    if request fails



107
108
109
110
111
112
# File 'lib/wikiwiki/api.rb', line 107

def put_attachment(encoded_page_name:, attachment_name:, encoded_content:)
  uri = BASE_URL + "/#{wiki_id}/page/#{encoded_page_name}/attachment"
  response = request(:put, uri, body: {"filename" => attachment_name, "data" => encoded_content})

  parse_json_response(response)
end

#put_page(encoded_page_name:, source:) ⇒ void

Note:

Passing an empty string as source will delete the page

This method returns an undefined value.

Update a page

Parameters:

  • encoded_page_name (String)

    the URL-encoded page name

  • source (String)

    the page source content

Raises:

  • (Error)

    if request fails



66
67
68
69
70
71
# File 'lib/wikiwiki/api.rb', line 66

def put_page(encoded_page_name:, source:)
  uri = BASE_URL + "/#{wiki_id}/page/#{encoded_page_name}"
  response = request(:put, uri, body: {"source" => source})

  parse_json_response(response)
end