Skip to content

yoavlt/nativegen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nativegen logo

Build Status Coverage Status hex.pm version

Nativegen is a source code generator of native app accessing REST API. This can reduce your dull works at all. If you use Phoenix framework, this is for you.

It is supported just Swift code yet, but will Android, Unity and so on.

Installation

You can add dependency to your project's mix.exs.

  defp deps do
    [
      {:nativegen, "~> 0.4.0"}
    ]
  end

then,

$ mix do deps.get, compile

iOS

Generating code depends on the follwoing library

Example generating source code

import Foundation
import BrightFutures
import Alamofire
import SwiftyJSON

public class User : NSObject, JsonModel {
    var id: Int
    let username: String
    var items: [Item]
    public required init(json: JSON) {
        id = json["id"].int
        username = json["username"].stringValue
        if json["items"].error == nil {
            items = json["items"].arrayValue.map { Item(json: $0) }
        }
    }
}

public class UserRepository : Repository {

    public func create(username: String) -> Future<User, NSError> {
        return requestData(.POST, routes: "/api/users", param: ["user": ["username": username]])
    }

    public func show(id: Int) -> Future<User, NSError> {
        return requestData(.GET, routes: "/api/users/\(id)", param: nil)
    }

    public func update(id: Int, username: String) -> Future<User, NSError> {
        return requestData(.PATCH, routes: "/api/users/\(id)", param: ["user": ["username": username]])
    }

    public func delete(id: Int) -> Future<Bool, NSError> {
        return requestSuccess(.DELETE, routes: "/api/users/\(id)", param: nil)
    }

    public func buyItem(itemId: Int) -> Future<Bool, NSError> {
        return requestSuccess(.POST, routes: "/users/buy", param: ["item_id": itemId])
    }

}

Usage(Swift)

First, you have to setup.

$ mix nativegen.swift.setup /your/to/your/directory

Next, following command will generate accessible REST API swift code.

$ mix nativegen.swift.create /path/to/your/directory User users username:string group:Group items:array:Item

And, generate Json model the following command.

$ mix nativegen.swift.model Item name:string strength:integer

Also, append model in your swift code.

$ mix nativegen.swift.model Item name:string strength:integer --file /path/to/your/repo.swift

You can also generate methods

$ mix nativegen.swift.method post /api/chat/response responseMessage Chat thread_id:integer message:string

Lisence

MIT Lisence

About

Accessible REST API code generator for native app.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages