From 74c699990aff3d8dd813f5155d77a5b5cc760b82 Mon Sep 17 00:00:00 2001 From: Igor Fedoronchuk Date: Tue, 4 Mar 2025 19:13:40 +0100 Subject: [PATCH 1/2] fix README typos --- README.md | 126 +++++++++++++++++++++++++++++------------------------- 1 file changed, 68 insertions(+), 58 deletions(-) diff --git a/README.md b/README.md index 176245b..8a2b193 100644 --- a/README.md +++ b/README.md @@ -1,23 +1,23 @@ -## 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 ) +## ActiveResource-Response +This gem adds the ability to access the HTTP response (`Net::HTTPResponse`) object from the result (either a single object or a collection) of an ActiveResource call (methods: `find`, `all`, `first`, `last`, `get`). -#### 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. +#### Why Can It Be Used? +This functionality can be used to easily implement pagination in a REST API, so that an ActiveResource client can navigate paginated results. -#### How to use? -Add dependency to your Gemfile +#### How to Use? +Add the dependency to your `Gemfile`: ```ruby gem "activeresource-response" ``` -Just open your ActiveResource class and add +Open your ActiveResource class and add: ```ruby add_response_method :your_method_name ``` -You can add method to ActiveResource::Base to use it in all subclasses +You can add the method to `ActiveResource::Base` to use it in all subclasses: ```ruby class ActiveResource::Base @@ -25,7 +25,7 @@ class ActiveResource::Base end ``` -You can remove method from ActiveResource subclass +You can remove the method from an ActiveResource subclass: ```ruby class Order < ActiveResource::Base @@ -33,25 +33,25 @@ class Order < ActiveResource::Base end ``` -## Full example of usage with kaminari gem +## Full Example of Usage with the Kaminari Gem -ActiveResource Class +ActiveResource Class: ```ruby 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 + add_response_method :http_response # Our new method for returned objects end ``` -```ruby -Server Side +Server Side: +```ruby class OrdersController < ApplicationController def index - @orders = Order.page(params[:page]).per(params[:per_page] || 10) #default 10 per page + @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 @@ -60,13 +60,13 @@ class OrdersController < ApplicationController end ``` -Client Side +Client Side: ```ruby class OrdersController < ApplicationController def index orders = Order.all(params: params) - @orders = Kaminari::PaginatableArray.new(orders,{ + @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 @@ -75,92 +75,102 @@ class OrdersController < ApplicationController end ``` -### will_paginate compatibility -will_paginate has a little different API, so to populate headers +### Will_paginate Compatibility +`will_paginate` has a slightly different API, so to populate the headers: ```ruby 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) - ```ruby - 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| +On the API client side, you might also use `will_paginate`. In that case, you can just require `will_paginate/array` (e.g., in an initializer): + +```ruby +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 +### Every Time an HTTP Connection is Invoked +The ActiveResource connection object stores the HTTP response. You can access it with the `http_response` method. + +Example: + ```ruby class Order < ActiveResource::Base self.site = 'http://0.0.0.0:3000/' self.element_name = "order" - add_response_method :my_response # our new method + add_response_method :my_response # Our new method end orders = Order.all first_order = Order.find(1) -#see Net::HTTPResponse#[] method +# 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 +# => "260" +# Connection also always has the last HTTP response object. To access it, use the 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 -You can access response from result of custom get method -Example +``` + +### Custom `get` Method +You can access the response from the result of a custom `get` method. + +Example: + ```ruby class Country < ActiveResource::Base self.site = 'http://0.0.0.0:3000/' - add_response_method :http # our new method + add_response_method :http # Our new method end + cities = Country.find(1).get(:cities) -cities.http #method from Country class is available -``` +cities.http # Method from the Country class is available +``` + +### Headers and Cookies Methods +You can get cookies and headers from the response. + +Example: -### Headers and cookies methods -You can get cookies and headers from response -Example ```ruby class Country < ActiveResource::Base self.site = 'http://0.0.0.0:3000/' - add_response_method :my_response # our new method + add_response_method :my_response # Our new method end + countries = Country.all countries.my_response.headers -# collection with symbolized keys => {:content_type=>["application/json; charset=utf-8"], :x_ua_compatible=>["IE=Edge"], ..., :set_cookie=>["bar=foo; path=/", "foo=bar; path=/"]} +# Collection with symbolized keys: +# {:content_type=>["application/json; charset=utf-8"], :x_ua_compatible=>["IE=Edge"], ..., :set_cookie=>["bar=foo; path=/", "foo=bar; path=/"]} countries.my_response.cookies - # => {"bar"=>"foo", "foo"=>"bar"} - ``` - -### Note About Http response -http response is object of ```Net::HTTPOK```, ```Net::HTTPClientError``` or one of other subclasses -of Net::HTTPResponse class. For more information see documentation http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPResponse.html +# => {"bar"=>"foo", "foo"=>"bar"} +``` + +### Note About HTTP Response +The HTTP response is an object of `Net::HTTPOK`, `Net::HTTPClientError`, or one of the other subclasses of the `Net::HTTPResponse` class. For more information, see the documentation: [Net::HTTPResponse](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTPResponse.html). ### Testing with ActiveResource::HttpMock -Add this line to your test to patch http_mock +Add this line to your test to patch `http_mock`: ```ruby - require "active_resource_response/http_mock" +require "active_resource_response/http_mock" ``` ### Contributing - Fork it - Create your feature branch (git checkout -b my-new-feature) - Commit your changes (git commit -am 'Add some feature') - Push to the branch (git push origin my-new-feature) - Create new Pull Request +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Add some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create a new Pull Request - -#### Please, feel free to contact me if you have any questions +#### Please feel free to contact me if you have any questions: fedoronchuk(at)gmail.com + From a347bad2202372a9363fe47caa75e6f4fa8085ee Mon Sep 17 00:00:00 2001 From: Igor Fedoronchuk Date: Tue, 4 Mar 2025 19:14:24 +0100 Subject: [PATCH 2/2] fixed gemspec homepage --- activeresource-response.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/activeresource-response.gemspec b/activeresource-response.gemspec index e34372d..657ad72 100644 --- a/activeresource-response.gemspec +++ b/activeresource-response.gemspec @@ -8,7 +8,7 @@ Gem::Specification.new do |s| s.version = ActiveResourceResponse::Version::VERSION s.authors = ["Igor Fedoronchuk"] s.email = ["fedoronchuk@gmail.com"] - s.homepage = "http://fivell.github.com/activeresource-response/" + s.homepage = "https://github.com/didww/activeresource-response" s.summary = %q{activeresource extension} s.description = %q{This gem adds possibility to access http response object from result of ActiveResource::Base find method } s.license = 'MIT'