https://github.com/pragmarb/pragma-client
Build clients for your Pragma-based APIs.
https://github.com/pragmarb/pragma-client
api client http rest
Last synced: 15 days ago
JSON representation
Build clients for your Pragma-based APIs.
- Host: GitHub
- URL: https://github.com/pragmarb/pragma-client
- Owner: pragmarb
- License: mit
- Created: 2018-02-25T10:25:51.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-03-17T18:26:49.000Z (over 7 years ago)
- Last Synced: 2025-03-03T16:50:21.102Z (over 1 year ago)
- Topics: api, client, http, rest
- Language: Ruby
- Homepage:
- Size: 85 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Pragma::Client
[](https://travis-ci.org/pragmarb/pragma-client)
[](https://coveralls.io/github/pragmarb/pragma-client?branch=master)
[](https://codeclimate.com/github/pragmarb/pragma-client/maintainability)
Welcome to Pragma::Client, a gem for implementing clients for Pragma-based APIs.
The clients you can build with this gem are very similar to [Stripe's Ruby client](https://github.com/stripe/stripe-ruby)
in features and behavior.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'pragma-client'
```
And then execute:
```bash
$ bundle
```
Or install it yourself as:
```bash
$ gem install pragma-client
```
## Usage
### Configuring clients
First of all, you should define your base resource class:
```ruby
module BlogService
class Resource < Pragma::Client::Resource
# Define the root URL of the API.
self.root_url = 'https://www.example.com/api/v1'
# Define authentication logic.
authenticate_with do |request|
request.query_params[:api_key] = '...'
# or maybe:
request.headers['Authorization'] = 'Bearer ...'
end
end
end
```
Now, you can start creating API resources:
```ruby
module BlogService
class Category < Resource
# Optional: This will be inferred if not provided.
self.base_url = '/categories'
# This assumes you have a `by_category` filter on /articles.
has_many :articles
end
class Article < Resource
belongs_to :category
end
end
```
### Using clients
Here are some usage examples:
```ruby
# Retrieve a category:
category = BlogService::Category.retrieve('test-category')
# Retrieve the articles of the category. This will loop through all the pages:
category.articles.each do |article|
puts article.title
end
# Create a new article in the category:
category.articles.create(
title: 'New Article',
body: 'This is the body of my article',
)
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/pragmarb/pragma-client.
This project is intended to be a safe, welcoming space for collaboration, and contributors are
expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Pragma::Client project’s codebases, issue trackers, chat rooms and
mailing lists is expected to follow the [code of conduct](https://github.com/pragmarb/pragma-client/blob/master/CODE_OF_CONDUCT.md).