Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 88 additions & 23 deletions src/Servant/Ruby.hs
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,23 @@ require "net/http"
require "uri"
<BLANKLINE>
class Baz
def initialize(origin)
def initialize(origin, timeout = nil)
@origin = URI(origin)
@http = Net::HTTP.new(@origin.host, @origin.port)
<BLANKLINE>
unless timeout.nil?
@http.open_timeout = timeout
@http.read_timeout = timeout
end
@http.use_ssl = @origin.scheme == 'https'
end
<BLANKLINE>
def get()
uri = URI("#{@origin}")
def get_uri()
URI("#{@origin}")
end
<BLANKLINE>
req = Net::HTTP::Get.new(uri)
def get()
req = Net::HTTP::Get.new(get_uri())
<BLANKLINE>
@http.request(req)
end
Expand All @@ -114,15 +122,23 @@ require "uri"
module Foo
module Bar
class Baz
def initialize(origin)
def initialize(origin, timeout = nil)
@origin = URI(origin)
@http = Net::HTTP.new(@origin.host, @origin.port)
<BLANKLINE>
unless timeout.nil?
@http.open_timeout = timeout
@http.read_timeout = timeout
end
@http.use_ssl = @origin.scheme == 'https'
end
<BLANKLINE>
def get()
uri = URI("#{@origin}")
def get_uri()
URI("#{@origin}")
end
<BLANKLINE>
req = Net::HTTP::Get.new(uri)
def get()
req = Net::HTTP::Get.new(get_uri())
<BLANKLINE>
@http.request(req)
end
Expand All @@ -134,22 +150,34 @@ Captures and query parameters are translated into required arguments, in that or

The request body and headers are translated into keyword arguments, in that order.

>>> let api = Proxy :: Proxy ("foo" :> Capture "fooId" Int :> ReqBody '[JSON] () :> QueryParam "barId" Bool :> Header "Max-Forwards" Int :> Post '[JSON] ())
>>> let api = Proxy :: Proxy ("foo" :> Capture "fooId" Int :> ReqBody '[JSON] () :> QueryParam "barId" Bool :> QueryParams "ids" Int :> Header "Max-Forwards" Int :> Post '[JSON] ())
>>> Data.Text.IO.putStr $ ruby (NameSpace [] "Foo") api
require "json"
require "net/http"
require "uri"
<BLANKLINE>
class Foo
def initialize(origin)
def initialize(origin, timeout = nil)
@origin = URI(origin)
@http = Net::HTTP.new(@origin.host, @origin.port)
<BLANKLINE>
unless timeout.nil?
@http.open_timeout = timeout
@http.read_timeout = timeout
end
@http.use_ssl = @origin.scheme == 'https'
end
<BLANKLINE>
def post_foo_by_foo_id_uri(foo_id, bar_id, ids)
foo_id = if foo_id.kind_of?(Array) then foo_id.join(',') else foo_id end
<BLANKLINE>
URI("#{@origin}/foo/#{foo_id}?barId=#{bar_id}&#{ ids.collect { |x| 'ids[]=' + x.to_s }.join('&') }")
end
<BLANKLINE>
def post_foo_by_foo_id(foo_id, bar_id, body:, max_forwards:)
uri = URI("#{@origin}/foo/#{foo_id}?barId=#{bar_id}")
def post_foo_by_foo_id(foo_id, bar_id, ids, body:, max_forwards:)
foo_id = if foo_id.kind_of?(Array) then foo_id.join(',') else foo_id end
<BLANKLINE>
req = Net::HTTP::Post.new(uri)
req = Net::HTTP::Post.new(post_foo_by_foo_id_uri(foo_id, bar_id, ids))
req["Content-Type"] = "application/json"
req["Max-Forwards"] = max_forwards
<BLANKLINE>
Expand Down Expand Up @@ -197,20 +225,34 @@ properIndent indent =
initialize :: Int -> [Text]
initialize indent =
properIndent indent
[ Just "def initialize(origin)"
[ Just "def initialize(origin, timeout = nil)"
, Just " @origin = URI(origin)"
, Just " @http = Net::HTTP.new(@origin.host, @origin.port)"
, Nothing
, Just " unless timeout.nil?"
, Just " @http.open_timeout = timeout"
, Just " @http.read_timeout = timeout"
, Just " end"
, Just " @http.use_ssl = @origin.scheme == 'https'"
, Just "end"
]

public :: Int -> Req NoContent -> [Text]
public indent req =
properIndent indent $
[ Nothing
, Just $ "def " <> functionName <> "(" <> argsStr <> ")"
, Just $ " uri = URI(" <> url <> ")"
, Just $ "def " <> functionName <> "_uri(" <> argsStr <> ")"
]
++ cleanCaptures
++
[ Just $ " URI(" <> url <> ")"
, Just "end"
, Nothing
, Just $ " req = Net::HTTP::" <> method <> ".new(uri)"
, Just $ "def " <> functionName <> "(" <> allArgsStr <> ")"
]
++ cleanCaptures
++
[ Just $ " req = Net::HTTP::" <> method <> ".new(" <> functionName <> "_uri(" <> argsStr <> "))"
]
++ requestHeaders
++
Expand All @@ -222,15 +264,38 @@ public indent req =
functionName :: Text
functionName = req ^. reqFuncName.snakeCaseL.to snake

cleanCaptures :: [Maybe Text]
cleanCaptures =
case captures of
[] -> []
xs ->
(Just . (<>) " " . cleanCapture . snake <$> xs)
++ [ Nothing ]

cleanCapture :: Text -> Text
cleanCapture c =
T.concat
[ c
, " = if "
, c
, ".kind_of?(Array) then "
, c
, ".join(',') else "
, c
, " end"
]

argsStr :: Text
argsStr = T.intercalate ", " $ snake <$> args

allArgsStr :: Text
allArgsStr = T.intercalate ", " $ snake <$> (args ++ bodyAndHeader)

args :: [Text]
args =
captures
++ ((^. queryArgName.argPath) <$> queryparams)
++ body
++ headerArgs
args = captures ++ ((^. queryArgName.argPath) <$> queryparams)

bodyAndHeader :: [Text]
bodyAndHeader = body ++ headerArgs

segments :: [Segment NoContent]
segments = filter isCapture paths
Expand Down Expand Up @@ -319,7 +384,7 @@ paramToStr qarg =
case qarg ^. queryArgType of
Normal -> key <> "=#{" <> val <> "}"
Flag -> key
List -> key <> "[]=#{" <> val <> "}"
List -> "#{ " <> val <> ".collect { |x| '" <> key <> "[]=' + x.to_s }.join('&') }"
where
key = qarg ^. queryArgName.argName._PathSegment
val = snake key
Expand Down