Project

klient

0.0
No commit activity in last 3 years
No release in over 3 years
Experimental REST API client library.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0.8.21
>= 0.11.3
>= 12.3.1
>= 3.7.0
>= 0.16.1

Runtime

>= 2.5.1
>= 1.7.0
>= 2.0.0
 Project Readme

klient

A REST client library I'm working on. I've only recently started on it and it has a long way to go before it might be worth using.

postcodes.io client example:

# Partial implementation of API for https://api.postcodes.io
class Postcodes
  include Klient
  default_collection_accessor :result

  def initialize
    super("https://api.postcodes.io", content_type: :json, accept: :json)
  end

  collection :postcodes, identifier: :postcode

  resource :random do
    resource :postcodes
  end
end

resource = api.postcodes.get "OX49 5NU"
=> #<Postcodes::Postcodes:70141412425480 @url="https://api.postcodes.io/postcodes">

# Most recent HTTP status for resource:
resource.status_code
=> 200

# Resource delegates method calls down to last response ('result' isn't a
# defined method call: It's just part of the structure of the document that's
# getting returned.):
resource.result.parish
=> "Brightwell Baldwin"

# Postcode bulk lookup. Collection responses are arrays right now, next step is
# to create a collection class that stores headers, allows pagination etc.
results = api.postcodes.post(postcodes: ["OX49 5NU", "M32 0JG", "NE30 1DP"])
=> [#<Postcodes::Postcodes:70243439926840 @url="https://api.postcodes.io/postcodes/OX49%205NU">,
 #<Postcodes::Postcodes:70243439926340 @url="https://api.postcodes.io/postcodes/M32%200JG">,
 #<Postcodes::Postcodes:70243439925820 @url="https://api.postcodes.io/postcodes/NE30%201DP">]

# Gets a random postcode.
resource = api.random.postcodes.get
=> #<Postcodes::Random::Postcodes:70243435956120 @url="https://api.postcodes.io/random/postcodes">
resource.result.postcode
=> "PO20 2WA"