<commit_before>= Activeresource-response 
This gem adds possibility to access http response (Net::HTTPResponse) object from result (single object or collection) of activeresource call (methods : find, all, first, last, get )

{<img src="https://secure.travis-ci.org/Fivell/activeresource-response.png" />}[http://travis-ci.org/Fivell/activeresource-response]  
{<img src="https://gemnasium.com/Fivell/activeresource-response.png" />}[https://gemnasium.com/Fivell/activeresource-response]  
{<img src="https://coveralls.io/repos/Fivell/activeresource-response/badge.png?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/Fivell/activeresource-response?branch=master]

== Why It can be used?
Such functionallity can be used for easily implementing pagination in a REST API so that an ActiveResource client can navigate paginated results.

== How to use?
Add dependency to your Gemfile

  gem "activeresource-response"

Just open your ActiveResource class  and add 
 
  add_response_method :your_method_name

You can add method to ActiveResource::Base to use it in all subclasses

  class ActiveResource::Base
    add_response_method :my_response  
  end


You can remove method from ActiveResource subclass

  class Order < ActiveResource::Base
    remove_response_method  
  end


== Full example of usage with kaminari gem

Rest Client

  class Order < ActiveResource::Base
    self.format = :json
    self.site = 'http://0.0.0.0:3000/'
    self.element_name = "order" 
    add_response_method :http_response  # our new method for returned objects 
  end

Server Side

  class OrdersController < ApplicationController
    def index
      @orders = Order.page(params[:page]).per(params[:per_page] || 10) #default 10 per page
      response.headers["X-total"] = @orders.total_count.to_s
      response.headers["X-offset"] = @orders.offset_value.to_s
      response.headers["X-limit"] = @orders.limit_value.to_s
      respond_with(@orders)
    end
  end

Client Side

  class OrdersController < ApplicationController
    def index
      orders = Order.all(:params=>params)     
      @orders = Kaminari::PaginatableArray.new(
        orders,{
               :limit => orders.http_response['X-limit'].to_i,
               :offset =>orders.http_response['X-offset'].to_i,
               :total_count => orders.http_response['X-total'].to_i
        }) 
    end
  end


== Will_paginate
Will_paginate has a little different API, so to populate headers 

  response.headers["X-total"] = @orders.total_entries.to_s
  response.headers["X-offset"] = @orders.offset.to_s
  response.headers["X-limit"] = @orders.per_page.to_s
  
On the API client side you might also use will_paginate, in that case you can just require will_paginate/array (in initializer for example)
and paginate array directly by calling .paginate(options) with proper total/page/per_page values – see 
{source}[https://github.com/mislav/will_paginate/blob/master/lib/will_paginate/array.rb].

 
== Every time when http connection invoked ActiveResource connection object  stores http response. You can access it with http_response method. 
Example

  class Order < ActiveResource::Base
    self.site = 'http://0.0.0.0:3000/'
    self.element_name = "order" 
    add_response_method :my_response  # our new method 
  end

  orders = Order.all
  first_order = Order.find(1) 
  #see Net::HTTPResponse#[] method
  orders.my_response['content-length'] 
  # => "3831" 
  first_order.my_response['content-length'] 
  #=> "260"
  #connection also always has last http response object , to access it use http_response method
  Order.connection.http_response.to_hash
  # => {"content-type"=>["application/json; charset=utf-8"], "x-ua-compatible"=>["IE=Edge"], "etag"=>["\"573cabd02b2f1f90405f7f4f77995fab\""], "cache-control"=>["max-age=0, private, must-revalidate"], "x-request-id"=>["2911c13a0c781044c474450ed789613d"], "x-runtime"=>["0.071018"], "content-length"=>["260"], "server"=>["WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)"], "date"=>["Sun, 19 Feb 2012 10:21:29 GMT"], "connection"=>["close"]} 
  
== Custom get method<commit_msg>Update README.rdoc<commit_after>= Activeresource-response 
This gem adds possibility to access http response (Net::HTTPResponse) object from result (single object or collection) of activeresource call (methods : find, all, first, last, get )

{<img src="https://secure.travis-ci.org/Fivell/activeresource-response.png" />}[http://travis-ci.org/Fivell/activeresource-response]  
{<img src="https://gemnasium.com/Fivell/activeresource-response.png" />}[https://gemnasium.com/Fivell/activeresource-response]  
{<img src="https://coveralls.io/repos/Fivell/activeresource-response/badge.png?branch=master" alt="Coverage Status" />}[https://coveralls.io/r/Fivell/activeresource-response?branch=master]

== Why It can be used?
Such functionallity can be used for easily implementing pagination in a REST API so that an ActiveResource client can navigate paginated results.

== How to use?
Add dependency to your Gemfile

  gem "activeresource-response"

Just open your ActiveResource class  and add 
 
  add_response_method :your_method_name

You can add method to ActiveResource::Base to use it in all subclasses

  class ActiveResource::Base
    add_response_method :my_response  
  end


You can remove method from ActiveResource subclass

  class Order < ActiveResource::Base
    remove_response_method  
  end


== Full example of usage with kaminari gem

Rest Client

  class Order < ActiveResource::Base
    self.format = :json
    self.site = 'http://0.0.0.0:3000/'
    self.element_name = "order" 
    add_response_method :http_response  # our new method for returned objects 
  end

Server Side

  class OrdersController < ApplicationController
    def index
      @orders = Order.page(params[:page]).per(params[:per_page] || 10) #default 10 per page
      response.headers["X-total"] = @orders.total_count.to_s
      response.headers["X-offset"] = @orders.offset_value.to_s
      response.headers["X-limit"] = @orders.limit_value.to_s
      respond_with(@orders)
    end
  end

Client Side

  class OrdersController < ApplicationController
    def index
      orders = Order.all(:params=>params)     
      @orders = Kaminari::PaginatableArray.new(
        orders,{
               :limit => orders.http_response['X-limit'].to_i,
               :offset =>orders.http_response['X-offset'].to_i,
               :total_count => orders.http_response['X-total'].to_i
        }) 
    end
  end


== Will_paginate
Will_paginate has a little different API, so to populate headers 

  response.headers["X-total"] = @orders.total_entries.to_s
  response.headers["X-offset"] = @orders.offset.to_s
  response.headers["X-limit"] = @orders.per_page.to_s
  
On the API client side you might also use will_paginate, in that case you can just require will_paginate/array (in initializer for example)
 
 orders = Order.all(:params=>params)     
 @orders = WillPaginate::Collection.create(params[:page] || 1, params[:per_page] || 10, orders.http_response['X-total'].to_i) do |pager|
     pager.replace orders
 end


== Every time when http connection invoked ActiveResource connection object  stores http response. You can access it with http_response method. 
Example

  class Order < ActiveResource::Base
    self.site = 'http://0.0.0.0:3000/'
    self.element_name = "order" 
    add_response_method :my_response  # our new method 
  end

  orders = Order.all
  first_order = Order.find(1) 
  #see Net::HTTPResponse#[] method
  orders.my_response['content-length'] 
  # => "3831" 
  first_order.my_response['content-length'] 
  #=> "260"
  #connection also always has last http response object , to access it use http_response method
  Order.connection.http_response.to_hash
  # => {"content-type"=>["application/json; charset=utf-8"], "x-ua-compatible"=>["IE=Edge"], "etag"=>["\"573cabd02b2f1f90405f7f4f77995fab\""], "cache-control"=>["max-age=0, private, must-revalidate"], "x-request-id"=>["2911c13a0c781044c474450ed789613d"], "x-runtime"=>["0.071018"], "content-length"=>["260"], "server"=>["WEBrick/1.3.1 (Ruby/1.9.2/2011-02-18)"], "date"=>["Sun, 19 Feb 2012 10:21:29 GMT"], "connection"=>["close"]} 
  
== Custom get method
{'fraction_non_alphanumeric': 0.12570036540803897, 'fraction_numerical': 0.027161997563946407, 'mean_word_length': 4.177175283732661, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 44, 'https://': 11, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3246452', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2547, 'n_words': 633}