Есть контроллер с кучей приватных методов. Подскажите, как лучше его реорганизовать и протестировать?
require 'httparty'
class Api::V1::SessionsController < Api::V1::ApplicationController
respond_to :json
def signin
if code = params[:code]
access_token = get_access_token
if access_token.present?
user = get_existing_user_or_create_new
respond_with user, status: :ok
else
respond_with error: 'Invalid code!', status: 401
end
else
head :bad_request
end
end
private
def access_credentials
HTTParty.get('https://oauth.vk.com/access_token', query: {
client_id: Rails.application.secrets.vk_app_id,
client_secret: Rails.application.secrets.vk_app_secret,
code: code,
redirect_uri: signin_api_sessions_url
}).parsed_response
end
def get_access_token
access_credentials["access_token"]
end
def get_existing_user_or_create_new
user_id = access_credentials["user_id"]
user = User.where(uid: user_id).first_or_initialize! do |u|
user_response = HTTParty.get('https://api.vk.com/method/users.get', query: {
user_ids: user_id,
access_token: access_token
}.parsed_response
u.name = %Q(#{user_response['first_name']} #{user_response['last_name']})
u.uid = user_id
end
expires_in = access_credentials["expires_in"]
user.generate_auth_token!(expires_in)
end
end