Git Product home page Git Product logo

proxima's Introduction

Proxima

Proxima is a REST Model for use with REST APIs. Proxima's goal is to be flexible, so it refrains from imposing opinions about the remote api. The goal is to allow a Rails application to model any remote REST resource regardless of how the remote REST is implemented. Things like object shape and property names can be completely remapped. Even subpaths can be putted out into attributes.

Proxima implements the entire Active Model interface.

Installation

Add this line to your application's Gemfile:

gem 'proxima'

And then execute:

$ bundle

Or install it yourself as:

$ gem install proxima

Usage

To use Proxima you must first declare your APIs. You may create as many as you like, but for this example we'll just add one for the GitHub API. You can do this in an initializer file.

# file: config/initializers/proxima.rb

GIT_HUB_API_V3 = Proxima::Api.new('https://api.github.com', {
  headers: {
    accept:        'application/vnd.github.v3+json'
    authorization: 'token OAUTH-TOKEN'
  }
})

Now that we've created our GIT_HUB_API_V3 we can use it to create models.

# file: app/models/user_repo.rb

class UserRepo < Proxima::Model
    api GIT_HUB_API_V3

    # First we must set our model's base uri
    base_uri "/user/repos"
    
    #then add the attributes we are intrested in
    attribute :id,               String,  'id'
    attribute :name,             String,  'name'
    attribute :full_name,        String,  'full_name'
    attribute :description,      String,  'description'
    attribute :private,          String,  'private'
    attribute :fork,             String,  'fork'
    attribute :url,              String,  'url'
    attribute :homepage,         String,  'homepage'
    attribute :forks_count,      Integer, 'forks_count'
    attribute :stargazers_count, Integer, 'stargazers_count'
    attribute :watchers_count,   Integer, 'watchers_count'
    
    # Note that any of the active model methods are avalible on proxima
    # models so feel free to add things such as validation.
    
    def useful_stats
        {
            forks:      self.forks_count,
            stargazers: self.stargazers_count,
            watchers:   self.watchers_count
        }
    end
end

These models can be used in your controllers once defined

# file: app/controllers/user_repos_controller.rb

class UserReposController < ApplicationController
    def index
        @repo_stats = UserRepo.find().map({ |u| u.useful_stats })
    end
end

proxima's People

Contributors

brandynprasad avatar fredmp avatar robertwhurst avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

fredmp

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.