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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5dd60630d96f26553e17ad85ac7a89e9610550dc
4
- data.tar.gz: 36b249489f5a0fd65d22bd2761135d95e690b369
3
+ metadata.gz: a2799190c2c330b60231086f0b754909102363e6
4
+ data.tar.gz: 66da9c71434eb4719fff15bda2f3018fd7b9f33f
5
5
  SHA512:
6
- metadata.gz: d2cc7a44c7c3b99fe51279f51474598a26db0092f5de6b6dee777c09c97ebc13a195d25072ec1c3e80fea0b68a66dd025e443c8b594935ea276f2ddf21bf41fb
7
- data.tar.gz: 04f91ebcfd40452f2dc493db694654ccd88b4125da2ec0fe365c97f796133833e4380141ad8e0537df72cce6164ce45d149df867bb0fdb0a63238fbd299d7d99
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
- TODO: Write usage instructions here
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
- http.request(Net::HTTP::Post.new(uri.path))
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, error_message if endpoint.nil?
42
+ raise Render::Component::Error.new, configuration_error_message(__method__, 'endpoint') if endpoint.nil?
27
43
  endpoint
28
44
  end
29
45
 
30
- def error_message
31
- 'To use `obtain_component` you must add default endpoint. e.g. Render::Component.configuration.endpoint='
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,3 +1,3 @@
1
1
  class Render::Component::Configuration
2
- attr_accessor :endpoint
2
+ attr_accessor :endpoint, :base_path
3
3
  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
@@ -1,5 +1,5 @@
1
1
  module Render
2
2
  module Component
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  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.1.1
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-18 00:00:00.000000000 Z
11
+ date: 2016-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler