Project

web_api

0.0
Low commit activity in last 3 years
No release in over a year
Ruby library for web API's.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 1.0, >= 1.0.221218
 Project Readme

WebApi

DESCRIPTION:

Ruby library for web API's.

SYNOPSIS:

require 'web_api'
webapi = WebApi.new "https://api.site.org/path-to-base/",
                             header: {Authorization: "Bearer ABC123XYZ"}
# for a post to https://api.site.org/path-to-base/resource...
webapi.add(:resource, type: :post)
# You can pass the post's (or query's) key value pairs in a hash.
body = webapi.resource(data: {key: "value"})

INSTALL:

$ gem install web_api

MORE:

There's not that much code here... Under 200 lines in lib/**.rb at the time of this writing. Take a look at the examples given at github for use cases.

The model is that at each step...

  1. instantiation of a WebApi object
  2. addition of a WebApi method
  3. call to a WebApi method

...one builds up the URL, type, data, and header of the HTTP request. The WebApi methods #new, #add, and #<method> all have the same argument signature:

extension String,
type:     Symbol,
data:     Hash,
header:   Hash,
dumper:   Proc,
Parser:   Proc|Hash(String, Proc)

The extension builds up the url by concatenation. The data and headers hashes are built up with merge. The type, dumper, and parser can be changed at each step.

One can read the code to check the minor nuances of each method's signature, such as default values.

Note that #add will assume extension is the same as the name of the method if no extension is given.

Note that #<method> will assume the user meant to pass data if it only gets a hash:

#<method>({"a"=>"ABC","x"=>"XYZ"})
#=> #<method>('', data: {"a"=>"ABC","x"=>"XYZ"})

The dumper to dump the data in a post request is JSON.dump by default if JSON is available.

The parser to parse the body of an application/json type content is JSON.parse by default if available.

If one does not want to parse the response's body, one can set parser: :none. For example:

body = webapi.resourse(data: {key: "value"}, parser: :none)

LICENSE:

(The MIT License)

Copyright (c) 2023 CarlosJHR64

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.