Git Product home page Git Product logo

funky_api_client's Introduction

FunkyApiClient

Simple ORM for json API responses mapping based on virtus and httparty gems.

To make gem working you need to specify base url for your backend:

FunkyApiClient.backend_url = 'http://your-backend.com'

class_call method

Use class_call method to map API response to object or collection of objects.

You can also set plain_response option to get pure API response.

This method always performs GET requests.

def class_call(method_name, relative_path, plain_response = false)

Let's consider following example:

class Car < FunkyApiClient::Base
  class_call :count, 'cars/:id/count', true
  class_call :fetch, 'cars/fetch'

  attribute :id
  attribute :brand
end

Assuming that our API returns '123' for cars/1/count endpoint, we can use Car.count(params: { id: 1 }) to return '123'.

Notice that we used params hash. This hash can contain ruote params and query params. We can also pass headers hash to this method.

Let's say our cars/fetch endpoint returns json like [{"id":1,"brand":"Audi"},{"id":2,"brand":"Mercedes"}]. Car.fetch call will return collection of Car objects.

Json like {"id":1,"brand":"Audi"} will be mapped to single Car object.

instance_call method

Use instance_call method to send state of the object to the API. This method also assigns response errors if needed.

def instance_call(method_name, relative_path, method_type, serialization_method_name = nil)

Parameter serialization_method_name must be name of the method that returns serialized object in json format.

E.g.

class Car < FunkyApiClient::Base
  instance_call :create, 'cars', :post, :create_json

  attribute :id
  attribute :brand
  attribute :color

  def create_json
    { brand: brand, color: color }.to_json
  end
end

car = Car.new(brand: 'Audi', color: 'red')
car.create # returns true if response successful, otherwise false

If response status was 422 and response json was like {"errors": ["brand is invalid"]} then car has response_errors set.

funky_api_client's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.