Git Product home page Git Product logo

remote_partial's Introduction

<img src=“https://codeclimate.com/github/warwickshire/remote_partial.png” /> <img src=“https://badge.fury.io/rb/remote_partial.png” alt=“Gem Version” />

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

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

Connection via a proxy

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"

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.

remote_partial's People

Contributors

reggieb avatar tonyheadford avatar

Watchers

 avatar James Cloos avatar Chris Jones avatar

Forkers

tonyheadford

remote_partial's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.