Project

proxima

0.0
No commit activity in last 3 years
No release in over 3 years
Proxima is a gem the provides models for use with REST endpoints. These models are based upon the Active Model interface
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 10.0
~> 3.5

Runtime

>= 4.0.0, ~> 4.0
~> 2.2
 Project Readme

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