class Users::Create
SOME_APP_URL = "https://api.someapp.io".freeze
def call
users_from_api.map do |user_params|
user = User.new
user.name = "#{user_params[:first_name]} #{user_params[:last_name}"
user.email = user_params[:email]
user.devices = Device.where(id: user[:id])
user.save!
end
end
def find_devices(user_id)
device_ids = user_devices(user_id).pluck(:id)
Device.where(id: device_ids)
end
def users_from_api
HTTParty.get("#{SOME_APP_URL}/users" ,headers: {
Authorization: "Bearer #{access_token}",
'Content-Type': 'application/json'
})
end
def user_devices(id)
HTTParty.get("#{SOME_APP_URL}/user/#{id}/devices" ,headers: {
Authorization: "Bearer #{access_token}",
'Content-Type': 'application/json'
})
end
def access_token
@access_token ||= HTTPart.post("#{SOME_APP_URL}/token")
end
end
class SomeApp::API
SOME_APP_URL = "https://api.someapp.io".freeze
def users
api_client "/users"
end
def user_devices(id)
api_client "/users/#{id}/devices"
end
private
def api_client(endpoint)
HTTParty.get("#{SOME_APP_URL}#{endpoint}", headers:)
end
def headers
{
Authorization: "Bearer #{access_token}",
'Content-Type': 'application/json'
}
end
def access_token
@access_token ||= HTTPart.post("#{SOME_APP_URL}/token")
end
end
class Users::Create
attr_reader :api_client
def initialize
@api_client = SomeApp::API.new
end
def call
users_from_api.map do |user_params|
user = User.new
user.name = "#{user_params[:first_name]} #{user_params[:last_name}"
user.email = user_params[:email]
user.save!
end
end
private
def users
api_client.users
end
def user_device(user)
api.client.user_device(user[:id])
end
def create_user(params)
user = User.new
user.name = "#{user_params[:first_name]} #{user_params[:last_name}"
user.email = user_params[:email]
user.devices = Device.where(id: user
user.save!
end
end