Easy access to http resources

When you want to access some http resource, you have plenty of solutions, such as:

But they tend to be too big to use for simple cases. All you need is probably simple wrapper around http/https ruby standard classes. You can get such implementation as resource_accessor gem.

Installation

Add this line to to your Gemfile:

1
gem "resource_accessor"

And then execute:

1
$ bundle

Usage

Create accessor object:

1
2
3
require 'resource_accessor' accessor = ResourceAccessor.new

If you want to access unprotected resource, located at some_url, execute this line:

1
response = accessor.get_response :url => some_url

If you want to get protected resource, first get the cookie and then access protected resource:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 1. Get cookie cookie = accessor.get_cookie login_url, user_name, password # 2.a. Get protected resource through POST and post body as hash some_hash = {...} response = accessor.get_response :url => some_url, :method => :post, :cookie => cookie, :body => some_hash # 2.b. Get protected resource through POST and post body as string some_string = "..." response = accessor.get_response :url => some_url, :method => :post, :cookie => cookie, :body => some_string

You have to specify HTTP method explicitly here (post).

If you want to get AJAX resource, add special header to the request or use dedicated method:

1
2
3
4
response1 = accessor.get_response {:url => some_url}, {'X-Requested-With' => 'XMLHttpRequest'} response2 = accessor.get_ajax_response :url => some_url

If you want to get SOAP resource, same as before, add special header to the request or use dedicated method:

1
2
3
4
5
response1 = accessor.get_response {:url => some_url}, {'SOAPAction' => 'someSoapOperation', 'Content-Type' => 'text/xml;charset=UTF-8'} response2 = accessor.get_soap_response :url => some_url

If you want to get JSON resource, same as before, add special header to the request or use dedicated method:

1
2
3
4
response = accessor.get_response {:url => some_url}, {'Content-Type" => "application/json;charset=UTF-8'} response2 = accessor.get_json_response :url => some_url

If you want to provide additional parameters in GET call, use query parameter:

1
response = accessor.get_response :url => some_url, :query => {:param1 => 'p1', :param2 => 'p2'}

or

1
response = accessor.get_response :url => "#{some_url?param1=p1&param2=p2}"

You can setup timeout for your accessor object in milliseconds:

1
accessor.timeout = 10000

If you need to work over ssl, enable certificate validation and certificate file location before the call:

1
2
accessor.validate_ssl_cert = true accessor.ca_file = 'your cert file location'