sinatra

Sinatra - API - Authentication

China☆狼群 提交于 2019-12-18 09:53:33
问题 We going to develop a little API application in Sinatra. What are the authentication options available to secure the API calls? 回答1: Sinatra has no built-in authentication support. There are some gems available, but most are designed for user authentication (i.e. for a website). For an API, they seem like overkill. It’s easy enough to make your own. Simply check the request params in each of your routes to see if they contain a valid API key, and if not, return a 401 error. helpers do def

How do I get Sinatra to work with HTTPClient?

安稳与你 提交于 2019-12-18 07:02:45
问题 I'm trying to create a facade API that receives requests via Sinatra then launches HTTP requests on Github API in the backend. In my "hello world" script I have: #!/usr/bin/ruby require 'httpclient' require 'sinatra' get '/foo' do "hello world" end However, it runs into errors like: $ ./api.rb /usr/local/share/gems/gems/sinatra-1.4.3/lib/sinatra/base.rb:1408:in `run!': undefined method `run' for HTTP:Module (NoMethodError) from /usr/local/share/gems/gems/sinatra-1.4.3/lib/sinatra/main.rb:25

Sinatra does not start with twitter gem

只愿长相守 提交于 2019-12-18 06:13:45
问题 when i try to start sinatra, i'm getting following error /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1488:in start_server': undefined method run' for HTTP:Module (NoMethodError) require 'sinatra/base' require_relative "twt.rb" class SinatraApp < Sinatra::Base set :static, true set :public_folder, File.dirname(__FILE__) + '/static' get '/getuserinfo' do @user = twit.getuserinfo erb :userInfo end end SinatraApp.run! in "twt.rb" i require twitter (5.7.1) require 'twitter' class

ActiveRecord connection warning. (Database connections will not be closed automatically)

孤街浪徒 提交于 2019-12-18 04:04:24
问题 I'm trying to create a little app with Sinatra and ActiveRecord (3.2.3). This is how my main file looks like: require "sinatra" require "sinatra/reloader" require "active_record" ... ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: 'db.sqlite3', host: 'localhost', ) class Post < ActiveRecord::Base ... end get('/') { ... } get('/posts') { ... } ... It works, but sometimes I get a warning in console: DEPRECATION WARNING: Database connections will not be closed

Slicing params hash for specific values

被刻印的时光 ゝ 提交于 2019-12-18 03:57:28
问题 Summary Given a Hash, what is the most efficient way to create a subset Hash based on a list of keys to use? h1 = { a:1, b:2, c:3 } # Given a hash... p foo( h1, :a, :c, :d ) # ...create a method that... #=> { :a=>1, :c=>3, :d=>nil } # ...returns specified keys... #=> { :a=>1, :c=>3 } # ...or perhaps only keys that exist Details The Sequel database toolkit allows one to create or update a model instance by passing in a Hash: foo = Product.create( hash_of_column_values ) foo.update( another

Sinatra and Rack Protection setting

穿精又带淫゛_ 提交于 2019-12-18 03:56:08
问题 I am using Sinatra and CORS to accept a file upload on domain A (hefty.burger.com). Domain B (fizzbuzz.com) has a form that uploads a file to a route on A. I have an options route and a post route, both named '/uploader'. options '/uploader' do headers 'Access-Control-Allow-Origin' => 'http://fizz.buzz.com', 'Access-Control-Allow-Methods' => 'POST' 200 end post '/uploader' do ... content_type :json [{:mary => 'little lamb'}].to_json end The options gets hit first... and it works.. then the

Setting up third-party server to interact with Game Center

∥☆過路亽.° 提交于 2019-12-17 23:18:47
问题 I'm thinking of adding a feature to my iOS game to allow players to create their own game levels, share them with other players, rate them, etc. There'd be a public repository of user-created levels, sortable by creation date, rating, difficulty, or other criteria. This kind of functionality would necessitate a third-party server. I was thinking I'd create a RESTful API using Sinatra and run it on Heroku. My question is: what would be the best way to authenticate requests to this API? I would

Mount Sinatra app inside a rails app and sharing layout

懵懂的女人 提交于 2019-12-17 23:03:01
问题 I would like to mount a sinatra application in my rails app. But I would like this one to share the same layout. The iframe could work but do you have any other idea ? Thanks 回答1: You basically need to do two things: You need to tell the Rails router that a certain URL path is to be handled by another Rack app (in your case a Sinata app). This can be done by adding this to your routes.rb: match "/sinatra" => MySinatraApp, :anchor => false Having done that, you can create your app like so:

Sinatra controller params method coming in empty on JSON post request

浪尽此生 提交于 2019-12-17 10:58:30
问题 I've got a Sinatra app and in most of my controllers json comes in and is picked up automatically in the params object. However, I've got a post action that doesn't get the params at all unless I play a trick with a before method to pull the request.body parameters parse them as JSON and merge them into the params hash. Here is the controller, along with the filter method: before do if request.request_method == "POST" body_parameters = request.body.read params.merge!(JSON.parse(body

Is Sinatra multi threaded?

吃可爱长大的小学妹 提交于 2019-12-17 08:12:40
问题 Is Sinatra multi-threaded? I read else where that "sinatra is multi-threaded by default", what does that imply? Consider this example get "/multithread" do t1 = Thread.new{ puts "sleeping for 10 sec" sleep 10 # Actually make a call to Third party API using HTTP NET or whatever. } t1.join "multi thread" end get "/dummy" do "dummy" end If I access "/multithread" and "/dummy" subsequently in another tab or browser then nothing can be served(in this case for 10 seconds) till "/multithread"