A Ruby client for the Hypernova service
Add this line to your application’s Gemfile:
gem'hypernova'And then execute:
$ bundle
Or install it yourself as:
$ gem install hypernova
In Rails, create an initializer in config/initializers/hypernova.rb.
# Really basic configuration only consists of the host and the portHypernova.configuredo |config|
config.host="localhost"config.port=80endAdd an :around_filter to your controller so you can opt into Hypernova rendering of view partials.
# In my_controller.rbrequire'hypernova'classMyController < ApplicationControlleraround_filter:hypernova_render_supportendUse the following methods to render React components in your view/templates.
<%=
render_react_component(
'MyComponent.js',
:name => 'Person',
:color => 'Blue',
:shape => 'Triangle'
)
%>You can pass more configuration options to Hypernova.
Hypernova.configuredo |config|
config.http_adapter=:patron# Use any adapter supported by Faradayconfig.host="localhost"config.port=80config.open_timeout=0.1config.scheme=:https# Valid schemes include :http and :httpsconfig.timeout=0.6endIf you do not want to use Faraday, you can configure Hypernova Ruby to use an HTTP client that
responds to post and accepts a hash argument.
Hypernova.configuredo |config|
# Use your own HTTP client!config.http_client=SampleHTTPClient.newendYou can access a lower-level interface to exactly specify the parameters that are sent to the Hypernova service.
<% things.each |thing| %><li><%=hypernova_batch_render(:name=>'your/component/thing.bundle.js',:data=>thing)%></li><%end%>You can also use the batch interface if you want to create and submit batches yourself:
batch=Hypernova::Batch.new(service)# each job in a hypernova render batch is identified by a token# this allows retrieval of unordered jobstoken=batch.render(:name=>'some_bundle.bundle.js',:data=>{foo: 1,bar: 2})token2=batch.render(:name=>'some_bundle.bundle.js',:data=>{foo: 2,bar: 1})# now we can submit the batch job and await its results# this blocks, and takes a significant time in round trips, so try to only# use it once per request!result=batch.submit!# ok now we can access our rendered strings.foo1=result[token].html_safefoo2=result[token2].html_safeHypernova enables you to control and alter requests at different stages of the render lifecycle via a plugin system.
All methods on a plugin are optional, and they are listed in the order that they are called.
initializers/hypernova.rb:
# initializers/hypernova.rbrequire'hypernova'classHypernovaPlugin# get_view_data allows you to alter the data given to any individual# component being rendered.# component is the name of the component being rendered.# data is the data being given to the component.defget_view_data(component_name,data)phrase_hash=data[:phrases]data[:phrases].keys.eachdo |phrase_key|
phrase_hash[phrase_key]="test phrase"enddataend# prepare_request allows you to alter the request object in any way that you# need.# Unless manipulated by another plugin, request takes the shape:# { 'component_name.js': { :name => 'component_name.js', :data => {} } }defprepare_request(current_request,original_request)current_request.keys.eachdo |key|
phrase_hash=req[key][:data][:phrases]ifphrase_hash.present?phrase_hash.keys.eachdo |phrase_key|
phrase_hash[phrase_key]=phrase_hash[phrase_key].upcaseendendendcurrent_requestend# send_request? allows you to determine whether a request should continue# on to the hypernova server. Returning false prevents the request from# occurring, and results in the fallback html.defsend_request?(request)trueend# after_response gives you a chance to alter the response from hypernova.# This will be most useful for altering the resulting html field, and special# handling of any potential errors.# res is a Hash like { 'component_name.js': { html: String, err: Error? } }defafter_response(current_response,original_response)current_response.keys.eachdo |key|
hash=current_response[key]hash['html']='<div>hello</div>'endcurrent_responseend# NOTE: If an error happens in here, it won’t be caught.defon_error(error,jobs)puts"Oh no, error - #{error}, jobs - #{jobs}"endendHypernova.add_plugin!(HypernovaPlugin.new)After checking out the repo, run bin/setup to install dependencies. Then, run
bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To
release a new version, update the version number in version.rb, and then run
bundle exec rake release to create a git tag for the version, push git
commits and tags, and push the .gem file to
rubygems.org.
- Fork it ( https://github.com/airbnb/hypernova-ruby/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request