<img src=“https://codeclimate.com/github/warwickshire/remote_partial.png” /> <img src=“https://badge.fury.io/rb/remote_partial.png” alt=“Gem Version” />
Remote Partial allows partials to be created from remote content.
Add this to your Gemfile:
gem 'remote_partial'
Prior to version 0.7.0, RemotePartial depended upon Rails to determine some of the configuration options. This dependency has now been removed, and therefore some paths will have to be defined when RemotePartial is run outside of Rails (Standalone mode).
The root location needs to be defined. For example:
RemotePartial.root = 'some/path'
With this defined:
some/path/db/remote_partial --> location for yaml file storing state information some/path/remote_partials --> output path, where partials will be created
The root will be Rails.root, unless RemotePartial.root is explicitly defined.
Partials will be placed in the app/views folder. Paths relative to root will then be:
db/remote_partial --> location for yaml file storing state information app/views/remote_partials --> output path, where partials will be created
In a Rails environment, the Rails logger will be used, otherwise the default logging is STDOUT.
Logging output to a specific file can be configured like this:
RemotePartial.logger_file = 'log/remote_partial.log'
Note that the location will be relative to RemotePartial.root, and the ‘log’ folder will need to exist.
Running this command:
RemotePartial.define( url: 'http://www.ruby-lang.org/en/', name: 'ruby', criteria: '#intro', minimum_life: 3.hours )
will create a partial at:
app/views/remote_partials/_ruby.html.erb
The content of this partial will be grabbed from the page at the url, and will comprise the content defined by ‘#intro’ (that is, the content of the tag with an id=‘intro’). This content will not be updated for at least 3 hours.
If you define the remote partials in an initializer (for example config/initializers/remote_partial.rb), the remote partial will be updated each time the Rails app is started.
Nokogiri is used to extract content from within the target page. Criteria are passed to Nokogiri’s search method, and can be either xpath or css format. See:
nokogiri.org/tutorials/searching_a_xml_html_document.html
If no criteria are specified, the whole page will be retrieved.
A lambda can be passed into RemotePartial.define, and this will be called on the retrieved content before the partial file is generated.
Note that the lambda must be passed in as a string so it can be stored in a serialized format. RemotePartial will convert the string into a lambda when it is needed.
For example, to change all instances of ‘foo’ to ‘bar’ in the partial:
RemotePartial.define( url: 'http://somewhere.com', name: 'foo_bar', output_modifier: '{|content| content.gsub(/foo/, "bar")}' )
To output the content of the remote partial ‘ruby’ to a rails view add this:
<%= render 'remote_partials/ruby' %>
Alternatively, the content of the remote partial can be accessed via:
partial = RemotePartial::Partial.find('ruby') File.read(partial.output_file_name)
An update of the existing remote partials, can be triggered by running the following rake task:
rake remote_partial:update
Each time a remote partial is updated, its partial.stale_at is set as the current datetime plus the minimum life of that partial (defaults to 1 minute). If an update process is run before the stale_at time, that partial will not be updated.
So once the ‘ruby’ remote partial has been updated, it will be at least three hours before it will be updated again (unless forced).
These is also a rake task that will force all partials to update irrespective of the stale_at time:
rake remote_partial:force_update
RemotePartial can be modified to connect to the remote source via a proxy. To do this set the environment variable http_proxy to the proxy url:
ENV['http_proxy'] = "http://proxy.example.com:8080"
If remote partial is unable to retrieve remote content, the problem will be logged, and a retrieval will be tried again at the next update.
The current state of each defined remote partial is stored in a YAML file:
db/remote_partial/partial.yml
This project wobbles and uses MIT-LICENSE.