0

I'm make a little program in sinatra and I'm wanted to perfom some dynamic call of post, with diynamic uri, so I make a Connexion class like this:

class Connexion
  def initialize(path)
    @path = path
  end

  def sinatraPost
    post "/#{@path}" do
     # some code
  end
  end
end

But when I'm launch sinatraPost, I've got this error:

 undefined method `post' for #<Connexion:0x000000026206b8> (NoMethodError)

How can I call the sinatra post method in my class ?

EDIT: Okay ! So, I change my strategy, I have this following code:

class Webhook < Sinatra::Base

 get '/:name' do
  # compare with names array
 end
end

Webhook.run!

Thank's to everyone !

Equinox
  • 11
  • 6

1 Answers1

0

It looks like you're going about this the wrong way. If you want to set up your app to receive a POST request, you'll need routing logic in your controller. Sinatra controllers normally look like this:

require 'sinatra'

get '/route1' do
  # do stuff
end

post '/route2' do
  # do stuff
end

If you're using a modular app, you'll want to have your app inherit from Sinatra::Base. See the Sinatra docs for more.

Making a post request is different, and doesn't rely on Sinatra methods.

require 'net/http'

uri = URI("http://google.com")
headers = {}
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri, headers)
response = http.request(request)

Or something like that. Good luck!

beane
  • 158
  • 1
  • 12