Sample irb commands#

Sample curl commands to speak into RoR#

CURL COMMANDS: 

// For creating a user:

curl -i -H "Accept: application/vnd.api+json" -H 'Content-Type:application/vnd.api+json' -X POST -d '{"data": {"type":"users", "attributes":{"name":"Consuela"}}}' http://localhost:3000/users

// For creating a post that belongs to contact with id of 1:

curl -i -H "Accept: application/vnd.api+json" -H 'Content-Type:application/vnd.api+json' -X POST -d '{ "data": { "type": "posts", "relationships": { "user": { "data": { "type": "users", "id": "1" } } }, "attributes": { "title": "Laughter Post", "content": "HAHAHA" } } }' http://localhost:3000/posts

Rails calling another /api/ (RestClient)#

 # If Game is an ActiveRecord class, probably you should modify this to look
  # for the specific record instead of loading everything with .all
  # If is just a PORO, forget what I said.
  game = Game.all[game_selection.to_i - 1]
  return if game.description.present?

  response = RestClient.get("#{DETAIL_URL}#{slug}", HEADERS)
  data = JSON.parse(response.body)
  game.details = data
end

class Game
  ALLOWED_RATINGS = %w[recommended exceptional meh skip].freeze

  # Moved most of the assigning logic into the Game class. This is debatible as
  # I could use a Service class or another pattern for this, but as I don't
  # know your code's nature, to me looks like the quickest way.
  def details=(data)
    self.description = data['description']
    self.released = data['released']
    # .presence will return nil for blank strings, empty arrays, etc, then with
    # || you will default the value to 0, as:
    #
    #   nil || 0 # => 0
    self.metacritic_rating = data['metacritic'].presence || 0

    data['ratings'].each { |rating| self.rating = rating }
  end

  def rating=(rating)
    # .send method can run any method of this class, so better to check if the
    # value is valid first
    return unless ALLOWED_RATINGS.include? rating['title']

    send("#{rating['title']}_rating=", rating['percent'].presence || 0.0)
  end
end

Use instances instead of class methods#

I assume you want to do something like this Game.get_game_details which makes sense. However, maybe you should wrap the code in the class method in an instance to leverage local state and make it more readable. Here is an example:

def self.get_game_details(game_selection)
  Game.new(RemoteGame.new(game_selection.to_i - 1).to_h)
end

class RemoteGame
  HEADERS = { "x-rapidapi-host" => ENV["HOST"], "x-rapidapi-key" => ENV["API_KEY"]
}.freeze

  def initialize(id)
    @id = id
  end

  def to_h
    JSON.parse(response.body)
  end

  private

    attr_reader :id

    def response
     RestClient.get("#{DETAIL_URL}#{game.slug}", headers)
    end

    def game
      @_game ||= Game.all[id].slug
    end
end

xxx#

ASDF#

# https://x.com/y/1?page=1 
# + current_url( :page => 3 )
# = https://x.com/y/1?page=3
def current_url(overwrite={})
    url_for :only_path => false, :params => params.merge(overwrite)
end

Example Usage:

>current_url --> http://...
>current_url(:page=>4) --> http://...&page=4

dd#

<%= request.env["HTTP_HOST"] + page = "/" + request.path_parameters['controller'] + "/" + request.path_parameters['action'] %>
an excerpt from the partial to make a decision#
- if request.env["HTTP_REFERER"].to_s.scan("history").length > 0
  - back_url = user_history_issue_path(@user, list: "needed_type")
- else
  - back_url = user_needed_type_issue_path(@user)
- remote ||= false
=link_to t("static.back"), back_url, :remote => remote

xx#

request.env['REQUEST_URI']

xx#

(url_for(:only_path => false) == "/" )? root_url : url_for(:only_path => false)