How to call one endpoint from another modules?

779 views
Skip to first unread message

Michael Fan

unread,
Jan 19, 2016, 12:28:11 AM1/19/16
to Grape Framework Discussion
The same question like Call request inside other request. Maybe like:

require './twitter'

module MyWebsite
 
class SomeResource < Grape::API
    resources
:user do
      post
do
       
# some code here
       
Twitter::API.endpoint['some_namespace'].call(POST, params)
     
end
   
end
 
end
end

Is there a better way do this expect for moving the common used logic code to one independent file or using a RestClient make a HTTP Request outside?

Daniel Doubrovkine

unread,
Jan 19, 2016, 7:49:04 AM1/19/16
to ruby-...@googlegroups.com
There's currently no better way. You should probably refactor the common code out. For an API the "correct" way would be to do a 302 redirect if the result is supposed to be the same, but then there's POST and not all (none?) clients will POST on a redirect.
--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock


Michael Fan

unread,
Jan 19, 2016, 8:26:07 AM1/19/16
to Grape Framework Discussion
Daniel, thank you. 302 redirect is not a solution. If we use 302 for internal calling, we have to consume 302 twice, direct into and then back...
I wish there is one approach to make DSL calling. What does the 'get/post/delete' DSL do with his class object?
I want to use the module mechanism of Ruby language first, this may work:

## File: twitter.rb
module Twitter
 
class API < Grape::API
   
def self.some_method(a, b)
     
return a + b
   
end
 
end
end

## File: my_website.rb

require './twitter'

module MyWebsite
 
class SomeResource < Grape::API
    resources
:user do
      post
do

       
Twitter::API.some_method(params[:a], params[:b])
     
end
   
end
 
end
end



在 2016年1月19日星期二 UTC+8下午8:49:04,Daniel Doubrovkine写道:
There's currently no better way. You should probably refactor the common code out. For an API the "correct" way would be to do a 302 redirect if the result is supposed to be the same, but then there's POST and not all (none?) clients will POST on a redirect.

On Tuesday, January 19, 2016, Michael Fan <fanli...@gmail.com> wrote:
The same question like Call request inside other request. Maybe like:

require './twitter'

module MyWebsite
 
class SomeResource < Grape::API
    resources
:user do
      post
do
       
# some code here
       
Twitter::API.endpoint['some_namespace'].call(POST, params)
     
end
   
end
 
end
end

Is there a better way do this expect for moving the common used logic code to one independent file or using a RestClient make a HTTP Request outside?

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Daniel Doubrovkine

unread,
Jan 19, 2016, 10:05:35 AM1/19/16
to ruby-...@googlegroups.com
Modules definitely will work. The construction of the actual endpoint involves duplicating some things, so you have to be careful with variables inside these methods, otherwise it's just Ruby.

To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Michael Fan

unread,
Jan 19, 2016, 11:15:01 PM1/19/16
to Grape Framework Discussion
I debugged the Twitter::API, but the method `self.some_method()` does not exist at all. The Grape DSL trick with method delegate, instance variable @endpoints is Array type and there is no getter function to easily access by routes/namespace.

在 2016年1月19日星期二 UTC+8下午11:05:35,Daniel Doubrovkine写道:
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages