mashery_rails

Coverage Status Build Status Code Climate

A Rails library for the Mashery API.

Includes:

  • config/mashery.yml config file
  • A simple Query generator for the RPC API, and it's SQL-esque syntax
  • Good test coverage

Installation

Add the following to your Gemfile

gem "mashery_rails", require: "mashery"

Then run the installer to create config/mashery.yml

$ rails g mashery:install

Configuration

Edit config/mashery.yml with your site_key, key and secret options.

---
site_id: '123'
key: "abc"
secret: "xyz"
host: "api.mashery.com"

Note this doesn't distinguish between development, testing, production, etc. If you really need that functionality, open a new issue.

JSON-RPC API

The central data objects are all listed on the RPC API documentation page, and should be supported. However, fetching objects are the main & typically only supported method for each of these objects. If you feel you want to support more methods, feel free to create a pull request.

all

Fetch the first 100 objects of a particular type. The 100 objects constraint is caused by Mashery:

Query results are always paginated. There is a limit of fetching 100 records at one time. By default, only the first 100 records are returned.

Mashery::Member.all #=>

find_each

Much like ActiveRecord::Base.find_each, this will take a block and auto-paginate the object set, working around the 100 items per page limitation stated above.

Mashery::Member.find_each {|m| m.do_something! }

first

Fetch the first object of a particular type. This is effectively setting "ITEMS 1" and reifying that object.

Mashery::Member.first #=>

count

The total number of objects.

Mashery::Member.count #=>

Query Language

Mashery has a basic SQL-like Query Language. This library provides basic AREL-like functionality to build those queries. One small, but important difference is lack of lazy loading. These methods produce queries, but you have to call all in order to retrieve the objects.

select

Change the SELECT fragment of the query.

Mashery::Member.select("name") #=>

where

Change the WHERE fragment of the query.

Mashery::Member.where(name: "User Name")

#=> SELECT * FROM members WHERE name = 'User Name' ITEMS 100

items

Change the ITEMS fragment of the query.

Mashery::Member.items(10)

#=> => SELECT * FROM members ITEMS 10

page

Change the PAGE fragment of the query.

Mashery::Member.page(10)

#=> SELECT * FROM members PAGE 10 ITEMS 100

REST API

Mashery's REST API are mostly calls based on service objects.