served 0.1.6 → 0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -33
- data/lib/served/http_client.rb +9 -9
- data/lib/served/resource/base.rb +6 -6
- data/lib/served/version.rb +1 -1
- data/served.gemspec +1 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fc33f665810efb0338be77783f46f28a79604a3
|
4
|
+
data.tar.gz: 9141fc737d07f6d9d8fdf235cbb3ac6dfd335a89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f38dae786e45652389e082659118258eb77d6bbccf34fe9f358321bfca0b2e416a7f7080c4523a6ccfa927fed2503e9aceb0d80b61755475c015eb71ed215658
|
7
|
+
data.tar.gz: 15ac1e21e7e1557b53de03862cb3a622e3b0fa33618c010d64c0fa3545e3e9b3a7fdf809db0369bca22a1302c8755352691ff375145c16dd5e8296c0aafde22e
|
data/README.md
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
# Served
|
2
2
|
[![Build Status](https://travis-ci.org/optoro/served.svg)](https://travis-ci.org/optoro/served)
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/served.svg)](https://badge.fury.io/rb/served)
|
3
4
|
|
4
5
|
Served is a gem in the vein of [ActiveResource](https://github.com/rails/activeresource) designed to facilitate
|
5
|
-
communication between distributed
|
6
|
+
communication between distributed services and APIs.
|
6
7
|
|
7
8
|
# Installation
|
8
9
|
|
@@ -10,58 +11,55 @@ Add the following to your Gemfile:
|
|
10
11
|
|
11
12
|
```gem 'served'```
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
A service model can be created by declaring a class inheriting from ```Service::Resource::Base```.
|
14
|
+
# Configuration
|
15
|
+
Served is configured by passing a block to ```Served::configure```.
|
16
16
|
|
17
17
|
```ruby
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
18
|
+
Served.configure do |config|
|
19
|
+
config.hosts = {
|
20
|
+
'some_service' => 'http://localhost:3000'
|
21
|
+
}
|
22
|
+
|
23
|
+
config.timeout = 100
|
24
24
|
end
|
25
25
|
```
|
26
26
|
|
27
|
-
##
|
28
|
-
Served
|
29
|
-
|
30
|
-
|
27
|
+
## Hosts
|
28
|
+
Served models derive their hostname by mapping their parent module to the ```Served::hosts``` configuration hash. For
|
29
|
+
example, ```SomeService::SomeResource``` would look up its host configuration at
|
30
|
+
```Served.config.hosts['some_service']```.
|
31
31
|
|
32
|
-
|
32
|
+
The host configuration accepts an (Addressable)[https://github.com/sporkmonger/addressable] template mapping
|
33
|
+
```resource``` as the resource name (derived from the model name) and ```query``` as the params. For example:
|
33
34
|
|
34
|
-
|
35
|
+
```
|
36
|
+
http://localhost:3000/{resource}{?query*}
|
37
|
+
```
|
35
38
|
|
36
|
-
|
39
|
+
For more on building Addressable templates view the addressable documentation. If the host configuration is not an
|
40
|
+
Addressable template, a default template will be applied (```{resource}.json{?query*}```). This is the current
|
41
|
+
maintained for backwards compatibility, however the extension will likely be removed for the 0.2 release.
|
37
42
|
|
38
|
-
|
43
|
+
## Timeout
|
44
|
+
Sets the request timeout in milliseconds.
|
39
45
|
|
40
|
-
Served uses the adjacent namespace of the resource to determine the the url for that service. For example given this
|
41
|
-
configuration:
|
42
46
|
|
43
|
-
|
44
|
-
|
45
|
-
c.hosts = {
|
46
|
-
some_service: 'http://localhost:3000',
|
47
|
-
some_other_service: 'http://localhost:3001'
|
48
|
-
}
|
49
|
-
```
|
50
|
-
|
51
|
-
and given this resource
|
47
|
+
# Usage
|
48
|
+
A service model can be created by declaring a class inheriting from ```Service::Resource::Base```.
|
52
49
|
|
53
50
|
```ruby
|
54
51
|
class SomeService::SomeResource < Served::Resource::Base
|
55
52
|
|
56
53
|
attribute :name
|
57
54
|
attribute :date
|
58
|
-
attribute :phone_number
|
55
|
+
attribute :phone_number, default: '555-555-5555'
|
59
56
|
|
60
57
|
end
|
61
58
|
```
|
62
59
|
|
63
|
-
|
60
|
+
## Saving a resource
|
61
|
+
Served follows resourceful standards. When a resource is initially saved a **POST** request will be sent
|
62
|
+
to the service. When the resource already exists, a **PUT** request will be sent. Served determines if
|
63
|
+
a resource is new or not based on the presence of an id.
|
64
64
|
|
65
|
-
### Timeout
|
66
65
|
|
67
|
-
Sets the request time out.
|
data/lib/served/http_client.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
1
|
+
require 'addressable/template'
|
1
2
|
module Served
|
2
3
|
# Provides an interface between HTTParty and the models. Most of the crap in here is self explanatory
|
3
4
|
class HTTPClient
|
4
5
|
HEADERS = { 'Content-type' => 'application/json', 'Accept' => 'application/json' }
|
6
|
+
DEFAULT_TEMPLATE = '{resource}.json{?query*}'
|
5
7
|
|
6
8
|
def initialize(host)
|
7
|
-
|
9
|
+
unless host =~ /{.+}/
|
10
|
+
host = "#{host}/#{DEFAULT_TEMPLATE}"
|
11
|
+
end
|
12
|
+
@template = Addressable::Template.new(host)
|
8
13
|
end
|
9
14
|
|
10
15
|
def get(endpoint, params={})
|
11
|
-
HTTParty.get(
|
12
|
-
query: params,
|
16
|
+
HTTParty.get(@template.expand(query: params, resource: endpoint).to_s,
|
13
17
|
headers: HEADERS,
|
14
18
|
timeout: Served.config.timeout
|
15
19
|
)
|
16
20
|
end
|
17
21
|
|
18
22
|
def put(endpoint, body, params={})
|
19
|
-
HTTParty.put(
|
20
|
-
"#{@host}/#{endpoint}",
|
23
|
+
HTTParty.put(@template.expand(query: params, resource: endpoint).to_s,
|
21
24
|
body: body,
|
22
|
-
query: params,
|
23
25
|
headers: HEADERS,
|
24
26
|
timeout: Served.config.timeout
|
25
27
|
)
|
26
28
|
end
|
27
29
|
|
28
30
|
def post(endpoint, body, params={})
|
29
|
-
HTTParty.post(
|
30
|
-
"#{@host}/#{endpoint}",
|
31
|
+
HTTParty.post(@template.expand(query: params, resource: endpoint).to_s,
|
31
32
|
body: body,
|
32
|
-
query: params,
|
33
33
|
headers: HEADERS,
|
34
34
|
timeout: Served.config.timeout
|
35
35
|
)
|
data/lib/served/resource/base.rb
CHANGED
@@ -45,16 +45,16 @@ module Served
|
|
45
45
|
name.split('::').last.tableize
|
46
46
|
end
|
47
47
|
|
48
|
-
# @return [String] the configured host.
|
48
|
+
# @return [String] or [Hash] the configured host.
|
49
49
|
# @see Services::Configuration
|
50
|
-
def
|
50
|
+
def host_config
|
51
51
|
Served.config[:hosts][parent.name.underscore.split('/')[-1]]
|
52
52
|
end
|
53
53
|
|
54
54
|
# @return [Served::HTTPClient] The connection instance to the service host. Note this is not an active
|
55
55
|
# connection but a passive one.
|
56
56
|
def client
|
57
|
-
@connection ||= Served::HTTPClient.new(
|
57
|
+
@connection ||= Served::HTTPClient.new(host_config)
|
58
58
|
end
|
59
59
|
|
60
60
|
private
|
@@ -121,17 +121,17 @@ module Served
|
|
121
121
|
private
|
122
122
|
|
123
123
|
def get(params={})
|
124
|
-
handle_response(client.get("
|
124
|
+
handle_response(client.get("#{resource_name}/#{id}", params))
|
125
125
|
end
|
126
126
|
|
127
127
|
def put(params={})
|
128
128
|
body = to_json
|
129
|
-
handle_response(client.put("
|
129
|
+
handle_response(client.put("#{resource_name}/#{id}", body, params))
|
130
130
|
end
|
131
131
|
|
132
132
|
def post(params={})
|
133
133
|
body = to_json
|
134
|
-
handle_response(client.post("
|
134
|
+
handle_response(client.post("#{resource_name}", body, params))
|
135
135
|
end
|
136
136
|
|
137
137
|
def handle_response(response)
|
data/lib/served/version.rb
CHANGED
data/served.gemspec
CHANGED
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
|
22
22
|
spec.add_dependency 'httparty'
|
23
23
|
spec.add_dependency 'activesupport', '>= 3.2'
|
24
|
+
spec.add_dependency 'addressable', '>= 2.4.0'
|
24
25
|
|
25
26
|
spec.add_development_dependency 'bundler', '~> 1.10'
|
26
27
|
spec.add_development_dependency 'rake', '~> 10.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: served
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jarod Reid
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -38,6 +38,20 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: addressable
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 2.4.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.4.0
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: bundler
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|