<commit_before># JsonApiClient [![Build Status](https://travis-ci.org/chingor13/json_api_client.png)](https://travis-ci.org/chingor13/json_api_client) [![Code Climate](https://codeclimate.com/github/chingor13/json_api_client.png)](https://codeclimate.com/github/chingor13/json_api_client) [![Code Coverage](https://codeclimate.com/github/chingor13/json_api_client/coverage.png)](https://codeclimate.com/github/chingor13/json_api_client)

This gem is meant to help you build an API client for interacting with REST APIs as laid out by [http://jsonapi.org](http://jsonapi.org). It attempts to give you a query building framework that is easy to understand (it is similar to ActiveRecord scopes).

*Note: master is currently tracking the 1.0.0 specification. If you're looking for the older code, see [0.x branch](https://github.com/chingor13/json_api_client/tree/0.x)*

*Note: This is still a work in progress.*

## Usage

You will want to create your own resource classes that inherit from `JsonApiClient::Resource` similar to how you would create an `ActiveRecord` class. You may also want to create your own abstract base class to share common behavior. Additionally, you will probably want to namespace your models. Namespacing your model will not affect the url routing to that resource.

```
module MyApi
  # this is an "abstract" base class that 
  class Base < JsonApiClient::Resource
    # set the api base url in an abstract base class
    self.site = "http://example.com/"
  end

  class Article < Base
  end

  class Comment < Base
  end

  class Person < Base
  end
end
```

By convention, we figure guess the resource route from the class name. In the above example, `Article`'s path is "http://example.com/articles" and `Person`'s path would be "http://example.com/people".

Some example usage:

```
MyApi::Article.all
MyApi::Article.where(author_id: 1).find(2)
MyApi::Article.where(author_id: 1).all

MyApi::Person.where(name: "foo").order(created_at: :desc).includes(:preferences, :cars).all

u = MyApi::Person.new(first_name: "bar", last_name: "foo")
u.save

u = MyApi::Person.find(1).first
u.update_attributes(
  a: "b",
  c: "d"
)<commit_msg>documentation about setting a resource's path [ci skip]<commit_after># JsonApiClient [![Build Status](https://travis-ci.org/chingor13/json_api_client.png)](https://travis-ci.org/chingor13/json_api_client) [![Code Climate](https://codeclimate.com/github/chingor13/json_api_client.png)](https://codeclimate.com/github/chingor13/json_api_client) [![Code Coverage](https://codeclimate.com/github/chingor13/json_api_client/coverage.png)](https://codeclimate.com/github/chingor13/json_api_client)

This gem is meant to help you build an API client for interacting with REST APIs as laid out by [http://jsonapi.org](http://jsonapi.org). It attempts to give you a query building framework that is easy to understand (it is similar to ActiveRecord scopes).

*Note: master is currently tracking the 1.0.0 specification. If you're looking for the older code, see [0.x branch](https://github.com/chingor13/json_api_client/tree/0.x)*

*Note: This is still a work in progress.*

## Usage

You will want to create your own resource classes that inherit from `JsonApiClient::Resource` similar to how you would create an `ActiveRecord` class. You may also want to create your own abstract base class to share common behavior. Additionally, you will probably want to namespace your models. Namespacing your model will not affect the url routing to that resource.

```
module MyApi
  # this is an "abstract" base class that 
  class Base < JsonApiClient::Resource
    # set the api base url in an abstract base class
    self.site = "http://example.com/"
  end

  class Article < Base
  end

  class Comment < Base
  end

  class Person < Base
  end
end
```

By convention, we figure guess the resource route from the class name. In the above example, `Article`'s path is "http://example.com/articles" and `Person`'s path would be "http://example.com/people".

You can customize this path by changing your resource's `table_name`:

```
module MyApi
  class SomeResource < Base
    def self.table_name
      "foobar"
    end
  end
end

# requests http://example.com/foobar
MyApi::SomeResource.all
```

Some basic example usage:

```
MyApi::Article.all
MyApi::Article.where(author_id: 1).find(2)
MyApi::Article.where(author_id: 1).all

MyApi::Person.where(name: "foo").order(created_at: :desc).includes(:preferences, :cars).all

u = MyApi::Person.new(first_name: "bar", last_name: "foo")
u.save

u = MyApi::Person.find(1).first
u.update_attributes(
  a: "b",
  c: "d"
)
{'fraction_non_alphanumeric': 0.11631944444444445, 'fraction_numerical': 0.009982638888888888, 'mean_word_length': 4.64828431372549, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 3, 'https://': 14, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '153751', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1398, 'n_words': 478}