opencomponents 0.1.0 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ad282648e755c0c28348bb1b2c243892bc4c100
4
- data.tar.gz: bbee3ca89a7c5b264a7e8b17872efed62f711989
3
+ metadata.gz: bad9832ecad1d26ca9511e032e4c85d948f17f18
4
+ data.tar.gz: cc277e8e5182192778dec331c80050d3d8dd4c5d
5
5
  SHA512:
6
- metadata.gz: 0bbfa189e576fd2e24ac401f159adf483d78e67e16a57ba44e87c598e1e493e7d0d87abdb5e121ca58d67adf5b358274c0bcbb4106e48246ec5dbf0c503a77cb
7
- data.tar.gz: 03b8c7878cb230499a3f6c5a4e0f1c79684eff6c222c8c76525af770f2267b749eca7f2efef535517c18adb87c27bc334d9318464928342e15c67a7ccd7aed94
6
+ metadata.gz: f33a1d25e888b0d4ad0a2e158f2b6eebfb93bb2959d3c2965dd8577e1663766dc758fb202d6ba9884f9ec2fe4187bc773a1e6db9e9ae10f62763684150947f37
7
+ data.tar.gz: 55f5b8e3d0c2aa0c932951a7ee06539c2d60ca55cba65f0d9d9f9021a8446824c919068e911ca4e3ce941b15a08427e52c0105b96caba31b2734d9f9f97a5699
data/README.md CHANGED
@@ -1,20 +1,28 @@
1
1
  # opencomponents
2
- [![Build Status](https://travis-ci.org/opentable/ruby-oc.svg?branch=master)](https://travis-ci.org/opentable/ruby-oc)
3
- [![Coverage Status](https://coveralls.io/repos/opentable/ruby-oc/badge.svg)](https://coveralls.io/r/opentable/ruby-oc)
2
+ [![Build Status](https://travis-ci.org/opentable/ruby-oc.svg?branch=master)][1]
3
+ [![Coverage Status](https://coveralls.io/repos/opentable/ruby-oc/badge.svg)][2]
4
+ [![Code Climate](https://codeclimate.com/github/opentable/ruby-oc/badges/gpa.svg)][3]
5
+ [![Gem Version](https://badge.fury.io/rb/opencomponents.svg)][4]
6
+ [![Dependency Status](https://gemnasium.com/opentable/ruby-oc.svg)][5]
4
7
 
5
- [OpenComponents][1] for Ruby
8
+ [1]:https://travis-ci.org/opentable/ruby-oc
9
+ [2]:https://coveralls.io/r/opentable/ruby-oc
10
+ [3]:https://codeclimate.com/github/opentable/ruby-oc
11
+ [4]:http://badge.fury.io/rb/opencomponents
12
+ [5]:https://gemnasium.com/opentable/ruby-oc
6
13
 
7
- [1]:https://github.com/opentable/oc
14
+ [OpenComponents][6] for Ruby
15
+
16
+ [6]:https://github.com/opentable/oc
8
17
 
9
18
  ## Important
10
19
  This gem is still under heavy development and the API is likely to change at any
11
20
  time.
12
21
 
13
22
  ## Getting Started
14
- Add the gem to your Gemfile and run `bundle install` (prerelease versions aren't
15
- on RubyGems):
23
+ Add the gem to your Gemfile and run `bundle install`:
16
24
  ```ruby
17
- gem 'opencomponents', github: 'opentable/ruby-oc'
25
+ gem 'opencomponents', '~> 0.1.0'
18
26
  ```
19
27
 
20
28
  By default, the gem will attempt to use a component registry located at
@@ -25,14 +33,48 @@ it with:
25
33
  OpenComponents.configure { |config| config.registry = 'http://some.other.host' }
26
34
  ```
27
35
 
36
+ ## Getting Rendered Components
37
+ To request an OpenComponent with its rendered HTML, you can create and load a new
38
+ `RenderedComponent` object.
39
+ ```ruby
40
+ component = OpenComponents::RenderedComponent.new('my-awesome-component')
41
+ component.load
42
+ ```
43
+
44
+ Optionally, you can also specify parameters, the component version, and
45
+ additional HTTP headers to request:
46
+ ```ruby
47
+ component = OpenComponents::RenderedComponent.new(
48
+ 'my-awesome-component',
49
+ params: {name: 'Kate'},
50
+ version: '1.0.2',
51
+ headers: {accept_language: 'emoji'}
52
+ )
53
+ ```
54
+
55
+ ## Getting Pre-rendered Components
56
+ If you'd like to perform the component rendering yourself, you can request a
57
+ pre-rendered component from a registry using a `PrerenderedComponent` object.
58
+ ```ruby
59
+ component = OpenComponents::PrerenderedComponent.new('my-awesome-component')
60
+ component.load
61
+ ```
62
+
63
+ You can use the same optional `params`, `version`, and `headers` arguments as
64
+ `RenderedComponent`s.
65
+
66
+ **Note**: `PrerenderedComponent`s will only fetch component data for you - they
67
+ do not provide an interface for rendering them. At the moment, it's up to you to
68
+ determine the best way to render the template.
69
+
28
70
  ## Integrations
29
71
  Individual integrations for rendering components in Rails and Sinatra are
30
72
  available.
31
- * [opencomponents-rails][2]
32
- * [sinatra-opencomponents][3]
73
+ * [opencomponents-rails][7]
74
+ * [sinatra-opencomponents][8]
33
75
 
34
- [2]:https://github.com/opentable/opencomponents-rails
35
- [3]:https://github.com/opentable/sinatra-opencomponents
76
+ [7]:https://github.com/opentable/opencomponents-rails
77
+ [8]:https://github.com/opentable/sinatra-opencomponents
36
78
 
37
79
  ## Contributing
38
80
  Would be rad. Open a PR or Issue if you have an idea for improvements.
@@ -5,17 +5,41 @@ require 'opencomponents/renderer'
5
5
  require 'opencomponents/version'
6
6
 
7
7
  module OpenComponents
8
+ # Public: Default OC registry URL (http://localhost:3030).
8
9
  DEFAULT_REGISTRY = 'http://localhost:3030'
9
10
 
11
+ # Internal: Custom exception class to raise in the event a component cannot be
12
+ # found in the registry.
10
13
  ComponentNotFound = Class.new(RuntimeError)
11
14
 
15
+ # Internal: Stores configuration data.
16
+ #
17
+ # registry - String for the registry host.
12
18
  Configuration = Struct.new(:registry)
13
- Template = Struct.new(:src, :type, :key)
14
19
 
20
+ # Internal: Wrapper object for pre-rendered OC templates.
21
+ #
22
+ # src - String for the template URL.
23
+ # type - String for template engine type.
24
+ # key - String for template key.
25
+ Template = Struct.new(:src, :type, :key)
26
+
27
+ # Public: Getter for OC Configuration.
28
+ #
29
+ # Returns the Configuration if set, a default Configuration if not set.
15
30
  def self.config
16
31
  @@_config ||= Configuration.new(DEFAULT_REGISTRY)
17
32
  end
18
33
 
34
+ # Public: Setter for Configuration.
35
+ #
36
+ # Examples
37
+ #
38
+ # OpenComponents.configure do |oc|
39
+ # oc.registry = 'http://my-awesome-oc-registry.com'
40
+ # end
41
+ #
42
+ # Returns the Configuration.
19
43
  def self.configure
20
44
  yield self.config
21
45
  end
@@ -1,20 +1,67 @@
1
1
  require 'rest-client'
2
2
 
3
3
  module OpenComponents
4
+ # Wrapper object for a component fetched from an OC registry.
4
5
  class Component
5
- attr_reader :name, :version, :params, :href, :registry_version,
6
- :type, :render_mode
6
+ # Public: Gets/sets the String name of the component.
7
+ attr_accessor :name
7
8
 
8
- def initialize(name, params = {}, version = '')
9
+ # Public: Gets/sets the desired String version of the component.
10
+ attr_accessor :version
11
+
12
+ # Public: Gets/sets a Hash of params to send in the component request.
13
+ attr_accessor :params
14
+
15
+ # Public: Gets/sets a Hash of headers to send in the component request.
16
+ attr_accessor :headers
17
+
18
+ # Public: Returns the String URL of the requested component.
19
+ attr_reader :href
20
+
21
+ # Public: Returns the String version of the component as served by the
22
+ # registry.
23
+ attr_reader :registry_version
24
+
25
+ # Public: Returns the String type of the component as served by the
26
+ # registry.
27
+ attr_reader :type
28
+
29
+ # Public: Returns the String render mode of the component as served by the
30
+ # registry. Generally either `rendered` or `pre-rendered`.
31
+ attr_reader :render_mode
32
+
33
+ # Public: Initializes a new Component subclass.
34
+ #
35
+ # name - The String name of the component to request.
36
+ # opts - A Hash of options to use when requesting the component
37
+ # (default: {}).
38
+ # :params - A Hash of parameters to send in the component request
39
+ # (optional, default: {}).
40
+ # :version - The String version of the component to request
41
+ # (optional, default: '').
42
+ # :headers - A Hash of HTTP request headers to include in the
43
+ # component request (optional, default: {}).
44
+ def initialize(name, opts = {})
9
45
  @name = name
10
- @params = params
11
- @version = version
46
+ @params = opts[:params] || {}
47
+ @version = opts[:version] || ''
48
+ @headers = opts[:headers] || {}
12
49
  end
13
50
 
51
+ # Public: Returns the String value of `requestVersion` from a component
52
+ # response, `nil` if not present.
14
53
  def request_version
15
54
  @request_version == '' ? nil : @request_version
16
55
  end
17
56
 
57
+ # Public: Resets all component attributes from a registry response to `nil`.
58
+ #
59
+ # Examples
60
+ #
61
+ # component.flush!
62
+ # # => #<OpenComponents::RenderedComponent: ... >
63
+ #
64
+ # Returns the reset Component.
18
65
  def flush!
19
66
  flush_variables_whitelist.each do |var|
20
67
  instance_variable_set(var, nil)
@@ -23,6 +70,15 @@ module OpenComponents
23
70
  self
24
71
  end
25
72
 
73
+ # Public: Resets all component attributes and reloads them from a registry
74
+ # response.
75
+ #
76
+ # Examples
77
+ #
78
+ # component.reload!
79
+ # # => #<OpenComponents::RenderedComponent: ... >
80
+ #
81
+ # Returns the reloaded Component.
26
82
  def reload!
27
83
  flush!
28
84
  load
@@ -30,6 +86,11 @@ module OpenComponents
30
86
 
31
87
  protected
32
88
 
89
+ # Internal: Executes a component request and sets attributes common to all
90
+ # component render modes. Public-facing `#load` methods exist on Component
91
+ # subclasses.
92
+ #
93
+ # Returns the Component.
33
94
  def load
34
95
  @href = response_data['href']
35
96
  @registry_version = response_data['version']
@@ -40,32 +101,47 @@ module OpenComponents
40
101
  self
41
102
  end
42
103
 
104
+ # Internal: Builds the URL to send a component request to.
105
+ #
106
+ # Returns the String URL to request a component from.
43
107
  def url
44
108
  File.join(OpenComponents.config.registry, name, version)
45
109
  end
46
110
 
111
+ # Internal: Executes a component request against the configured registry.
112
+ #
113
+ # Returns a response body String.
114
+ # Raises OpenComponents::ComponentNotFound if the registry responds with a
115
+ # 404.
47
116
  def response
117
+ request_headers = headers.merge(params: params)
118
+
48
119
  RestClient::Request.execute(
49
120
  method: :get,
50
121
  url: url,
51
122
  timeout: 10,
52
- headers: {
53
- accept: accept_header,
54
- params: params,
55
- }
123
+ headers: request_headers
56
124
  )
57
125
  rescue RestClient::ResourceNotFound => e
58
126
  fail ComponentNotFound, e.message
59
127
  end
60
128
 
129
+ # Internal: Helper method for converting and memoizing registry response
130
+ # data.
131
+ #
132
+ # Returns a Hash of registry response data.
61
133
  def response_data
62
134
  @_response_data ||= JSON.parse(response)
63
135
  end
64
136
 
65
137
  private
66
138
 
139
+ # Internal: Whitelists instance variable names allowed to be reset when
140
+ # calling `#flush!`.
141
+ #
142
+ # Returns an Array of instance variable Symbols allowed to be reset.
67
143
  def flush_variables_whitelist
68
- instance_variables - [:@name, :@version, :@params]
144
+ instance_variables - [:@name, :@version, :@params, :@headers]
69
145
  end
70
146
  end
71
147
  end
@@ -1,7 +1,42 @@
1
1
  module OpenComponents
2
+ # Wrapper object for components using the `pre-rendered` rendering mode.
2
3
  class PrerenderedComponent < Component
3
- attr_reader :data, :template
4
+ # Internal: Default HTTP headers to send when requesting a component.
5
+ DEFAULT_HEADERS = {accept: 'application/vnd.oc.prerendered+json'}
4
6
 
7
+ # Public: Returns a Hash of data to use when rendering the component.
8
+ attr_reader :data
9
+
10
+ # Public: Returns a Template with data for rendering the raw component.
11
+ attr_reader :template
12
+
13
+ # Public: Initializes a new PrerenderedComponent.
14
+ #
15
+ # name - The String name of the component to request.
16
+ # opts - A Hash of options to use when requesting the component
17
+ # (default: {}).
18
+ # :params - A Hash of parameters to send in the component request
19
+ # (optional, default: {}).
20
+ # :version - The String version of the component to request
21
+ # (optional, default: nil).
22
+ # :headers - A Hash of HTTP request headers to include in the
23
+ # component request (optional, default: DEFAULT_HEADERS).
24
+ def initialize(name, opts = {})
25
+ super(name, opts)
26
+
27
+ @headers.merge!(DEFAULT_HEADERS)
28
+ end
29
+
30
+ # Public: Executes a request for the Component against the configured
31
+ # registry and sets the component attributes.
32
+ #
33
+ # Examples
34
+ #
35
+ # component = OpenComponents::RenderedComponent.new('my-component')
36
+ # component.load
37
+ # # => #<OpenComponents::RenderedComponent: ... >
38
+ #
39
+ # Returns the Component with attributes set.
5
40
  def load
6
41
  template_data = response_data['template']
7
42
 
@@ -12,11 +47,5 @@ module OpenComponents
12
47
 
13
48
  super
14
49
  end
15
-
16
- private
17
-
18
- def accept_header
19
- 'application/vnd.oc.prerendered+json'
20
- end
21
50
  end
22
51
  end
@@ -1,17 +1,23 @@
1
1
  module OpenComponents
2
+ # Wrapper object for components using the `rendered` rendering mode.
2
3
  class RenderedComponent < Component
4
+ # Public: Returns the HTML String for the rendered component.
3
5
  attr_reader :html
4
6
 
7
+ # Public: Executes a request for the Component against the configured
8
+ # registry and sets the component attributes.
9
+ #
10
+ # Examples
11
+ #
12
+ # component = OpenComponents::RenderedComponent.new('my-component')
13
+ # component.load
14
+ # # => #<OpenComponents::RenderedComponent: ... >
15
+ #
16
+ # Returns the Component with attributes set.
5
17
  def load
6
18
  @html = response_data['html']
7
19
 
8
20
  super
9
21
  end
10
-
11
- private
12
-
13
- def accept_header
14
- nil
15
- end
16
22
  end
17
23
  end
@@ -1,7 +1,20 @@
1
1
  module OpenComponents
2
+ # Provides rendering helper methods for components.
2
3
  module Renderer
3
- def render_component(component, params = {}, version = '')
4
- OpenComponents::RenderedComponent.new(component, params, version)
4
+ # Public: Builds a new RenderedComponent, executes a request for it against
5
+ # the configured registry, and returns the rendered component HTML.
6
+ #
7
+ # component - The String name of the component to render.
8
+ # opts - A Hash of options to use when requesting the component
9
+ # (default: {}).
10
+ # :params - A Hash of parameters to send in the component request
11
+ # (optional, default: {}).
12
+ # :version - The String version of the component to request
13
+ # (optional, default: nil).
14
+ # :headers - A Hash of HTTP request headers to include in the
15
+ # component request (optional, default: {}).
16
+ def render_component(component, opts = {})
17
+ OpenComponents::RenderedComponent.new(component, opts)
5
18
  .load
6
19
  .html
7
20
  end
@@ -1,3 +1,3 @@
1
1
  module OpenComponents
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0' # :nodoc:
3
3
  end
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ['lib']
21
21
  spec.summary = spec.description
22
22
  spec.version = OpenComponents::VERSION
23
+ spec.rdoc_options = ['--markup=tomdoc']
23
24
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opencomponents
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Bealmear
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-02 00:00:00.000000000 Z
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -59,7 +59,8 @@ licenses:
59
59
  - MIT
60
60
  metadata: {}
61
61
  post_install_message:
62
- rdoc_options: []
62
+ rdoc_options:
63
+ - "--markup=tomdoc"
63
64
  require_paths:
64
65
  - lib
65
66
  required_ruby_version: !ruby/object:Gem::Requirement