All Projects → peek-travel → lurch

peek-travel / lurch

Licence: MIT License
A simple Ruby JSON API client.

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to lurch

Videosniffer
视频嗅探服务(VideoSniffer API Service On Android)
Stars: ✭ 68 (+353.33%)
Mutual labels:  json-api, restful
Tns Restful Json Api
This is the code repository that goes along with the "TheNewStack" article for RESTful JSON API post
Stars: ✭ 846 (+5540%)
Mutual labels:  json-api, restful
Bootstrapi
A better framework for building API with PHP. Built using Slim 3, Eloquent, Zend-ACL
Stars: ✭ 86 (+473.33%)
Mutual labels:  json-api, restful
Dashboard Server
A JSON file RESTful API with authorization based on json-server
Stars: ✭ 48 (+220%)
Mutual labels:  json-api, restful
Gores
Go package that handles HTML, JSON, XML and etc. responses
Stars: ✭ 94 (+526.67%)
Mutual labels:  json-api, restful
ctuning-programs
Collective Knowledge extension with unified and customizable benchmarks (with extensible JSON meta information) to be easily integrated with customizable and portable Collective Knowledge workflows. You can easily compile and run these benchmarks using different compilers, environments, hardware and OS (Linux, MacOS, Windows, Android). More info:
Stars: ✭ 41 (+173.33%)
Mutual labels:  json-api
yamlful
YAML-based HTTP client code generation
Stars: ✭ 77 (+413.33%)
Mutual labels:  restful
RESTEasy
REST API calls made easier
Stars: ✭ 12 (-20%)
Mutual labels:  json-api
go-restapi-boilerplate
How I write rest api service in go
Stars: ✭ 55 (+266.67%)
Mutual labels:  restful
nest-blog-api
B站全栈之巅:基于TypeScript的NodeJs框架:NestJs开发博客API (node.js+nest.js)
Stars: ✭ 34 (+126.67%)
Mutual labels:  restful
kungfu
A REST framework, base on JFinal.
Stars: ✭ 78 (+420%)
Mutual labels:  restful
redbull
Develop JSON endpoints fast!
Stars: ✭ 11 (-26.67%)
Mutual labels:  json-api
light-rest-4j
A RESTful framework built on top of light-4j with both Swagger 2.0 and OpenAPI 3.0 supports
Stars: ✭ 113 (+653.33%)
Mutual labels:  restful
rudder
RESTful API Proxy for Helm
Stars: ✭ 60 (+300%)
Mutual labels:  restful
hanami-serializer
Serializer library for hanami applications
Stars: ✭ 22 (+46.67%)
Mutual labels:  json-api
spec
Spezifikation für eine offene Schnittstelle für Ratsinformationssysteme
Stars: ✭ 56 (+273.33%)
Mutual labels:  restful
JSONinSV
JSON lib in Systemverilog
Stars: ✭ 25 (+66.67%)
Mutual labels:  json-api
ck-web
Collective Knowledge web extension to browse CK repositories, visualize interactive graphs and articles, render CK-based websites, implement simple web services with JSON API (for example to crowdsource experiments or unify access to DNN). Demos of interactive articles, graphs and crowdsourced experiments:
Stars: ✭ 31 (+106.67%)
Mutual labels:  json-api
koa-rest-router
Most powerful, flexible and composable router for building enterprise RESTful APIs easily!
Stars: ✭ 67 (+346.67%)
Mutual labels:  restful
in-memoriam
Lightweight, super fast, atomic, transactional in-memory database
Stars: ✭ 13 (-13.33%)
Mutual labels:  restful

Lurch

Build Status Code Coverage Gem Version

lurch

A simple Ruby JSON API client.

Installation

Add this line to your application's Gemfile:

gem "lurch", "~> 0.2"

And then execute:

$ bundle

Or install it yourself:

$ gem install lurch

Basic Usage

Start by creating a store:

store = Lurch::Store.new("http://example.com/api")

Fetch resources from the server

GET individual resources from the server by id:

person = store.from(:people).find("1")
#=> #<Lurch::Resource[Person] id: "1", name: "Bob">

Or GET all of them at once:

people = store.from(:people).all
#=> [#<Lurch::Resource[Person] id: "1", name: "Bob">, #<Lurch::Resource[Person] id: "2", name: "Alice">]

Lurch::Resource objects have easy accessors for all attributes returned from the server:

person.name
#=> "Bob"
person[:name]
#=> "Bob"
person.attributes[:name]
#=> "Bob"

But, Lurch::Resource objects are immutable:

person.name = "Robert"
#=> NoMethodError: undefined method `name=' for #<Lurch::Resource:0x007fe62c848fb8>

Update existing resources

To update an existing resource, create a changeset from the resource, then PATCH it to the server using the store:

changeset = Lurch::Changeset.new(person, name: "Robert")
store.save(changeset)
#=> #<Lurch::Resource[Person] id: "1", name: "Robert">

Existing references to the resource will be updated:

person.name
#=> "Robert"

Create new resources

To create new resources, first create a changeset, then POST it to the server using the store:

changeset = Lurch::Changeset.new(:person, name: "Carol")
new_person = store.insert(changeset)
#=> #<Lurch::Resource[Person] id: "3", name: "Carol">

Filtering

You can add filters to your request if your server supports them:

people = store.from(:people).filter(name: "Alice").all
#=> [#<Lurch::Resource[Person] id: "2", name: "Alice">]

Other query parameters

You can add arbitrary parameters as well. Note that your server should adhere to JSON:API's query parameter constraints.

people = store.from(:people).params(someQuery: "blue").all
# => GET /people?someQuery=blue

Relationships

Lurch can fetch has-many and has-one relationships from the server when they are provided as related links:

person = store.from(:people).find("1")

person.hobbies
#=> #<Lurch::Relationship::Linked href: "http://example.com/api/people/1/friends">
person.hobbies.fetch
#=> #<Lurch::Collection[Hobby] size: 2, pages: 1>

person.best_friend
#=> #<Lurch::Relationship::Linked href: "http://example.com/api/people/1/best-friend">
person.best_friend.fetch
#=> #<Lurch::Resource[Person] id: "2", name: "Alice">

If the server provides the relationships as resource identifiers instead of links, you can get some information about the relationships without having to load them:

person = store.from(:people).find("1")

person.hobbies
#=> [#<Lurch::Resource[Hobby] id: "1", not loaded>, ...]
person.hobbies.count
#=> 3
person.hobbies.map(&id)
#=> ["1", "2", "3"]
person.hobbies.map(&:name)
#=> Lurch::Errors::ResourceNotLoaded: Resource (Hobby) not loaded, try calling #fetch first.

person.best_friend
#=> #<Lurch::Resource[Person] id: "2", not loaded>
person.best_friend.id
#=> "2"
person.best_friend.name
#=> Lurch::Errors::ResourceNotLoaded: Resource (Person) not loaded, try calling #fetch first.

Regardless of what kind of relationship it is, it can be fetched from the server:

person.best_friend.id
#=> "2"
person.best_friend.loaded?
#=> false
person.best_friend.fetch
#=> #<Lurch::Resource[Person] id: "2", name: "Alice">
person.best_friend.loaded?
#=> true
person.best_friend.name
#=> "Alice"

Pagination

Lurch supports traversing and requesting paginated results if the server implements pagination:

people = store.from(:people).all
#=> #<Lurch::Collection[Person] size: 1000, pages: 100>

If the server responded with meta data about the resources, you can get some information about them without loading them all:

people.size
#=> 1000
people.page_count
#=> 100

NOTE: This data comes from the top-level meta key in the jsonapi response document. It assumes by default the keys are "record-count" and "page-count" respectively, but can be configured in the store.

To request a specific page, use the page query method:

people = store.from(:people).page(number: 12, size: 50).all
#=> #<Lurch::Collection[Person] size: 1000, pages: 20>

If you'd like to traverse the whole set, you can do that using the collection enumerator or the page enumerator:

people.map(&:name)
# ...many HTTP requests later...
#=> ["Summer Brakus", "Katharina Orn", "Mr. Angus Hickle", "Collin Lowe PhD", "Kaylie Larson", ...]

people.each_page.map(&:size)
# ...many HTTP requests later...
#=> [10, 10, 10, 10, ...]

NOTE: These enumerators can cause many HTTP requests to the server, since when it runs out of the first page of resources, it will automatically request the next page to continue.

TIP: Don't use #count on a collection to get its size. Use #size instead. #count causes the entire collection to be traversed, whereas #size will try and get the information from the collection meta data.

You can also just get the resources from the current page as an array:

people.resources
#=> [#<Lurch::Resource[Person] id: "2", name: "Summer Brakus", email: "[email protected]", twitter: "@summerb2b">, ...]

Authentication

You can add an Authorization header to all your requests by configuring the store:

store = Lurch::Store.new("...", authorization: "Bearer eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJzdWIiOjEsIm5hbWUiOiJCb2IifQ.")

Customize Faraday

You can customize faraday by passing a block to Store.new. Use this to add arbitrary headers or faraday middlewares.

store = Lurch::Store.new("http://example.com/api") do |conn|
  # conn.use MyFaradayMiddleware
  conn.headers['X-Request-Id'] = '123'
end

store.from(:people).all

Contributing

  1. Fork it (https://github.com/peek-travel/lurch/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].