Sorry to ask such a simple question but I didn't see any possible answer in internet.
I just wondering in Sinatra, can I write something like:
get '/users/:user_id/posts/:id' do
xxx
end
just like rails? cause when I write this on my rb file, sinatra keep telling me it didn't know this ditty.
thank you
A (horrible) example from one of our legacy systems.
get '/is_type/:type/id/:id' do
store.valid_for_type?(params)
end
Multiple placeholders in a route is totally fine in Sinatra. It sounds like your config.ru is wrong, you're issuing a POST, or you're calling the wrong endpoint entirely. Can you update your post with an example of the request you are issuing?
Related
I seem to be missing something basic, as I don't know how to capture and then handle an incoming HTTP Post. I'm a C/C++ developer learning Ruby for the first time, and I've never dealt with web-stuff in my life.
I'm writing my first Slack Webhook, and slack will send data similar to this to my webserver:
token=ZLAmT1sKurm2KwvmYDR9hbiV
team_id=T0001
team_domain=example
channel_id=C2147483705
channel_name=test
user_id=U2147483697
user_name=Steve
command=/weather
text=94070
response_url=https://hooks.slack.com/commands/1234/5678
I have no idea how to accept this POST request, and send it to my application for processing.
I assume it's similar to handling argv and argc for a terminal application, but I'm stuck writing the first few lines.
I've searched for a solution, but it seems like I'm asking the wrong questions.
I currently have a Puma/Sinatra webserver running on heroku, with the Procfile containing:
web: bundle exec puma -p $PORT (I also have no idea what port is assigned to it.)
Similar to this answer, Sinatra provides a DSL to accept POST requests. Within the block, the data will be accessible in the params hash. Something like
post '/my_endpoint' do
content_type :json
#team_id = params[:team_id]
res = do_stuff_with_channel_id params[:channel_id] # passing the value to a custom method example
{my_response: res}.to_json #simple example of returning JSON in response
end
Sinatra Docs on routing
HTH
I have a few Ruby scripts that process text files in different ways, that many of my friends find useful. However, most of the people I know are not comfortable running scripts on the command line. The easiest thing for them would be to create a simple webpage where people could upload a file, select a few options, have it processed, and then download the result.
I know it wouldn't be too hard to build something like this in Rails or Merb or something like that, however it seems like a very common problem, so I was wondering if there was already some kind of template, or similar application that I could easily modify, i.e. let the user upload a file, choose a few options, then {fill in code to do something with file}, let the user download the resulting file?
In the past I used Carrierwave to upload user avatars.
If you are used to Rails it's really straightforward.
Let it be a TextFile resource:
gem 'carrierwave'
$ rails g scaffold textfile content:string title:string etc etc
$ rails g uploader textfile
class TextFile < ActiveRecord::Base
attr_accesible :content
mount_uploader :content, TextFileUploader
end
And that is is pretty much all you have to do to obtain the app's skeleton. However, to answer your real question, no, I don't think there is already a rails app that does exactly that.
https://github.com/jnicklas/carrierwave
I found sinatra-fileupload, which pretty perfectly answers my question. It's a very minimalistic framework which works perfectly, I can just plug in the file handling, and change the layout etc a bit. There were many examples of sophisticated Rails plugins linked to databases, with versioning and stuff, but I really wanted the most minimal example.
I wrote my own FaceBook library that uses actual Curl requests, not libcurl.
Is there a way to test it? I'm asking this because most solutions involve using something like fakeweb which as far as I can tell will not work here.
The existing code can be found on my github page.
One approach would be to use a different host/port in test mode (eg localhost:12345)
Then in your test run a sinatra or webrick servlet on that port that you configure to respond to the requests your code should be making
You could mock Request.dispatcher with an expected behavior, pretty much like Fakeweb would do.
There are a few examples on this file, specially https://github.com/chrisk/fakeweb/blob/master/lib/fake_web/ext/net_http.rb#L44.
When running your tests/specs, monkey-patch the run method of your Request class to hook into the Marston VCR library. See the existing library_hooks subdir for examples and ideas on how to do this -- the fakeweb implementation is a good place to start.
VCR works well with live services like Facebook's because it captures interactions "as is", and VCRs can be easily re-recorded when the services change.
I'm running into problems with your library, however. You need to require the cgi and json libraries; it also looks like it requires a Rails environment (it's failing to find with_indifferent_access on Hash).
I'm trying to get the domain name in my Sinatra app but as a newbie I really am struggling to figure out how to do this, and I know it must be possible!
Rack::Request#host_with_port looks promising, but I don't know how to get this from my app - how do I get stuff from Rack in my Ruby code?
Or is there another way - I'm thinking I don't really want to do this every time a request happens (although it's not too bad), but I thought it'd be better if I could just do it once when the application loads up.
Any hints?
simply use request.host inside your code.
get "/" do
puts request.host #=> localhost
end
Take a look at:
request.env.inspect
so you can see all the request environment variables.
I think that you are looking for
request.env["SERVER_NAME"]
ruby newbie alert! (hey that rhymes :))
I have read the official definition but still come up empty handed. What exactly is it when they say middleware? Is the purpose using ruby with https?
the smallish tutorial at patnaik's blog makes things clearer but how do I do something with it on localhost? I have ruby 1.9.2 installed along with rack gem and mongrel server.
Do I start mongrel first? How?
Just to add a simplistic explanation of Rack (as I feel that is missing):
Rack is basically a way in which a web app can communicate with a web server. The communication goes like this:
The web server tells the app about the environment - this contains mainly what the user sent in as his request - the url, the headers, whether it's a GET or a POST, etc.
The web app responds with three things:
the status code which will be something like 200 when everything went OK and above 400 when something went wrong.
the headers which is information web browsers can use like information on how long to hold on to the webpage in their cache and other stuff.
the body which is the actual webpage you see in the browser.
These two steps more or less can define the whole process by which web apps work.
So a very simple Rack app could look like this:
class MyApp
def call(environment) # this method has to be named call
[200, # the status code
{"Content-Type" => "text/plain", "Content-length" => "11" }, # headers
["Hello world"]] # the body
end
end
# presuming you have rack & webrick
if $0 == __FILE__
require 'rack'
Rack::Handler::WEBrick.run MyApp.new
end
You would do well to search for other questions & answers that make sense to you. Try "Getting Started with Rails" or "Ruby Web Development". A lot of different topics on this site have been devoted to this exact subject, so you might save yourself some trouble there...
Ignoring the specifics of your question for a minute, it seems like you want to learn Ruby and build web apps. Before you start delving into Rack or Mongrel or anything else, you should know that there are 2 well established frameworks that help build Ruby web applications. The first is Ruby on Rails, and the other is Sinatra. There are many others, but these are the most well documented on Stack Overflow and the internet in general.
Check out the following links for some background...
www.rubyonrails.org
SO: building-a-website-best-practice-and-architecture-with-ruby
www.railstutorial.org
SO: learning-ruby-on-rails
If you still have a burning desire to answer your question - "what is rack?", you should follow the same process, and end up at this Stack Overflow Answer:
What is Rack middleware?
Good luck!
Very nice answers yes indeed. For my two cents I'll add this because if you know how to get to the documentation behind the scenes here you will find lots of information as I have it stashed here and by no means is all that I have.
http://myrackapps.herokuapp.com/