popit 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +15 -14
- data/lib/popit/version.rb +1 -1
- data/lib/popit.rb +35 -5
- data/spec/popit_spec.rb +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -2,7 +2,6 @@
|
|
2
2
|
|
3
3
|
A Ruby wrapper for the [PopIt](http://popit.mysociety.org/) API, which allows you to create, read, update and delete items from PopIt.
|
4
4
|
|
5
|
-
[](http://travis-ci.org/opennorth/popit-ruby)
|
6
5
|
[](https://gemnasium.com/opennorth/popit-ruby)
|
7
6
|
[](https://codeclimate.com/github/opennorth/popit-ruby)
|
8
7
|
|
@@ -12,19 +11,19 @@ A Ruby wrapper for the [PopIt](http://popit.mysociety.org/) API, which allows yo
|
|
12
11
|
|
13
12
|
## API Examples
|
14
13
|
|
15
|
-
|
14
|
+
First, require the PopIt gem:
|
16
15
|
|
17
16
|
```ruby
|
18
17
|
require 'popit'
|
19
18
|
```
|
20
19
|
|
21
|
-
|
20
|
+
Then, create an API client for PopIt:
|
22
21
|
|
23
22
|
```ruby
|
24
23
|
api = PopIt.new :instance_name => 'demo'
|
25
24
|
```
|
26
25
|
|
27
|
-
You can pass
|
26
|
+
You can pass these options to `PopIt.new`:
|
28
27
|
|
29
28
|
* `:instance_name` the PopIt instance, usually the first part of the domain name
|
30
29
|
* `:host_name` the PopIt API's host name – defaults to "popit.mysociety.org"
|
@@ -33,7 +32,7 @@ You can pass the options:
|
|
33
32
|
* `:user` a user name – if blank, the API will be read-only
|
34
33
|
* `:password` the user's password
|
35
34
|
|
36
|
-
For brevity, we only show examples for `person` items, but you use the same code to operate on organisations and positions by substituting `organisation` or `position` for `person`.
|
35
|
+
For brevity, we only show examples below for `person` items, but you can use the same code to operate on organisations and positions by substituting `organisation` or `position` for `person`.
|
37
36
|
|
38
37
|
More documentation at [RubyDoc.info](http://rdoc.info/gems/popit/PopIt).
|
39
38
|
|
@@ -53,12 +52,14 @@ response = api.person('47cc67093475061e3d95369d').get
|
|
53
52
|
p response['result']
|
54
53
|
```
|
55
54
|
|
56
|
-
You can search...
|
55
|
+
You can also search for...
|
57
56
|
|
58
57
|
* people by slug, name or summary
|
59
58
|
* organisations by slug or name
|
60
59
|
* positions by title, person or organisation
|
61
60
|
|
61
|
+
For example:
|
62
|
+
|
62
63
|
```ruby
|
63
64
|
response = api.person.get :name => 'John Doe'
|
64
65
|
p response['results']
|
@@ -74,30 +75,30 @@ id = response['result']['_id']
|
|
74
75
|
### Update
|
75
76
|
|
76
77
|
```ruby
|
77
|
-
response = api.person(
|
78
|
+
response = api.person(id).put :name => 'Jane Doe'
|
78
79
|
p response['result']
|
79
80
|
```
|
80
81
|
|
81
82
|
### Delete
|
82
83
|
|
83
84
|
```ruby
|
84
|
-
success = api.person(
|
85
|
+
success = api.person(id).delete
|
85
86
|
```
|
86
87
|
|
87
88
|
## Error Handling
|
88
89
|
|
89
|
-
If you:
|
90
|
+
If you attempt to:
|
90
91
|
|
91
|
-
*
|
92
|
-
*
|
93
|
-
*
|
92
|
+
* read an item that doesn't exist
|
93
|
+
* create, update or delete an item without authenticating
|
94
|
+
* operate on something other than people, organisations and positions
|
94
95
|
|
95
|
-
you will raise a `PopIt::Error` exception. The exception's message will be the
|
96
|
+
you will raise a `PopIt::Error` exception. The exception's message will be the same as from the PopIt API.
|
96
97
|
|
97
98
|
```ruby
|
98
99
|
require 'popit'
|
99
100
|
api = PopIt.new :instance_name => 'demo'
|
100
|
-
api.person.get 'foo' # raises PopIt::Error
|
101
|
+
api.person.get 'foo' # raises PopIt::Error with {"error":"page not found"}
|
101
102
|
```
|
102
103
|
|
103
104
|
## Running Tests
|
data/lib/popit/version.rb
CHANGED
data/lib/popit.rb
CHANGED
@@ -3,6 +3,20 @@ require 'yajl'
|
|
3
3
|
|
4
4
|
# A Ruby wrapper for the PopIt API.
|
5
5
|
#
|
6
|
+
# Instead of writing the path to an API endpoint, you can use method chaining.
|
7
|
+
# For example:
|
8
|
+
#
|
9
|
+
# require 'popit'
|
10
|
+
# api = PopIt.new :instance_name => 'demo'
|
11
|
+
# api.get 'person/john-doe'
|
12
|
+
#
|
13
|
+
# can be written as:
|
14
|
+
#
|
15
|
+
# api.person('john-doe').get
|
16
|
+
#
|
17
|
+
# All methods and arguments between `api` and the HTTP method - in this case,
|
18
|
+
# `get` - become parts of the path.
|
19
|
+
#
|
6
20
|
# @see https://github.com/mysociety/popit/blob/master/lib/apps/api/api_v1.js
|
7
21
|
class PopIt
|
8
22
|
class Error < StandardError; end
|
@@ -33,22 +47,38 @@ class PopIt
|
|
33
47
|
@password = opts[:password]
|
34
48
|
end
|
35
49
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
50
|
+
# Send a GET request.
|
51
|
+
#
|
52
|
+
# @param [String] path a path with no leading slash
|
53
|
+
# @param [Hash] opts key-value pairs for the query string
|
54
|
+
# @return the JSON response from the server
|
40
55
|
def get(path, opts = {})
|
41
56
|
request :get, path, opts
|
42
57
|
end
|
43
58
|
|
59
|
+
# Send a POST request.
|
60
|
+
#
|
61
|
+
# @param [String] path a path with no leading slash
|
62
|
+
# @param [Hash] opts key-value pairs for the message body
|
63
|
+
# @return the JSON response from the server
|
44
64
|
def post(path, opts = {})
|
45
65
|
request :post, path, opts
|
46
66
|
end
|
47
67
|
|
68
|
+
# Send a PUT request.
|
69
|
+
#
|
70
|
+
# @param [String] path a path with no leading slash
|
71
|
+
# @param [Hash] opts key-value pairs for the message body
|
72
|
+
# @return [nil] nothing
|
48
73
|
def put(path, opts = {})
|
49
74
|
request :put, path, opts
|
50
75
|
end
|
51
76
|
|
77
|
+
# Send a DELETE request.
|
78
|
+
#
|
79
|
+
# @param [String] path a path with no leading slash
|
80
|
+
# @param [Hash] opts key-value pairs for the query string
|
81
|
+
# @return [Hash] an empty hash
|
52
82
|
def delete(path, opts = {})
|
53
83
|
request :delete, path, opts
|
54
84
|
end
|
@@ -56,7 +86,7 @@ class PopIt
|
|
56
86
|
private
|
57
87
|
|
58
88
|
def request(http_method, path, opts = {})
|
59
|
-
path = "
|
89
|
+
path = "http://#{instance_name}.#{host_name}:#{port}/api/#{version}/#{path}"
|
60
90
|
|
61
91
|
response = case http_method
|
62
92
|
when :get
|
data/spec/popit_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require 'yaml'
|
|
4
4
|
|
5
5
|
# We don't want to test the PopIt API. We want to check that the wrapper works.
|
6
6
|
#
|
7
|
-
# @see https://github.com/mysociety/popit
|
7
|
+
# @see https://github.com/mysociety/popit/blob/master/lib/apps/api/api_v1.js
|
8
8
|
describe PopIt do
|
9
9
|
let :unauthenticated do
|
10
10
|
PopIt.new :instance_name => 'tttest'
|