The main ingredient of a RESTful API client.
Add the following line to your Gemfile
:
gem 'apitizer'
Then execute:
$ bundle
Alternatively, you can install the gem manually:
$ gem install apitizer
Create an apitizer describing the API of the Web service you would like to interact with:
apitizer = Apitizer::Base.new do
address 'https://service.com/api'
resources :posts do
resources :comments
end
end
The apitizer can now be used to manipulate the resources provided by the
Web service. To this end, there are five methods: index
, show
, create
,
update
, and delete
, which can be used as shown below.
To list the members of a collection:
apitizer.index(:posts)
apitizer.index(:posts, post_id, :comments)
To read a member of a collection:
apitizer.show(:posts, post_id)
apitizer.show(:posts, post_id, :comments, comment_id)
To create a new member in a collection:
apitizer.create(:posts, title: 'To be or not to be')
apitizer.create(:posts, post_id, :comments, content: 'That is the question.')
To update a member of a collection:
apitizer.update(:posts, post_id, title: 'What is the meaning of life?')
apitizer.update(:posts, post_id, :comments, comment_id, content: '42.')
To delete a member of a collection:
apitizer.delete(:posts, post_id)
apitizer.delete(:posts, post_id, :comments, comment_id)
Here is an example for the Typekit API. Check out Typekit Client as well.
Code:
require 'apitizer'
options = {
# Format
format: :json,
# Authorization
headers: { 'X-Typekit-Token' => ENV['tk_token'] },
# Non-standard REST-HTTP mapping
dictionary: { update: :post }
}
apitizer = Apitizer::Base.new(options) do
address 'https://typekit.com/api/v1/json'
resources :families, only: :show do
show ':variation', on: :member
end
resources :kits do
resources :families, only: [ :show, :update, :delete ]
show :published, on: :member
update :publish, on: :member
end
resources :libraries, only: [ :index, :show ]
end
puts JSON.pretty_generate(apitizer.index(:kits))
Output:
{
"kits": [
{
"id": "bas4cfe",
"link": "/api/v1/json/kits/bas4cfe"
},
{
"id": "sfh6bkj",
"link": "/api/v1/json/kits/sfh6bkj"
},
{
"id": "kof8zcn",
"link": "/api/v1/json/kits/kof8zcn"
},
{
"id": "zyx4wop",
"link": "/api/v1/json/kits/zyx4wop"
}
]
}
Apitizer was a part of Typekit Client.