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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/History.txt +5 -0
  4. data/README.md +17 -9
  5. data/Rakefile +6 -0
  6. data/integration/helper.rb +1 -1
  7. data/integration/live_saltlake_wiki_spec.rb +2 -2
  8. data/integration/live_test_wiki_spec.rb +26 -11
  9. data/lib/localwiki/client.rb +143 -115
  10. data/lib/localwiki/resource.rb +76 -0
  11. data/lib/localwiki/version.rb +1 -1
  12. data/lib/localwiki_client.rb +1 -0
  13. data/localwiki_client.gemspec +1 -0
  14. data/spec/basic_crud_spec.rb +12 -5
  15. data/spec/basic_spec.rb +2 -2
  16. data/spec/fixtures/cassettes/basic.yml +21 -19
  17. data/spec/fixtures/cassettes/basic_crud.yml +21 -19
  18. data/spec/fixtures/cassettes/basic_crud_delete_fail.yml +11 -9
  19. data/spec/fixtures/cassettes/basic_crud_delete_success.yml +10 -8
  20. data/spec/fixtures/cassettes/basic_crud_page_by_name_success.yml +40 -0
  21. data/spec/fixtures/cassettes/basic_crud_read_fail.yml +22 -34
  22. data/spec/fixtures/cassettes/basic_crud_read_success.yml +11 -11
  23. data/spec/fixtures/cassettes/basic_crud_update_success.yml +11 -9
  24. data/spec/fixtures/cassettes/basic_fetch_version_success.yml +41 -37
  25. data/spec/fixtures/cassettes/basic_page_by_name_spaces.yml +12 -12
  26. data/spec/fixtures/cassettes/basic_unique_authors_success.yml +41 -37
  27. data/spec/fixtures/cassettes/fetch.yml +8 -8
  28. data/spec/fixtures/cassettes/list.yml +93 -0
  29. data/spec/fixtures/cassettes/list_file_success.yml +40 -0
  30. data/spec/fixtures/cassettes/list_limit_success.yml +124 -0
  31. data/spec/fixtures/cassettes/list_map_success.yml +42 -0
  32. data/spec/fixtures/cassettes/list_page_success.yml +1108 -0
  33. data/spec/fixtures/cassettes/list_page_tags_success.yml +42 -0
  34. data/spec/fixtures/cassettes/list_site_success.yml +46 -0
  35. data/spec/fixtures/cassettes/list_tag_success.yml +43 -0
  36. data/spec/fixtures/cassettes/list_user_success.yml +56 -0
  37. data/spec/fixtures/cassettes/localwiki_client.yml +121 -115
  38. data/spec/fixtures/cassettes/slug.yml +8 -8
  39. data/spec/helper.rb +7 -0
  40. data/spec/list_spec.rb +47 -0
  41. data/spec/localwiki_client_spec.rb +4 -12
  42. data/spec/localwiki_spec.rb +17 -0
  43. data/spec/map_spec.rb +135 -0
  44. data/spec/resource_spec.rb +60 -0
  45. metadata +33 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 57676e3cee0822abe0c07ab173531579f1cccb21
4
- data.tar.gz: c7b7b0370f9372cca7b2149e0374737a04fca115
3
+ metadata.gz: bb9cc507732637d939aa2fdea6e73523e17b0765
4
+ data.tar.gz: c2eaa613db6c5df7cb2dad1940d693fa0a16907b
5
5
  SHA512:
6
- metadata.gz: 9a420a1750d4d72858603feff2eeb7656e27649526b5138eeffb8d933fc685e5de239515df86f2cbaed82e46c1b760c0d28b18122d1816f27d34f1218fcc0a9c
7
- data.tar.gz: 254a56caf270b691b689ea326f8d3000ebe211aedfece5b411646f9609997ccc06c9caefe0b03d1ecbe80c322cdac7a17b1f189c16288c918138fadc4d407bd9
6
+ metadata.gz: a62093cafaa6429868d66ff1c4a15187f37c9e93b8fec8666bc9d36602884feba589b18a201aca253aee61e57b193e3d72c61b023ac4acd3dfdd7113cb94fc60
7
+ data.tar.gz: 6368549d3fa07caf1678080786626cbcd14442c1864ed176774ce6ef80665a7b6190f0f15b467b8da57dabbd13db055da09b9d62cea6c82f3d0e083c840dbe9a
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  .yardoc
2
2
  doc/
3
3
  .idea
4
- Gemfile.lock
4
+ Gemfile.lock
5
+ coverage
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ === 0.3.0 / 2013-03-28
2
+
3
+ * Localwiki::Resource subclasses used to represent resources
4
+ * Better docs
5
+
1
6
  === 0.2.0 / 2013-03-16
2
7
 
3
8
  * Implemented CRUD methods
data/README.md CHANGED
@@ -5,7 +5,7 @@ LocalwikiClient
5
5
  Synopsis
6
6
  --------
7
7
 
8
- A thin client for the Localwiki API
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.site_name
26
+ wiki.site.name
27
27
  => SeattleWiki
28
- wiki.count('user')
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
- page_json = {name: 'Current Events', content: 'Coming Soon!'}.to_json
34
- response = wiki.create('page', page_json)
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
- * #create, #read (#fetch), #update, #delete provide basic CRUD functionality
42
- * #list fetches all (or with a limit) of a specific type of resource
43
- * #fetch_version retrieves version history for a resource
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
- * #count, #page_by_name, #unique_authors
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')
@@ -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 add or modify tests in the spec folder you need to do two things:"
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
- if test_env_vars_set?
3
+ describe 'LIVE testwiki instance' do
4
4
 
5
- describe 'LIVE testwiki instance' do
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
- before(:all) {
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
- json = {name: @pagename, content: '<p>Created!</p>'}.to_json
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
@@ -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 - display name of wiki
15
- attr_reader :site_name
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
- # @example LocalwikiClient.new 'seattlewiki.net'
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
- create_connection
33
- collect_site_details
29
+ @hostname = hostname
30
+ initialize_connection @hostname
34
31
  end
35
32
 
36
33
  ##
37
34
  # Request total count of given resource
38
- # @param resource are "site", "page", "user", "file", "map", "tag", "page_tag"
35
+ # @param resource_type "site", "page", "user", "file", "map", "tag", "page_tags"
39
36
  # @return [Fixnum] resource count
40
- # @example wiki.count('user')
41
- def count(resource)
42
- list(resource.to_s,1)["meta"]["total_count"]
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 [Hash] the parsed JSON object from the response body, otherwise the whole http response object
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,"#{name.gsub!(/\s/, '_')}")
50
+ fetch(:page, name)
51
51
  end
52
52
 
53
53
  ##
54
54
  # list of a specific type of resource
55
- # @param resource are "site", "page", "user", "file", "map", "tag", "page_tag"
56
- # @param limit is an integer
57
- # @param params is a hash of query string params
58
- # @return [Hash] the parsed JSON object from the response body, otherwise the whole http response object
59
- def list(resource,limit=0,params={})
60
- uri = '/api/' + resource.to_s
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(uri,params)
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 resource are "site", "page", "user", "file", "map", "tag", "page_tag"
68
- # @param identifier is id, pagename, slug, etc.
69
- # @param params is a hash of query string params
70
- # @example wiki.update('page', '<page tag>')
71
- # @return [Hash] the parsed JSON object from the response body, otherwise the whole http response object
72
- def fetch(resource,identifier,params={})
73
- uri = '/api/' + resource.to_s + '/' + slugify(identifier)
74
- http_get(uri,params)
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 resource are "site", "page", "user", "file", "map", "tag", "page_tag"
81
- # @param identifier is id, pagename, slug, etc.
82
- # @param params is a hash of query string params
83
- # @example wiki.fetch_version('page', 'First Page')
84
- # @return [Hash] the parsed JSON object from the response body, otherwise the whole http response object
85
- def fetch_version(resource,identifier,params={})
86
- uri = '/api/' + resource.to_s + '_version?name=' + identifier
87
- http_get(uri,params)
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 resource are "site", "page", "user", "file", "map", "tag", "page_tag"
93
- # @param identifier is id, pagename, slug, etc.
94
- # @param params is a hash of query string params
95
- # @example wiki.unique_authors('page', 'First Page')
96
- # @return [Fixnum]
97
- def unique_authors(resource,identifier,params={})
98
- json = fetch_version(resource,identifier,params)
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 resource are "site", "page", "user", "file", "map", "tag", "page_tag"
105
- # @param json is a json object
106
- # @example wiki.create('page', <json object containing the page tag>)
107
- # @return [HTTPResponse] the http response object
108
- def create(resource, json)
109
- uri = '/api/' + resource.to_s + '/'
110
- http_post(uri, json)
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 resource are "site", "page", "user", "file", "map", "tag", "page_tag"
116
- # @param identifier is id, pagename, slug, etc.
117
- # @param json is a json object
118
- # @example wiki.update('page', '<page tag>', <json object>)
119
- # @return [HTTPResponse] the http response object
120
- def update(resource,identifier,json)
121
- uri = '/api/' + resource.to_s + '/' + slugify(identifier)
122
- http_put(uri, json)
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 resource are "site", "page", "user", "file", "map", "tag", "page_tag"
128
- # @param identifier is id, pagename, slug, etc.
129
- # @example wiki.delete('page', '<page tag>')
130
- # @return [HTTPResponse] the http response object
131
- def delete(resource,identifier)
132
- uri = '/api/' + resource.to_s + '/' + slugify(identifier)
133
- http_delete(uri)
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
- # Get site resource and set instance variables
140
- def collect_site_details
141
- site = fetch('site','1')
142
- @site_name = site['name']
143
- @time_zone = site['time_zone']
144
- @language_code = site['language_code']
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
- # create Faraday::Connection instance and set @site
149
- def create_connection
150
- @site = Faraday.new :url => @hostname
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 uri /api/<resource>/<resource identifier>
156
- # @param params is a hash of query string params
157
- # @return [Hash] the parsed JSON object, otherwise the http response object
158
- def http_get(uri,params={})
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
- full_url = 'http://' + @hostname + uri.to_s
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 uri /api/<resource>/
168
- # @param json is a json object
169
- # @return [HTTPResponse] the http response object
170
- def http_post(uri, json)
171
- full_url = 'http://' + @hostname + uri.to_s
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 uri /api/<resource>/<resource identifier>
184
- # @param json is a json object
185
- # @return [HTTPResponse] the http response object
186
- def http_put(uri, json)
187
- full_url = 'http://' + @hostname + uri.to_s
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 uri /api/<resource>/<resource identifier>
200
- # @return [HTTPResponse] the http response object
201
- def http_delete(uri)
202
- full_url = 'http://' + @hostname + uri.to_s
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
@@ -1,5 +1,5 @@
1
1
  module Localwiki
2
2
 
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
 
5
5
  end
@@ -1,6 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__)) unless
2
2
  $LOAD_PATH.include?(File.dirname(__FILE__)) || $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
+ require 'localwiki/resource'
4
5
  require 'localwiki/client'
5
6
  require 'localwiki/version'
6
7
 
@@ -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