Ruby and Rudy on Rails

Recently upgraded to Rails 5. There seems to be some compatibility issue, in particular when I run “rails c” or “rails s”.

The warning message is

Array values in the parameter are deprecated. Please use a String or nil.

Fixed by applying the same join you see here to the line 11 of bin/spring:
Gem.paths = { 'GEM_PATH' => [Bundler.bundle_path.to_s, *Gem.path].uniq.join(File::PATH_SEPARATOR) }

How to push the code to the remote repository and deploy to Heroku

$git add -A

$git commit -m "made some improvements"

$git checkout master

$git merge test_branch_name

$git push

$git push heroku

$heroku pg:reset DATABASE

$heroku run rake db:migrate

$heroku run rake db:seed

There you go…


Question:

I would like to take information from another website. Therefore (maybe) I should make a request to that website (in my case a HTTP GET request) and receive the response.

How can I make this in Ruby on Rails?

If it is possible, is it a correct approach to use in my controllers?

Answer #1:

You can use Ruby’s Net::HTTP class:

require 'net/http'

url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.to_s)
res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
puts res.body

Answer #2:
You could write a method to do this in the controller

  def api_request(type , url, body=nil, header =nil )
    require "net/http"
    uri = URI.parse(url)
    case type
    when :post
      request = Net::HTTP::Post.new(uri)
      request.body = body
    when :get
      request = Net::HTTP::Get.new(uri)
    when :put
      request = Net::HTTP::Put.new(uri)
      request.body = body
    when :delete
      request = Net::HTTP::Delete.new(uri)
    end
    request.initialize_http_header(header)
    #request.content_type = 'application/json'
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') {|http| http.request request}
  end

Answer #3:
A general example of using Ruby Net::http

url = URI.parse(‘https://eleville.com’)
req = Net::HTTP::Get.new(url.request_uri)
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = (url.scheme == “https”)
response = http.request(req)
puts response.body

 


Question: Should people store JSON object in the database file as a string?

Size is not so much of an issue, the ability to query and maintain the data however is.

If, for example, Greenhaven Press decides they want to change their name to Greenhaven Press International, you’ll have to find the record, deserialize it, change it, serialize it, pump it back into the database.

Consider this: does storing these objects as serialized data offer you a clear added value over storing it in a relational form? If the answer is no, then it might not be worth the hassle.

UPDATE

As far as your update of your question goes: I’m inclined to say no, it makes little or no difference. Whether you update one field or all of them in this json string is irrelevant because the whole process is identical.

Don’t forget that your requirements might change; even though you’re using json on the client side now doesn’t mean you’ll need json in the future. Storing your data in a relational form guarantees technology-independence while preserving relationships, data constraints and queryable metadata: this is where the true value of a relational db lies. Discarding those advantages will neither give you a performance gain nor make your application more scalable or flexible.