• Stars
    star
    215
  • Rank 183,925 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 13 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

API Smith makes building structured API clients even simpler.

API Smith

API Smith makes building clients for HTTP-based APIs easy.

By building on top of HTTParty and Hashie, API Smith provides tools tailor-made for making API clients for structured data - That is, responses with a well defined design. These tools are made possible by two parts - APISmith::Smash, a smarter dash / hash-like object which lets you have simple declarative data structures and APISmith::Client, a layer on top of HTTParty that makes dealing with APIs even simpler and more consistent.

APISmith::Smash - A Smarter Hash

API Smith's Smash class is a 'smarter' (or, alternatively, structured) hash. Built on top of Hashie::Dash, APISmith::Smash adds several features that make it useful for making objects that represent an external api response. On top of the base Hashie::Dash feature set, APISmith::Smash adds:

Configuration of alternative names for fields

A feature useful for dealing with apis where they may have a fullName field in their api response but you want to use full_name. Configurable by a simple :from option on property declarations.

This importantly lets you deal with one format internally whilst automatically taking care of normalising external data sources. More importantly, it also provides simple methods you can override to handle a wide range of external schemes (e.g. always underscoring the field name).

Configurable transformers

Essentially, any object (e.g. a lambda, a class or something else) that responds to #call can be used to transform incoming data into a useable format. More importantly, your smash classes will also respond to #call meaning they can intelligently be used as transformers for other classes, making complex / nested objects simple and declarative.

On top of this, APISmith::Client also uses the same #call-able convention, making it even easier to use a consistent scheme for converting data across the application.

Using it on a property is as simple as passing a :transformer option with the #call-able object as the value.

A well defined (and documented) api

Making it possible for you to hook in at multiple stages to further specialise your Smash objects for specific API use cases.

APISmith::Client - Making API Clients Sexy

APISmith::Client is a collection of tools built on top of HTTParty that adds tools to make writing API clients simpler. Namely, it adds:

Configurable Endpoints

Put simply, even though HTTParty adds the base_uri class option, there are times where we want to be able to create a class of base logic but still vary a common part of the URI. For this, APISmith::Client supports endpoints. Essentially, a path part that is prefixed to all paths passed to get, post, put and delete.

Using this in your client is as simple as calling the endpoint class method - e.g.:

class MyAPIClient
  include APISmith::Client
  base_uri "http://example.com/"
  endpoint "api/v1"
end

Then, calling MyAPIClient.new.get('/blah') will hit up the url at http://example.com/api/v1/blah.

This is most importantly useful when dealing with restful - base_uri can point to the site root and then you can subclass your base client class and set the endpoint for each resource. More importantly, because you can override APISmith::Client::InstanceMethods#endpoint method, you can also make your endpoint take into account parent resource ids and the like.

Hierarchal Request, Body and Query String options

Out of the box, we give you support for configuring options on three levels:

  • The class - e.g. parameters to set the response type to JSON
  • The instance - e.g. an api key parameter that all instances require
  • The request - e.g. a parameter required for that specific API call.

Out of the box, it transparently supports using these options for both the request body, the request query string and the request options in general (for HTTParty).

For each of these types (query, body and request), it's easy to hook in to them and to set them. For class-level options, simply define a base_#{type}_options method, e.g:

def base_query_options
  {:format => 'json'}
end

For per-instance options, simply use the add_#{type}_options! method (which takes a hash of options). For example, see APISmith::Client::InstanceMethods#add_query_options!.

Finally, you can use the :extra_#{type} options (e.g. :extra_query), for example:

get '/', :extra_query => {:before_timestamp => 2.weeks.ago.to_s}

Response Unpacking

Via the :response_container argument to the get, post, put and delete methods, API Smith supports automatically taking the parsed responses and getting just the bit you care about.

In cases where the API consistently packages the data in a simple manner, it's also possible to override the default response container, making it somewhat simple to automate the whole unpacking process. As an example, say your api returns:

{
  "data": {
    "values": "some-other-data-here"
  }
}

Via the :response_container option, when your transformer is called, it wont have to deal with the data and values keys, You will only need to deal with the contents directly, in this case - "some-other-data-here", simply by passing:

:response_container => %w(data values)

Simple Response Transformations

The most important aspect of APISmith::Client comes down to it's support of the :transform option. Much like the :transformer option on Smash properties, Adding :transform with a #call-able object to your call to get, post, put or delete will automatically invoke your transformer with the unpacked response.

As an added bonus, because APISmith::Smash defines a call class method, you can then simply pass one of your Smash subclasses to the transform option and API Smith will intelligently unpack your data into the objects you care about.

Contributors

API Smith was written by Darcy Laycock, and Steve Webb from The Frontier Group, as part of a bigger project with Filter Squad.

Contributing

We encourage all community contributions. Keeping this in mind, please follow these general guidelines when contributing:

  • Fork the project
  • Create a topic branch for what you’re working on (git checkout -b awesome_feature)
  • Commit away, push that up (git push your_remote awesome_feature)
  • Create a new GitHub Issue with the commit, asking for review. Alternatively, send a pull request with details of what you added.
  • Once it’s accepted, if you want access to the core repository feel free to ask! Otherwise, you can continue to hack away in your own fork.

Other than that, our guidelines very closely match the GemCutter guidelines here.

(Thanks to GemCutter for the contribution guide)

License

API Smith is released under the MIT License (see the license file) and is copyright Filter Squad, 2011.

More Repositories

1

rocket_pants

API building tools on top of ActionController. Also, an awesome name.
Ruby
982
star
2

barista

Simple, transparent CoffeeScript support for Rails and Rack apps.
Ruby
398
star
3

rubyflow

A gitified copy of Peter Cooper's Rubyflow website
Ruby
104
star
4

slugged

Super simple slugs for ActiveRecord 3+, with slug history. Formerly known as Pseudocephalopod.
Ruby
86
star
5

transperth-api

A simple API web app for Transperth
Ruby
40
star
6

marvin

A Ruby IRC library built on EventMachine
Ruby
40
star
7

BirdGrinder

An evented twitter client, specifically built to make it easy to build bots and the like. Inc. built in streaming / search support
Ruby
16
star
8

Pebble

Pebble is a tool for publishing real time event streams from sources such as IRC, Twitter and the like.
CoffeeScript
11
star
9

chef-brightbox

Brightbox Next Gen Ruby + Passenger / Nginx recipes for Chef.
Ruby
9
star
10

auspost-drc

Australia Post Delivery Rate Calculator
Ruby
9
star
11

sabnzbd-ruby

Basic Ruby interface to the SABnzbd api
Ruby
8
star
12

eoraptor

The code behind my blog
Ruby
8
star
13

gitauth-gh

GitAuth + GitHub == Automatic mirror for your github repositories to any gitauth install
Ruby
8
star
14

perennial

A simple event-oriented application library for Ruby
Ruby
7
star
15

emmett

A simple markdown -> site generator, focused on handwritten api documentation.
Ruby
7
star
16

map-builder

A simple ruby wrapper around the mapstraction library
Ruby
7
star
17

Spinderella

Ruby pubsub server with flash xmlsocket, orbited, and websocket support.
JavaScript
6
star
18

shuriken

Simple namespace helpers for javascript, written in coffeescript.
JavaScript
6
star
19

unique_generator

MongoMapper + ActiveRecord Unique Value Generator
Ruby
6
star
20

whitepicketfence

micro sms gateway for the web built with rubygsm, sinatra and a whole bucket of awesomeness.
Ruby
5
star
21

pyapns2

An alternative pyapns client for ruby, built on libxml-xmlrpc.
Ruby
5
star
22

slidedown-template

a simple base project for a presentation built on slidedown
JavaScript
5
star
23

hudson-rvmRuby-plugin

fork of the hudson ruby plugin with support for specifying a ruby string
Java
4
star
24

dead-bird

web based irc client modelled around twitter (like kookaburra but client-centric)
JavaScript
4
star
25

irclerk

Simple IRC logging built on Sinatra, CouchDB and Marvin (with a horrible name)
4
star
26

smokey_bear

Simple HTTP / JSON API smoke tests.
Ruby
4
star
27

chainsaw

Simple real time event streams - Soon with WebSocket, Orbited and Flash Socket support
JavaScript
4
star
28

hudson-rvmRake-plugin

Fork of the hudson rake plugin which detects rvm rubies
Java
4
star
29

kookaburra-server

The IRCD server component of Kookaburra
Ruby
4
star
30

word-count-validator

A simple ActiveModel-based word count validator.
Ruby
3
star
31

88miles

Simple OSX client for 88miles built with MacRuby
Ruby
3
star
32

rocket_pants-core

The inner core of Rocket Pants 2.0 - Error handling primarily.
Ruby
3
star
33

dot-files

Bash, Ack, Git - oh my!
Vim Script
3
star
34

offlogger

IRC Logger Website + Bot (built on Marvin) for #offrails on Freenode
JavaScript
3
star
35

almost-happy

Super simple content conversion / publishing stuff for ActiveRecord 3.0 - Like, stupidly simple.
Ruby
3
star
36

kookaburra-web-app

The web app component of Kookaburra
Ruby
2
star
37

rocket_pants-rpm

A RocketPants extension to make it work with NewRelic RPM.
Ruby
2
star
38

projects

Simple Go-based Project Discovery Tool. Used for tab-completed cd / ls for code I'm working on.
Go
2
star
39

serverless-redirector

Write a URL redirector (shortener, etc etc) Using Amazon S3.
Ruby
2
star
40

roro-perth-rails-3

RORO Perth Talk on Rails 3
Ruby
2
star
41

pixtory

Pixtory API - Contextually relevant history photo API of Doom for #govhack 2013.
Ruby
2
star
42

throttled_object

Implements a distributed throttled object lock thingymabob. Useful for rate limited APIs. And Pandas.
Ruby
2
star
43

ninjahideout-toolkit

A Bunch of Helpers that I should standardise and clean up but have never before bothered.
Ruby
2
star
44

Reversible-Data

A simple migration-like library for doing models and tables in tests
Ruby
2
star
45

tracktime

time tracking for minimalists with an emphasis on no bullshit.
2
star
46

coderack-entries

an unofficial submodulized-mirror of all coderack.org entries
2
star
47

chef-pyapns

Sets up pyapns on a server using Chef.
Ruby
2
star
48

eoraptor-misc

Abandoned attempts at making a rack-friendly blog framework.
Ruby
2
star
49

tavern

Simple PubSub for Ruby
Ruby
1
star
50

tavern-redis

Redis-based distributed PubSub for Tavern.
Ruby
1
star
51

bhm-admin

Generic admin area functionality for Rails 3 applications
Ruby
1
star
52

rumbl.in

URL Shortener + Misc Stuff for RailsRumble
Ruby
1
star
53

machinist_mongo

Ruby
1
star
54

tarsnap.rb

Simple WIP ruby wrapper for Tarsnap I started whilst bored a while ago
Ruby
1
star
55

trix

Trix is a bot for playing tetris. Built for CITS4211 at UWA.
Python
1
star
56

stupid-filter.rb

Ruby wrapper around Stupid Filter, built for a hack at EOTW09. Incomplete
Ruby
1
star
57

spinderella-client

Client Libraries for Spinderella
Ruby
1
star
58

Tweep-Tweep

Tweep Tweep is a real time twitter monitor written in CoffeeScript and running on Node.JS
1
star
59

tavern-history

Adds history support to tavern.
Ruby
1
star