0.0
Low commit activity in last 3 years
No release in over a year
Asynchronous API communication - Server
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Project Readme

Asyncapi::Server

Asyncapi::Server is a Rails engine that allows asynchronous responses for API calls made by Asyncapi::Client.

Installation

Add asyncapi-server into your Gemfile, bundle install, then run the following commands:

rails g asyncapi:server:config
rake asyncapi_server:install:migrations
rake db:migrate

Make sure you have :host in the default_url_options set up for your server. Example config/initializers/default_url_options.rb:

Rails.application.routes.default_url_options ||= {}
Rails.application.routes.default_url_options[:host] = ENV["HOST"]

Usage

In your controller where you want a create action:

# some_controller.rb
class SomeController < ApplicationController
  async :create, CreateSomething
end

Create the CreateSomething runner class that will be called in the background:

# create_something.rb
class CreateSomething
  def self.call(params)
    something = Something.new(params)

    # the last line in this method is sent back to Asyncapi::Client as the `message`
    if something.save
      something.to_json
    else
      something.errors.messages
    end
  end
end

If you use protected_attributes, in an initializer:

Asyncapi::Server::Job.attr_accessible :status, :callback_url, :class_name, :params, :secret

Usage without Asyncapi::Client

If you want to use this without asyncapi client, you need to prepare two things: the endpoint that asyncapi-server will reply to.

Create the job by POSTing the following to CreateSomething above:

{
  "job": {
    "callback_url": "https://myclient.com/jobs_callback",
    "params": {
      "name": "Something's name",
      "approved": true
    },
    "secret": "A secret unique to this job, so that you know what job the server is referring to"
  }
}

When the server is done processing, it will post something to your client. Your endpoint must accept the following json as the body:

{
  "job": {
    "status": "success",
    "message": "The output of the Runner class (i.e. `CreateSomething`)",
    "secret": "The secret you had sent earlier (this is how you can be sure it's not someone else updating your endpoint)",
  }
}

RSpec

If you want to create an integration spec for you Asyncapi server endpoint, make sure you require the helper:

require "asyncapi/server/rspec"

When you make a request, instead of post, use asyncapi_post. Ex:

asyncapi_post("/api/v1/long_running_job", params: { name: "Compute" })

This helper calls post underneath but builds the request in a way that Asyncapi server understands.

Development

  • Run rake db:migrate && rake db:migrate RAILS_ENV=test
  • Make changes
  • rspec

License

Copyright (c) 2016 G5

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.