render-component 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -1
- data/lib/render/component/client.rb +23 -7
- data/lib/render/component/configuration.rb +1 -1
- data/lib/render/component/helper.rb +2 -2
- data/lib/render/component/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a2799190c2c330b60231086f0b754909102363e6
|
4
|
+
data.tar.gz: 66da9c71434eb4719fff15bda2f3018fd7b9f33f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30f78357b1a917df58e7955acdd7e1303bc4c4108fafea3fe8d45de740ef8c258b3378334158c22c517e785216592b19f947291c538800f11b3dc89c6b6c3134
|
7
|
+
data.tar.gz: 3095b514b09e2bcdbb22c1b52b39a76eb77e91957a3090bd13333e3ee740e99cb37afa2cdbcc6415dd774cf98561818cab4eedc6137c9fb7d0c167f39309f7d5
|
data/README.md
CHANGED
@@ -20,9 +20,22 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
$ gem install render-component
|
22
22
|
|
23
|
+
Add in your file environment the endpoint to fetch component.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Render::Component.configuration.endpoint='http://render.component.here'
|
27
|
+
```
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
Render::Component.configuration.base_path='http://your.app.base.path'
|
31
|
+
```
|
32
|
+
|
23
33
|
## Usage
|
34
|
+
To get component, use the helper `render_component`. For example:
|
24
35
|
|
25
|
-
|
36
|
+
```
|
37
|
+
<%= render_component('site-nav', '{"some": "attribute"}') %>
|
38
|
+
```
|
26
39
|
|
27
40
|
## Development
|
28
41
|
|
@@ -3,17 +3,21 @@ require 'net/http'
|
|
3
3
|
|
4
4
|
class Render::Component::Client
|
5
5
|
|
6
|
-
def obtain_component(component)
|
7
|
-
execute_request(component)
|
6
|
+
def obtain_component(component, attributes)
|
7
|
+
execute_request(component, attributes)
|
8
8
|
end
|
9
9
|
|
10
10
|
private
|
11
11
|
|
12
|
-
def execute_request(component)
|
12
|
+
def execute_request(component, attributes)
|
13
13
|
uri = URI("#{default_endpoint}/#{component}")
|
14
14
|
|
15
15
|
response = Net::HTTP.start(uri.host, uri.port) do |http|
|
16
|
-
|
16
|
+
request = Net::HTTP::Post.new(uri.path)
|
17
|
+
request.content_type = 'application/json'
|
18
|
+
request.body = apply_default_attributes(attributes)
|
19
|
+
|
20
|
+
http.request(request)
|
17
21
|
end
|
18
22
|
|
19
23
|
return response.body if response.code_type == Net::HTTPOK
|
@@ -21,13 +25,25 @@ class Render::Component::Client
|
|
21
25
|
raise StandardError.new, "Request Error. Uri: #{uri}, Response Code Type: #{response.code_type}, Response Code: #{response.code}"
|
22
26
|
end
|
23
27
|
|
28
|
+
def apply_default_attributes(attributes)
|
29
|
+
body = JSON.parse(attributes)
|
30
|
+
body['base_path'] = default_base_path
|
31
|
+
JSON.generate(body)
|
32
|
+
end
|
33
|
+
|
34
|
+
def default_base_path
|
35
|
+
base_path = Render::Component.configuration.base_path
|
36
|
+
raise Render::Component::Error.new, configuration_error_message(__method__, 'base_path') if base_path.nil?
|
37
|
+
base_path
|
38
|
+
end
|
39
|
+
|
24
40
|
def default_endpoint
|
25
41
|
endpoint = Render::Component.configuration.endpoint
|
26
|
-
raise Render::Component::Error.new,
|
42
|
+
raise Render::Component::Error.new, configuration_error_message(__method__, 'endpoint') if endpoint.nil?
|
27
43
|
endpoint
|
28
44
|
end
|
29
45
|
|
30
|
-
def
|
31
|
-
|
46
|
+
def configuration_error_message(method, attribute)
|
47
|
+
"To use `#{method}` you must add default #{attribute}. e.g. Render::Component.configuration.#{attribute}="
|
32
48
|
end
|
33
49
|
end
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module Render::Component
|
2
2
|
module Helper
|
3
3
|
|
4
|
-
def render_component(component)
|
5
|
-
Client.new.obtain_component(component)
|
4
|
+
def render_component(component, attributes = {})
|
5
|
+
Client.new.obtain_component(component, attributes)
|
6
6
|
end
|
7
7
|
module_function :render_component
|
8
8
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: render-component
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lucas Gomide
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|