Coder Social home page Coder Social logo

apix's Introduction

Apix

Simple convention and DSL for transformation of elixir functions to a documented and ready for validation API.

Example of usage (very simple get/put API):

defmodule Simple.Api do
  @moduledoc """
  This api describes very simple get/put storage api.
  And should be a very small example of how to use it.
  """
  use Apix
  @name "SimpleStore"
  @tech_name "store"
  api "Get", :get
  api "Put", :put

  def ensure_started() do
    case Process.whereis(:simple) do
      nil ->
        {:ok, pid} = Agent.start_link(fn -> %{} end)
        Process.register(pid, :simple)
      _ ->
        :ok
    end
  end

  @doc """
  Get value for a defined key

  ## Parameters

    * `:key` - string, must be sent
    * `:default` - string, optional, defines default, if nothing found to be returned

  ## Results

  """
  def get(%{key: key} = args) do
    ensure_started()
    %{result: Agent.get(:simple, &Map.get(&1, key, args[:default]))}
  end

  @doc """
  Put a value for the key

  ## Parameters

    * `:key` - string, describes key, on which it will be saved
    * `:value` - string, describes value

  ## Results

  """
  def put(%{key: key, value: value} = _args) do
    ensure_started()
    Agent.update(:simple, &Map.put(&1, key, value))
    %{result: true}
  end
end

Now, it is possible to get information to your API:

iex> Apix.spec(Simple.Api, :methods)
["Get", "Put"]
iex> Apix.spec(Simple.Api, :method, "Put")
%{arguments: [key: %{description: "describes key, on which it will be saved", optional: false, type: "string"},
              value: %{description: "describes value", optional: false, type: "string"}],
  doc: "Put a value for the key"}
iex> Apix.spec(Simple.Api, :name)
"SimpleStore"
iex> Apix.spec(Simple.Api, :doc)
"This api describes very simple get/put storage api.\nAnd should be a very small example of how to use it.\n"

There are some word in documetation, which will be identified, for example: ## Parameters, starting the attributes section.

Each attribute should have the same format: "* key - type, description" or "* key - type, optional, description". Type should be of type, which your validator supports. Apix may support JSON validation in the future.

For more information, use Apix documentation. All examples are actually tested with doctest.

apix's People

Contributors

liveforeverx avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

ruby2elixir

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.