ryz310/my_api_client

View on GitHub
lib/generators/rails/templates/api_client.rb.erb

Summary

Maintainability
Test Coverage
# frozen_string_literal: true

class <%= "#{class_name}ApiClient" %> < ::ApplicationApiClient
  endpoint '<%= options[:endpoint] %>'

  # error_handling json: { '$.errors.code': 10 }, raise: MyApiClient::Error

  def initialize
  end

<% yeild_request_arguments do |action, http_method, pathname| -%>
  # <%= "#{http_method.upcase} #{pathname}" %>
  #
  # @return [Sawyer::Resource] description_of_the_api_response
  # @raise [MyApiClient::Error] description_of_the_error
  # @see Reference of the API
  def <%= action %>
<% if http_method == 'get' -%>
    query = {}
    <%= http_method %> '<%= pathname %>', query: query, headers: headers
<% else -%>
    body = {}
    <%= http_method %> '<%= pathname %>', body: body, headers: headers
<% end -%>
  end
<% end -%>

  private

  def headers
    {
      'Content-Type': 'application/json',
    }
  end
end