<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> File: README — Documentation by YARD 0.9.37 </title> <link rel="stylesheet" href="css/style.css" type="text/css" /> <link rel="stylesheet" href="css/common.css" type="text/css" /> <script type="text/javascript"> pathId = "README"; relpath = ''; </script> <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script> <script type="text/javascript" charset="utf-8" src="js/app.js"></script> </head> <body> <div class="nav_wrap"> <iframe id="nav" src="file_list.html?1"></iframe> <div id="resizer"></div> </div> <div id="main" tabindex="-1"> <div id="header"> <div id="menu"> <a href="_index.html">Index</a> » <span class="title">File: README</span> </div> <div id="search"> <a class="full_list_link" id="class_list_link" href="class_list.html"> <svg width="24" height="24"> <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect> <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect> <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect> </svg> </a> </div> <div class="clear"></div> </div> <div id="content"><div id='filecontents'> <h1 id="label-SimpleHttpService+rdoc-image-3Ahttps-3A-2F-2Fbadge.fury.io-2Frb-2Fsimple_http_service.svg">SimpleHttpService <a href="https://badge.fury.io/rb/simple_http_service"><img src="https://badge.fury.io/rb/simple_http_service.svg"></a></h1> <p>SimpleHttpService is a simple Ruby library to make HTTP requests with customizable options for headers, timeouts, and retries. It provides a convenient way to create and send HTTP requests using a clean and simple interface.</p> <h2 id="label-Installation">Installation</h2> <p>Add this line to your application’s Gemfile:</p> <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>simple_http_service</span><span class='tstring_end'>'</span></span> </code></pre> <p>And then execute:</p> <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_bundle'>bundle</span> <span class='id identifier rubyid_install'>install</span> </code></pre> <p>Or install it yourself as:</p> <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='id identifier rubyid_install'>install</span> <span class='id identifier rubyid_simple_http_service'>simple_http_service</span> </code></pre> <h2 id="label-Usage">Usage</h2> <h3 id="label-Creating+a+Client">Creating a Client</h3> <p>You can create a new HTTP client using the SimpleHttpService.new method, which initializes an instance of SimpleHttpService::Client.</p> <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>simple_http_service</span><span class='tstring_end'>'</span></span> <span class='id identifier rubyid_client'>client</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="SimpleHttpService.html" title="SimpleHttpService (module)">SimpleHttpService</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'><span class='object_link'><a href="SimpleHttpService.html#new-class_method" title="SimpleHttpService.new (method)">new</a></span></span><span class='lparen'>(</span> <span class='label'>url:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>https://api.example.com/endpoint</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>http_method:</span> <span class='symbol'>:get</span><span class='comma'>,</span> <span class='label'>headers:</span> <span class='lbrace'>{</span> <span class='label'>accept:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>application/json</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>authorization:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Bearer your_token</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>content_type:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>application/json</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>cookie:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>your cookie</span><span class='tstring_end'>'</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>open_timeout:</span> <span class='int'>5</span><span class='comma'>,</span> <span class='label'>read_timeout:</span> <span class='int'>10</span><span class='comma'>,</span> <span class='label'>write_timeout:</span> <span class='int'>5</span><span class='comma'>,</span> <span class='label'>max_retries:</span> <span class='int'>3</span><span class='comma'>,</span> <span class='label'>additional_headers:</span> <span class='lbrace'>{</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>X-Request-Id</span><span class='label_end'>':</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>12345</span><span class='tstring_end'>'</span></span> <span class='rbrace'>}</span> <span class='rparen'>)</span> </code></pre> <h3 id="label-Making+a+Request">Making a Request</h3> <p>After creating the client, you can call the call method to make the HTTP request:</p> <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_response'>response</span> <span class='op'>=</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span> <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_response'>response</span><span class='period'>.</span><span class='id identifier rubyid_body'>body</span> </code></pre> <h3 id="label-Options">Options</h3> <ul><li> <p><code>url</code> (required): The URL for the HTTP request.</p> </li><li> <p><code>http_method</code> (required): The HTTP method to use (:get, :post, :put).</p> </li><li> <p><code>headers</code>: A hash of headers to include in the request.</p> </li><li> <p><code>open_timeout</code>: Timeout for opening the connection (default is false).</p> </li><li> <p><code>read_timeout</code>: Timeout for reading the response (default is false).</p> </li><li> <p><code>write_timeout</code>: Timeout for writing the request (default is false).</p> </li><li> <p><code>max_retries</code>: The number of times to retry the request in case of failure.</p> </li><li> <p><code>request_body</code>: The body of the request (used for POST and PUT requests).</p> </li><li> <p><code>additional_headers</code>: Additional headers to include in the request.</p> </li></ul> <h3 id="label-Example">Example</h3> <p>Here’s a complete example of using <code>SimpleHttpService</code> to make a <code>GET</code> request:</p> <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>simple_http_service</span><span class='tstring_end'>'</span></span> <span class='id identifier rubyid_client'>client</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="SimpleHttpService.html" title="SimpleHttpService (module)">SimpleHttpService</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'><span class='object_link'><a href="SimpleHttpService.html#new-class_method" title="SimpleHttpService.new (method)">new</a></span></span><span class='lparen'>(</span> <span class='label'>url:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>https://api.example.com/endpoint</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>http_method:</span> <span class='symbol'>:get</span><span class='comma'>,</span> <span class='label'>headers:</span> <span class='lbrace'>{</span> <span class='label'>accept:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>application/json</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>authorization:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Bearer your_token</span><span class='tstring_end'>'</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>open_timeout:</span> <span class='int'>5</span><span class='comma'>,</span> <span class='label'>read_timeout:</span> <span class='int'>10</span> <span class='rparen'>)</span> <span class='id identifier rubyid_response'>response</span> <span class='op'>=</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span> <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_response'>response</span><span class='period'>.</span><span class='id identifier rubyid_body'>body</span> </code></pre> <p>For a POST request with a request body:</p> <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_require'>require</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>simple_http_service</span><span class='tstring_end'>'</span></span> <span class='id identifier rubyid_client'>client</span> <span class='op'>=</span> <span class='const'><span class='object_link'><a href="SimpleHttpService.html" title="SimpleHttpService (module)">SimpleHttpService</a></span></span><span class='period'>.</span><span class='id identifier rubyid_new'><span class='object_link'><a href="SimpleHttpService.html#new-class_method" title="SimpleHttpService.new (method)">new</a></span></span><span class='lparen'>(</span> <span class='label'>url:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>https://api.example.com/endpoint</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>http_method:</span> <span class='symbol'>:post</span><span class='comma'>,</span> <span class='label'>headers:</span> <span class='lbrace'>{</span> <span class='label'>accept:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>application/json</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>authorization:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>Bearer your_token</span><span class='tstring_end'>'</span></span><span class='comma'>,</span> <span class='label'>content_type:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>application/json</span><span class='tstring_end'>'</span></span> <span class='rbrace'>}</span><span class='comma'>,</span> <span class='label'>request_body:</span> <span class='lbrace'>{</span> <span class='label'>key:</span> <span class='tstring'><span class='tstring_beg'>'</span><span class='tstring_content'>value</span><span class='tstring_end'>'</span></span> <span class='rbrace'>}</span><span class='period'>.</span><span class='id identifier rubyid_to_json'>to_json</span><span class='comma'>,</span> <span class='label'>open_timeout:</span> <span class='int'>5</span><span class='comma'>,</span> <span class='label'>read_timeout:</span> <span class='int'>10</span><span class='comma'>,</span> <span class='label'>write_timeout:</span> <span class='int'>5</span> <span class='rparen'>)</span> <span class='id identifier rubyid_response'>response</span> <span class='op'>=</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span> <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_response'>response</span><span class='period'>.</span><span class='id identifier rubyid_body'>body</span> </code></pre> <h3 id="label-Error+Handling">Error Handling</h3> <p>The library defines a custom error class SimpleHttpService::Error that you can use to handle exceptions:</p> <pre class="code ruby"><code class="ruby"><span class='kw'>begin</span> <span class='id identifier rubyid_response'>response</span> <span class='op'>=</span> <span class='id identifier rubyid_client'>client</span><span class='period'>.</span><span class='id identifier rubyid_call'>call</span> <span class='id identifier rubyid_puts'>puts</span> <span class='id identifier rubyid_response'>response</span><span class='period'>.</span><span class='id identifier rubyid_body'>body</span> <span class='kw'>rescue</span> <span class='const'><span class='object_link'><a href="SimpleHttpService.html" title="SimpleHttpService (module)">SimpleHttpService</a></span></span><span class='op'>::</span><span class='const'><span class='object_link'><a href="SimpleHttpService/Error.html" title="SimpleHttpService::Error (class)">Error</a></span></span> <span class='op'>=></span> <span class='id identifier rubyid_e'>e</span> <span class='id identifier rubyid_puts'>puts</span> <span class='tstring'><span class='tstring_beg'>"</span><span class='tstring_content'>An error occurred: </span><span class='embexpr_beg'>#{</span><span class='id identifier rubyid_e'>e</span><span class='period'>.</span><span class='id identifier rubyid_message'>message</span><span class='embexpr_end'>}</span><span class='tstring_end'>"</span></span> <span class='kw'>end</span> </code></pre> <h2 id="label-Development">Development</h2> <p>After checking out the repo, run <code>bin/setup</code> to install dependencies. Then, run <code>rspec</code> to run the tests. You can also run <code>bin/console</code> for an interactive prompt that will allow you to experiment.</p> <p>To install this gem onto your local machine, run <code>bundle install</code>. To release a new version, update the version number in <code>version.rb</code>.</p> <h2 id="label-Contributing">Contributing</h2> <p>Bug reports and pull requests are welcome on GitHub at <a href="https://github.com/gklsan/simple_http_service">github.com/gklsan/simple_http_service</a>.</p> <h2 id="label-License">License</h2> <p>The gem is available as open source under the terms of the <a href="https://opensource.org/licenses/MIT">MIT License</a>.</p> <h2 id="label-Code+of+Conduct">Code of Conduct</h2> <p>Everyone interacting in the SimpleHttpService project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.</p> </div></div> <div id="footer"> Generated on Sun Jan 19 17:15:39 2025 by <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a> 0.9.37 (ruby-3.2.2). </div> </div> </body> </html>