Skip to content

tonyheadford/remote_partial

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

60 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

<img src=“https://codeclimate.com/github/warwickshire/remote_partial.png” />

RemotePartial

Remote Partial allows partials to be created from remote content.

Installation

Add this to your Gemfile:

gem 'remote_partial'

Configuration

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).

Standalone

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

Within a Rails environment

The root is defined as Rails.root, and partials will be placed in the app/views folder. Paths relative to Rails.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

Logging

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.

Defining a remote partial

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.

In Rails

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.

Criteria

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.

Output modifier

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")}'
)

Adding remote partial content to a page

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)

Updating the content

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

Problems grabbing content

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.

Persistence

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.

About

Create partials from remote assets

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 99.7%
  • Shell 0.3%