Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Take a look at <a href="http://api.rubyonrails.org/classes/ActiveResource/Base.html" rel="nofollow">ActiveResource</a>.</p> <p>You'll end up with models, but most will only take up one line.</p> <p>Say your api service has a url like: <a href="http://api.fart.com/products.json" rel="nofollow">http://api.fart.com/products.json</a> which spits out a list of products. In your rails app that will be consuming this data, create a <code>Product</code> model inheriting from <code>ActiveResource</code>. First, set your api url for activeresource.</p> <pre><code>class ActiveResource::Base self.site = "http://api.fart.com" end </code></pre> <p>Next, define a Product class.</p> <pre><code>class Product &lt; ActiveResource::Base; end </code></pre> <p>Now, in your controllers, you can do:</p> <pre><code>Product.all </code></pre> <p>And it should hit "http://api.fart.com/products.json" and return the good stuff.</p> <p>Obviously this is a quickly contrived example, but I have recently used it for a similar situation and it worked great.</p> <p>///////////////</p> <p>see <a href="https://github.com/davidglivar/stack_rest_api" rel="nofollow">example applications</a> for basic concept.</p> <p>///////////////</p> <p>Per your questions and your pull requests on github I understand what you are trying to do now. <code>ActiveResource</code> does not come with a <code>where</code> method - however, it's <code>find</code> method has an options argument that supports a params hash. </p> <p>Rails controller with query params</p> <pre><code>def index @products = Product.find :all, params: { country: "Brazil", price: 500 } end </code></pre> <p>This will hit our Sinatra route at api.fart.com/products.json?country=Brazil&amp;price=500</p> <pre><code># Assume we are using ActiveRecord/DataMapper or the like get "/products.json" do @products = Product.where country: params[:country], price: params[:price].to_i json products: @products end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload