localwiki_client 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/History.txt +5 -0
- data/README.md +17 -9
- data/Rakefile +6 -0
- data/integration/helper.rb +1 -1
- data/integration/live_saltlake_wiki_spec.rb +2 -2
- data/integration/live_test_wiki_spec.rb +26 -11
- data/lib/localwiki/client.rb +143 -115
- data/lib/localwiki/resource.rb +76 -0
- data/lib/localwiki/version.rb +1 -1
- data/lib/localwiki_client.rb +1 -0
- data/localwiki_client.gemspec +1 -0
- data/spec/basic_crud_spec.rb +12 -5
- data/spec/basic_spec.rb +2 -2
- data/spec/fixtures/cassettes/basic.yml +21 -19
- data/spec/fixtures/cassettes/basic_crud.yml +21 -19
- data/spec/fixtures/cassettes/basic_crud_delete_fail.yml +11 -9
- data/spec/fixtures/cassettes/basic_crud_delete_success.yml +10 -8
- data/spec/fixtures/cassettes/basic_crud_page_by_name_success.yml +40 -0
- data/spec/fixtures/cassettes/basic_crud_read_fail.yml +22 -34
- data/spec/fixtures/cassettes/basic_crud_read_success.yml +11 -11
- data/spec/fixtures/cassettes/basic_crud_update_success.yml +11 -9
- data/spec/fixtures/cassettes/basic_fetch_version_success.yml +41 -37
- data/spec/fixtures/cassettes/basic_page_by_name_spaces.yml +12 -12
- data/spec/fixtures/cassettes/basic_unique_authors_success.yml +41 -37
- data/spec/fixtures/cassettes/fetch.yml +8 -8
- data/spec/fixtures/cassettes/list.yml +93 -0
- data/spec/fixtures/cassettes/list_file_success.yml +40 -0
- data/spec/fixtures/cassettes/list_limit_success.yml +124 -0
- data/spec/fixtures/cassettes/list_map_success.yml +42 -0
- data/spec/fixtures/cassettes/list_page_success.yml +1108 -0
- data/spec/fixtures/cassettes/list_page_tags_success.yml +42 -0
- data/spec/fixtures/cassettes/list_site_success.yml +46 -0
- data/spec/fixtures/cassettes/list_tag_success.yml +43 -0
- data/spec/fixtures/cassettes/list_user_success.yml +56 -0
- data/spec/fixtures/cassettes/localwiki_client.yml +121 -115
- data/spec/fixtures/cassettes/slug.yml +8 -8
- data/spec/helper.rb +7 -0
- data/spec/list_spec.rb +47 -0
- data/spec/localwiki_client_spec.rb +4 -12
- data/spec/localwiki_spec.rb +17 -0
- data/spec/map_spec.rb +135 -0
- data/spec/resource_spec.rb +60 -0
- metadata +33 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bb9cc507732637d939aa2fdea6e73523e17b0765
|
4
|
+
data.tar.gz: c2eaa613db6c5df7cb2dad1940d693fa0a16907b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a62093cafaa6429868d66ff1c4a15187f37c9e93b8fec8666bc9d36602884feba589b18a201aca253aee61e57b193e3d72c61b023ac4acd3dfdd7113cb94fc60
|
7
|
+
data.tar.gz: 6368549d3fa07caf1678080786626cbcd14442c1864ed176774ce6ef80665a7b6190f0f15b467b8da57dabbd13db055da09b9d62cea6c82f3d0e083c840dbe9a
|
data/.gitignore
CHANGED
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -5,7 +5,7 @@ LocalwikiClient
|
|
5
5
|
Synopsis
|
6
6
|
--------
|
7
7
|
|
8
|
-
A
|
8
|
+
A helper client for the Localwiki API
|
9
9
|
|
10
10
|
http://localwiki.readthedocs.org/en/latest/api.html
|
11
11
|
|
@@ -23,26 +23,34 @@ Usage
|
|
23
23
|
|
24
24
|
# read access
|
25
25
|
wiki = LocalwikiClient.new 'seattlewiki.net'
|
26
|
-
wiki.
|
26
|
+
wiki.site.name
|
27
27
|
=> SeattleWiki
|
28
|
-
wiki.count(
|
28
|
+
wiki.count(:user)
|
29
29
|
=> 47
|
30
|
+
map = wiki.fetch(:map, 'Luna Park Cafe')
|
31
|
+
=> #<Localwiki::Map>
|
32
|
+
map.page
|
33
|
+
=> '/api/page/Luna_Park_Cafe'
|
34
|
+
map.single_point?
|
35
|
+
=> true
|
36
|
+
[map.lat, map.long]
|
37
|
+
=> [47.570641, -122.370919]
|
30
38
|
|
31
39
|
# write access
|
32
40
|
wiki = LocalwikiClient.new 'seattlewiki.net', 'myusername', 'myapikey'
|
33
|
-
|
34
|
-
response = wiki.create(
|
41
|
+
json = {name: 'Current Events', content: 'Coming Soon!'}.to_json
|
42
|
+
response = wiki.create(:page, json)
|
35
43
|
response.status
|
36
44
|
=> 201
|
37
45
|
|
38
46
|
Features / Problems
|
39
47
|
-------------------
|
40
48
|
|
41
|
-
*
|
42
|
-
*
|
43
|
-
*
|
49
|
+
* \#create, #read (#fetch), #update, #delete provide basic CRUD functionality
|
50
|
+
* \#list fetches all (or with a limit) of a specific type of resource
|
51
|
+
* \#fetch_version retrieves version history for a resource
|
44
52
|
* a few helper methods exist, more to come
|
45
|
-
|
53
|
+
* \#count, #page_by_name, #unique_authors
|
46
54
|
|
47
55
|
|
48
56
|
Compatibility
|
data/Rakefile
CHANGED
@@ -24,6 +24,12 @@ RSpec::Core::RakeTask.new(:integration) do |t|
|
|
24
24
|
t.rspec_opts = ['-f d']
|
25
25
|
end
|
26
26
|
|
27
|
+
desc "Run code coverage"
|
28
|
+
task :coverage do
|
29
|
+
ENV['COVERAGE'] = '1'
|
30
|
+
Rake::Task['spec'].invoke
|
31
|
+
end
|
32
|
+
|
27
33
|
desc "Flog the code! (*nix only)"
|
28
34
|
task :flog do
|
29
35
|
system('find lib -name \*.rb | xargs flog')
|
data/integration/helper.rb
CHANGED
@@ -3,7 +3,7 @@ require 'localwiki_client'
|
|
3
3
|
|
4
4
|
def test_env_vars_set?
|
5
5
|
return true if ENV['localwiki_client_user'] && ENV['localwiki_client_apikey']
|
6
|
-
puts "\nTo
|
6
|
+
puts "\nTo run all tests in the integration folder you need to do two things:"
|
7
7
|
puts "\tContact sethvincent@gmail.com to request a username and apikey on the test server."
|
8
8
|
puts "\tSet these two environment variables:\n\t\texport localwiki_client_user=USERNAME\n\t\texport localwiki_client_apikey=APIKEY"
|
9
9
|
end
|
@@ -5,11 +5,11 @@ describe 'LIVE saltlakewiki.org' do
|
|
5
5
|
subject { Localwiki::Client.new 'saltlakewiki.org' }
|
6
6
|
|
7
7
|
context '#time_zone' do
|
8
|
-
it {subject.time_zone.should eq 'America/Chicago' }
|
8
|
+
it {subject.site.time_zone.should eq 'America/Chicago' }
|
9
9
|
end
|
10
10
|
|
11
11
|
context '#language_code' do
|
12
|
-
it {subject.language_code.should eq 'en-us'}
|
12
|
+
it {subject.site.language_code.should eq 'en-us'}
|
13
13
|
end
|
14
14
|
|
15
15
|
context '#total_resources("user")' do
|
@@ -1,26 +1,26 @@
|
|
1
1
|
require File.expand_path("../helper", __FILE__)
|
2
2
|
|
3
|
-
|
3
|
+
describe 'LIVE testwiki instance' do
|
4
4
|
|
5
|
-
|
5
|
+
before(:all) do
|
6
|
+
@wiki = Localwiki::Client.new 'ec2-54-234-151-52.compute-1.amazonaws.com',
|
7
|
+
ENV['localwiki_client_user'],
|
8
|
+
ENV['localwiki_client_apikey']
|
9
|
+
end
|
6
10
|
|
7
|
-
|
8
|
-
@wiki = Localwiki::Client.new 'ec2-54-234-151-52.compute-1.amazonaws.com',
|
9
|
-
ENV['localwiki_client_user'],
|
10
|
-
ENV['localwiki_client_apikey']
|
11
|
-
}
|
11
|
+
if test_env_vars_set?
|
12
12
|
|
13
13
|
context "CRUD methods" do
|
14
14
|
|
15
15
|
before(:all) do
|
16
16
|
require 'securerandom'
|
17
17
|
@pagename = "TestPage#{SecureRandom.uuid}"
|
18
|
+
json = {name: @pagename, content: '<p>Created!</p>'}.to_json
|
19
|
+
@create_response = @wiki.create('page', json)
|
18
20
|
end
|
19
21
|
|
20
22
|
it "#create('page', json) response.status is 201" do
|
21
|
-
|
22
|
-
response = @wiki.create('page', json)
|
23
|
-
response.status.should eq 201
|
23
|
+
@create_response.status.should eq 201
|
24
24
|
end
|
25
25
|
|
26
26
|
it "#read('page', 'TestPage<uuid>') response.status is 200" do
|
@@ -42,6 +42,21 @@ if test_env_vars_set?
|
|
42
42
|
end
|
43
43
|
|
44
44
|
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "#list" do
|
48
|
+
|
49
|
+
before(:all) do
|
50
|
+
@response = @wiki.list('page', 2)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns collection of resources objects" do
|
54
|
+
@response['objects'].first.keys.length.should be > 1
|
55
|
+
end
|
56
|
+
|
57
|
+
it "limit parameter limits number of resources returned" do
|
58
|
+
@response['objects'].length.should eq 2
|
59
|
+
end
|
45
60
|
|
46
61
|
end
|
47
|
-
end
|
62
|
+
end
|
data/lib/localwiki/client.rb
CHANGED
@@ -10,168 +10,201 @@ module Localwiki
|
|
10
10
|
|
11
11
|
# hostname of the server we'd like to point at
|
12
12
|
attr_accessor :hostname
|
13
|
-
|
14
|
-
# site resource
|
15
|
-
attr_reader :
|
16
|
-
|
17
|
-
# site resource - time zone of server, example: 'America/Chicago'
|
18
|
-
attr_reader :time_zone
|
19
|
-
|
20
|
-
# site resource - language code of the server, example: 'en-us'
|
21
|
-
attr_reader :language_code
|
13
|
+
|
14
|
+
# localwiki site resource
|
15
|
+
attr_reader :site
|
22
16
|
|
23
17
|
##
|
24
18
|
# Create a LocalWikiClient instance
|
25
|
-
#
|
26
|
-
# @
|
27
|
-
#
|
19
|
+
# @param hostname domain host of localwiki server
|
20
|
+
# @param user localwiki username
|
21
|
+
# @param apikey localwiki user's apikey provided by the localwiki server administrator
|
22
|
+
# @example Connect to http\:\/\/seattlewiki.net
|
23
|
+
# wiki = LocalwikiClient.new 'seattlewiki.net'
|
24
|
+
# @example Connect to http\:\/\/seattlewiki.net with user and apikey for write access
|
25
|
+
# wiki = LocalwikiClient.new 'seattlewiki.net', 'myusername', '89f17088f43b5ae22779365b8d1ff0ed076'
|
28
26
|
def initialize hostname, user=nil, apikey=nil
|
29
|
-
@hostname = hostname
|
30
27
|
@user = user
|
31
28
|
@apikey = apikey
|
32
|
-
|
33
|
-
|
29
|
+
@hostname = hostname
|
30
|
+
initialize_connection @hostname
|
34
31
|
end
|
35
32
|
|
36
33
|
##
|
37
34
|
# Request total count of given resource
|
38
|
-
# @param
|
35
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
39
36
|
# @return [Fixnum] resource count
|
40
|
-
# @example
|
41
|
-
|
42
|
-
|
37
|
+
# @example Get the number of users
|
38
|
+
# wiki.count('user')
|
39
|
+
def count(resource_type)
|
40
|
+
path = '/api/' + resource_type.to_s
|
41
|
+
response = http_get(path,{limit: '1'})
|
42
|
+
response["meta"]["total_count"]
|
43
43
|
end
|
44
44
|
|
45
45
|
##
|
46
46
|
# fetch a page by name ("The Page Name" or "The_Page_Name" or "the page name")
|
47
47
|
# @param name "The Page Name" or "The_Page_Name" or "the page name"
|
48
|
-
# @return [
|
48
|
+
# @return [Localwiki::Page] the parsed JSON object from the response body, otherwise the whole http response object
|
49
49
|
def page_by_name(name)
|
50
|
-
fetch(:page,
|
50
|
+
fetch(:page, name)
|
51
51
|
end
|
52
52
|
|
53
53
|
##
|
54
54
|
# list of a specific type of resource
|
55
|
-
# @param
|
56
|
-
# @param limit
|
57
|
-
# @param params
|
58
|
-
# @return [
|
59
|
-
|
60
|
-
|
55
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
56
|
+
# @param limit maximum number to return
|
57
|
+
# @param params hash of query string params
|
58
|
+
# @return [Array, Faraday::Response] array of Localwiki::Resource objects, or the http response object
|
59
|
+
# @example Get the first 10 files
|
60
|
+
# wiki.list(:file, 10)
|
61
|
+
# #=> [ #<Localwiki::File>, ... ]
|
62
|
+
def list(resource_type,limit=0,params={})
|
63
|
+
path = '/api/' + resource_type.to_s
|
61
64
|
params.merge!({limit: limit.to_s})
|
62
|
-
http_get(
|
65
|
+
response = http_get(path,params)
|
66
|
+
hydrate_list(resource_type, response['objects'])
|
63
67
|
end
|
64
68
|
|
65
69
|
##
|
66
70
|
# fetch a specific resource
|
67
|
-
# @param
|
68
|
-
# @param identifier
|
69
|
-
# @param params
|
70
|
-
# @example
|
71
|
-
#
|
72
|
-
|
73
|
-
|
74
|
-
|
71
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
72
|
+
# @param identifier id, pagename, slug, etc.
|
73
|
+
# @param params hash of query string params
|
74
|
+
# @example Get the page with the name 'Schedule'
|
75
|
+
# wiki.fetch(:page, 'Schedule')
|
76
|
+
# #=> #<Localwiki::Page>
|
77
|
+
# @return [Localwiki::Resource, Faraday::Response] the resource, or the http response object
|
78
|
+
def fetch(resource_type,identifier,params={})
|
79
|
+
path = '/api/' + resource_type.to_s + '/' + slugify(identifier)
|
80
|
+
hydrate(resource_type, http_get(path,params))
|
75
81
|
end
|
76
82
|
alias_method :read, :fetch
|
77
83
|
|
78
84
|
##
|
79
85
|
# fetch version information for a resource
|
80
|
-
# @param
|
81
|
-
# @param identifier
|
82
|
-
# @param params
|
83
|
-
# @example
|
84
|
-
#
|
85
|
-
|
86
|
-
|
87
|
-
|
86
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
87
|
+
# @param identifier id, pagename, slug, etc.
|
88
|
+
# @param params hash of query string params
|
89
|
+
# @example Get the version history for page called 'First Page'
|
90
|
+
# wiki.fetch_version(:page, 'First Page')
|
91
|
+
# @return [Hash, Faraday::Response] hash from json response body, otherwise the http response object
|
92
|
+
def fetch_version(resource_type,identifier,params={})
|
93
|
+
path = '/api/' + resource_type.to_s + '_version?name=' + identifier
|
94
|
+
http_get(path,params)
|
88
95
|
end
|
89
96
|
|
90
97
|
##
|
91
98
|
# number of unique authors that have modified a resource
|
92
|
-
# @param
|
93
|
-
# @param identifier
|
94
|
-
# @param params
|
95
|
-
# @example
|
96
|
-
#
|
97
|
-
|
98
|
-
|
99
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
100
|
+
# @param identifier id, pagename, slug, etc.
|
101
|
+
# @param params hash of query string params
|
102
|
+
# @example Get number of unique authors for the page 'First Page'
|
103
|
+
# wiki.unique_authors(:page, 'First Page')
|
104
|
+
# @return [Fixnum] number of unique authors
|
105
|
+
def unique_authors(resource_type,identifier,params={})
|
106
|
+
json = fetch_version(resource_type,identifier,params)
|
99
107
|
json['objects'].map {|entry| entry['history_user']}.uniq.length
|
100
108
|
end
|
101
109
|
|
102
110
|
##
|
103
|
-
# create a specific resource
|
104
|
-
# @param
|
105
|
-
# @param json
|
106
|
-
# @example
|
107
|
-
#
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
+
# create a specific resource. LocalwikiClient must have been initialized with user and apikey.
|
112
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
113
|
+
# @param json json string
|
114
|
+
# @example Create a page from the json string
|
115
|
+
# wiki.create(:page, {name: 'New Page', content: '<p>A New Page!</p>'}.to_json)
|
116
|
+
# @return [Faraday::Response] http response object
|
117
|
+
def create(resource_type, json)
|
118
|
+
path = '/api/' + resource_type.to_s + '/'
|
119
|
+
http_post(path, json)
|
111
120
|
end
|
112
121
|
|
113
122
|
##
|
114
|
-
# update a specific resource
|
115
|
-
# @param
|
116
|
-
# @param identifier
|
117
|
-
# @param json
|
118
|
-
# @example
|
119
|
-
#
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
+
# update a specific resource. LocalwikiClient must have been initialized with user and apikey.
|
124
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
125
|
+
# @param identifier id, pagename, slug, etc.
|
126
|
+
# @param json json string
|
127
|
+
# @example Update an existing page with json string
|
128
|
+
# wiki.update(:page, {name: 'Existing Page', content: '<p>Updated Page!</p>'}.to_json)
|
129
|
+
# @return [Faraday::Response] http response object
|
130
|
+
def update(resource_type,identifier,json)
|
131
|
+
path = '/api/' + resource_type.to_s + '/' + slugify(identifier)
|
132
|
+
http_put(path, json)
|
123
133
|
end
|
124
134
|
|
125
135
|
##
|
126
|
-
# delete a specific resource
|
127
|
-
# @param
|
128
|
-
# @param identifier
|
129
|
-
# @example
|
130
|
-
#
|
131
|
-
|
132
|
-
|
133
|
-
|
136
|
+
# delete a specific resource. LocalwikiClient must have been initialized with user and apikey.
|
137
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
138
|
+
# @param identifier id, pagename, slug, etc.
|
139
|
+
# @example Delete the tag 'library'
|
140
|
+
# wiki.delete(:tag, 'library')
|
141
|
+
# @return [Faraday::Response] http response object
|
142
|
+
def delete(resource_type,identifier)
|
143
|
+
path = '/api/' + resource_type.to_s + '/' + slugify(identifier)
|
144
|
+
http_delete(path)
|
134
145
|
end
|
135
146
|
|
136
147
|
private
|
137
148
|
|
138
149
|
##
|
139
|
-
#
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
@
|
144
|
-
|
150
|
+
# prepend the hostname
|
151
|
+
# @param path path portion of a url
|
152
|
+
# @return [String] entire url
|
153
|
+
def full_url(path)
|
154
|
+
'http://' + @hostname + path.to_s
|
155
|
+
end
|
156
|
+
|
157
|
+
##
|
158
|
+
# Create the appropriate resource object
|
159
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
160
|
+
# @param [Hash, Faraday::Response] param object to create resource from, or http response object
|
161
|
+
# @return [Localwiki::Resource, Faraday::Response] resource object, or http response object
|
162
|
+
def hydrate(resource_type,param)
|
163
|
+
# skip if given an http response object
|
164
|
+
return param if param.respond_to? :status
|
165
|
+
Localwiki::make_one(resource_type,param)
|
145
166
|
end
|
146
167
|
|
147
168
|
##
|
148
|
-
#
|
149
|
-
|
150
|
-
|
169
|
+
# Create array of the appropriate resource objects
|
170
|
+
# @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
|
171
|
+
# @param [Array, Faraday::Response] param array of objects to create resources from, or http response object
|
172
|
+
# @return [Array, Faraday::Response] array of resource objects, or http response object
|
173
|
+
def hydrate_list(resource_type,param)
|
174
|
+
# skip if given an http response object
|
175
|
+
return param if param.respond_to? :status
|
176
|
+
param.collect do |json_hash|
|
177
|
+
Localwiki::make_one(resource_type,json_hash)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
##
|
182
|
+
# initialize Faraday::Connection instance. set @conn and @site
|
183
|
+
# @param [String] hostname localwiki server hostname
|
184
|
+
def initialize_connection(hostname)
|
185
|
+
@conn = Faraday.new :url => hostname
|
186
|
+
@site = fetch('site','1')
|
151
187
|
end
|
152
188
|
|
153
189
|
##
|
154
190
|
# http get request
|
155
|
-
# @param
|
156
|
-
# @param params
|
157
|
-
# @return [Hash]
|
158
|
-
def http_get(
|
191
|
+
# @param path /api/<resource>/<resource identifier>
|
192
|
+
# @param params hash of query string params
|
193
|
+
# @return [Hash, Faraday::Response] hash from json response body, otherwise the http response object
|
194
|
+
def http_get(path,params={})
|
159
195
|
params.merge!({format: 'json'})
|
160
|
-
|
161
|
-
response = @site.get full_url, params
|
196
|
+
response = @conn.get full_url(path), params
|
162
197
|
JSON.parse(response.body) rescue response
|
163
198
|
end
|
164
199
|
|
165
200
|
##
|
166
201
|
# http post request
|
167
|
-
# @param
|
168
|
-
# @param json
|
169
|
-
# @return [
|
170
|
-
def http_post(
|
171
|
-
|
172
|
-
|
173
|
-
@site.post do |req|
|
174
|
-
req.url full_url
|
202
|
+
# @param path /api/<resource>/
|
203
|
+
# @param json json string
|
204
|
+
# @return [Faraday::Response] http response object
|
205
|
+
def http_post(path, json)
|
206
|
+
@conn.post do |req|
|
207
|
+
req.url full_url(path)
|
175
208
|
req.headers['Content-Type'] = 'application/json'
|
176
209
|
req.headers['Authorization'] = "ApiKey #{@user}:#{@apikey}"
|
177
210
|
req.body = json
|
@@ -180,14 +213,12 @@ module Localwiki
|
|
180
213
|
|
181
214
|
##
|
182
215
|
# http put request
|
183
|
-
# @param
|
184
|
-
# @param json
|
185
|
-
# @return [
|
186
|
-
def http_put(
|
187
|
-
|
188
|
-
|
189
|
-
@site.put do |req|
|
190
|
-
req.url full_url
|
216
|
+
# @param path /api/<resource>/<resource identifier>
|
217
|
+
# @param json json string
|
218
|
+
# @return [Faraday::Response] http response object
|
219
|
+
def http_put(path, json)
|
220
|
+
@conn.put do |req|
|
221
|
+
req.url full_url(path)
|
191
222
|
req.headers['Content-Type'] = 'application/json'
|
192
223
|
req.headers['Authorization'] = "ApiKey #{@user}:#{@apikey}"
|
193
224
|
req.body = json
|
@@ -196,13 +227,11 @@ module Localwiki
|
|
196
227
|
|
197
228
|
##
|
198
229
|
# http delete request
|
199
|
-
# @param
|
200
|
-
# @return [
|
201
|
-
def http_delete(
|
202
|
-
|
203
|
-
|
204
|
-
@site.delete do |req|
|
205
|
-
req.url full_url
|
230
|
+
# @param path /api/<resource>/<resource identifier>
|
231
|
+
# @return [Faraday::Response] http response object
|
232
|
+
def http_delete(path)
|
233
|
+
@conn.delete do |req|
|
234
|
+
req.url full_url(path)
|
206
235
|
req.headers['Content-Type'] = 'application/json'
|
207
236
|
req.headers['Authorization'] = "ApiKey #{@user}:#{@apikey}"
|
208
237
|
end
|
@@ -211,7 +240,6 @@ module Localwiki
|
|
211
240
|
##
|
212
241
|
# create human readable identifier that is used to create clean urls
|
213
242
|
# @param string
|
214
|
-
# @example slugify('My Page') == 'My_Page'
|
215
243
|
# @return [String]
|
216
244
|
def slugify(string)
|
217
245
|
string.to_s.strip.gsub(' ', "_")
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
module Localwiki
|
4
|
+
|
5
|
+
##
|
6
|
+
# create instance (of correct type) for resource
|
7
|
+
# @param type resource type "site", "page", "user", "file", "map", "tag", "page_tags"
|
8
|
+
# @param json_hash [Hash] hash object from parsed json
|
9
|
+
# @return new instance - child class of Localwiki::Resource
|
10
|
+
def self.make_one(type, json_hash)
|
11
|
+
klass_name = type.to_s.split('_').map {|s|s.capitalize}.join
|
12
|
+
Object.const_get('Localwiki').const_get(klass_name).new(json_hash)
|
13
|
+
end
|
14
|
+
|
15
|
+
##
|
16
|
+
# simple OpenStruct object to allow attribute access on resources (instead of hash key access)
|
17
|
+
class Resource < OpenStruct
|
18
|
+
|
19
|
+
# raw json string used to instanciate object
|
20
|
+
attr_reader :json
|
21
|
+
|
22
|
+
##
|
23
|
+
# create instance of resource
|
24
|
+
# @param [Hash] json_hash hash object from parsed json
|
25
|
+
def initialize json_hash
|
26
|
+
@json = json_hash
|
27
|
+
super
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# http://localwiki.readthedocs.org/en/latest/api.html#site
|
32
|
+
class Site < Resource; end
|
33
|
+
|
34
|
+
# http://localwiki.readthedocs.org/en/latest/api.html#pages
|
35
|
+
class Page < Resource; end
|
36
|
+
|
37
|
+
##
|
38
|
+
# represents map object returned from server
|
39
|
+
# http://localwiki.readthedocs.org/en/latest/api.html#maps
|
40
|
+
class Map < Resource
|
41
|
+
|
42
|
+
def single_point?
|
43
|
+
self.points && self.points['coordinates'].size == 1
|
44
|
+
end
|
45
|
+
|
46
|
+
def line?
|
47
|
+
self.lines
|
48
|
+
end
|
49
|
+
|
50
|
+
def poly?
|
51
|
+
self.polys
|
52
|
+
end
|
53
|
+
|
54
|
+
def lat
|
55
|
+
self.points['coordinates'].first[0] if self.single_point?
|
56
|
+
end
|
57
|
+
|
58
|
+
def long
|
59
|
+
self.points['coordinates'].first[1] if self.single_point?
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
# http://localwiki.readthedocs.org/en/latest/api.html#files
|
65
|
+
class File < Resource; end
|
66
|
+
|
67
|
+
# http://localwiki.readthedocs.org/en/latest/api.html#tags
|
68
|
+
class Tag < Resource; end
|
69
|
+
|
70
|
+
# http://localwiki.readthedocs.org/en/latest/api.html#page-tags
|
71
|
+
class PageTags < Resource; end
|
72
|
+
|
73
|
+
# http://localwiki.readthedocs.org/en/latest/api.html#users
|
74
|
+
class User < Resource; end
|
75
|
+
|
76
|
+
end
|
data/lib/localwiki/version.rb
CHANGED
data/lib/localwiki_client.rb
CHANGED
data/localwiki_client.gemspec
CHANGED
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.add_development_dependency('webmock', '>= 1.8.0', '< 1.10')
|
23
23
|
s.add_development_dependency('yard')
|
24
24
|
s.add_development_dependency('kramdown')
|
25
|
+
s.add_development_dependency('simplecov')
|
25
26
|
|
26
27
|
s.add_development_dependency('rb-fsevent')
|
27
28
|
|