Guest User

Untitled

a guest
Nov 6th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. require 'rubygems'
  2. require 'active_record'
  3. require 'mysql2'
  4. require 'sinatra/base'
  5.  
  6. ActiveRecord::Base.establish_connection(
  7. :adapter => 'mysql2',
  8. :host => '127.0.0.1',
  9. :port => '3306',
  10. :database => 'MY_DB',
  11. :username => 'MY_USER',
  12. :password => 'MY_PASSWORD'
  13. )
  14.  
  15. class Contact < ActiveRecord::Base
  16. end
  17.  
  18.  
  19. class FormContact < Sinatra::Base
  20.  
  21.  
  22. before do
  23. content_type :json
  24. end
  25.  
  26.  
  27. post '/register' do
  28. contact = Contact.new(json_params)
  29. if contact.save
  30. status 201
  31. else
  32. status 400
  33. end
  34. end
  35.  
  36.  
  37. helpers do
  38. def json_params
  39. begin
  40. JSON.parse(request.body.read)
  41. rescue
  42. halt 400, {message: 'Invalid json'}.to_json
  43. end
  44. end
  45. end
  46.  
  47.  
  48. run!
  49. end
Add Comment
Please, Sign In to add comment