Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fdfa626150
Fetching contributors…

Cannot retrieve contributors at this time

file 44 lines (33 sloc) 1.258 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
-module(tinderl).
-export([start/4, stop/0, speak/1, paste/1]).

start(Domain, Email, Password, Room) ->
  spawn(fun() ->
    register(tinderl, self()),
    process_flag(trap_exit, true),
    Cmd = lists:flatten(io_lib:format("ruby ./tinderl.rb ~s ~s ~s ~s", [Domain, Email, Password, Room])),
    Port = open_port({spawn, Cmd}, [{packet, 4}, use_stdio, exit_status, binary]),
    port_loop(Port)
  end).

stop() -> tinderl ! stop.


speak(String) when is_list(String) -> speak(list_to_binary(String));
speak(String) when is_binary(String) -> tinderl ! {speak, self(), String}.

paste(String) when is_list(String) -> speak(list_to_binary(String));
paste(String) when is_binary(String) -> tinderl ! {paste, self(), String}.

port_loop(Port) ->
  receive
    {speak, _Caller, String} ->
      Data = term_to_binary({speak, String}),
      Port ! {self(), {command, Data}},

      port_loop(Port);
    
    {paste, _Caller, String} ->
        Data = term_to_binary({paste, String}),
        Port ! {self(), {command, Data}},

        port_loop(Port);
        
    stop ->
      Port ! {self(), close},
      receive
        {Port, closed} -> exit(normal)
      end;
    
    {'EXIT', Port, Reason} ->
      exit({port_terminated,Reason})
  end.
Something went wrong with that request. Please try again.