diff --git a/src/Servant/Ruby.hs b/src/Servant/Ruby.hs index 9474d57..6174255 100644 --- a/src/Servant/Ruby.hs +++ b/src/Servant/Ruby.hs @@ -90,15 +90,23 @@ require "net/http" require "uri" class Baz - def initialize(origin) + def initialize(origin, timeout = nil) @origin = URI(origin) @http = Net::HTTP.new(@origin.host, @origin.port) + + unless timeout.nil? + @http.open_timeout = timeout + @http.read_timeout = timeout + end + @http.use_ssl = @origin.scheme == 'https' end - def get() - uri = URI("#{@origin}") + def get_uri() + URI("#{@origin}") + end - req = Net::HTTP::Get.new(uri) + def get() + req = Net::HTTP::Get.new(get_uri()) @http.request(req) end @@ -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) + + unless timeout.nil? + @http.open_timeout = timeout + @http.read_timeout = timeout + end + @http.use_ssl = @origin.scheme == 'https' end - def get() - uri = URI("#{@origin}") + def get_uri() + URI("#{@origin}") + end - req = Net::HTTP::Get.new(uri) + def get() + req = Net::HTTP::Get.new(get_uri()) @http.request(req) end @@ -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" class Foo - def initialize(origin) + def initialize(origin, timeout = nil) @origin = URI(origin) @http = Net::HTTP.new(@origin.host, @origin.port) + + unless timeout.nil? + @http.open_timeout = timeout + @http.read_timeout = timeout + end + @http.use_ssl = @origin.scheme == 'https' + end + + 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 + + URI("#{@origin}/foo/#{foo_id}?barId=#{bar_id}&#{ ids.collect { |x| 'ids[]=' + x.to_s }.join('&') }") end - 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 - 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 @@ -197,9 +225,15 @@ 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" ] @@ -207,10 +241,18 @@ 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 ++ @@ -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 @@ -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