require "services"

class Attachment
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title
  field :filename
  field :file_id, type: String
  field :file_url, type: String

  embedded_in :section_edition

  before_save :upload_file, if: :file_has_changed?

  def to_param
    id.to_s
  end

  def snippet
    "[InlineAttachment:#{filename}]"
  end

  def file=(file)
    @file_has_changed = true
    @uploaded_file = file
  end

  def file_has_changed?
    @file_has_changed
  end

  def upload_file
    raise ApiClientNotPresent unless Services.attachment_api
    begin
      if file_id.nil?
        response = Services.attachment_api.create_asset(file: @uploaded_file)
        self.file_id = response["id"].split("/").last
      else
        response = Services.attachment_api.update_asset(file_id, file: @uploaded_file)
      end
      self.file_url = response["file_url"]
    rescue GdsApi::HTTPNotFound => e
      raise "Error uploading file. Is the Asset Manager service available?\n#{e.message}"
    rescue StandardError
      errors.add(:file_id, "could not be uploaded")
    end
  end

  def content_type
    return unless file_url
    extname = File.extname(file_url).delete(".")
    "application/#{extname}"
  end

  class ::ApiClientNotPresent < StandardError; end
end

require "services"

class Attachment
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title
  field :filename
  field :file_id, type: String
  field :file_url, type: String

  embedded_in :section_edition

  before_save :upload_file, if: :file_has_changed?

  def to_param
    id.to_s
  end

  def snippet
    "[InlineAttachment:#{filename}]"
  end

  def file=(file)
    @file_has_changed = true
    @uploaded_file = file
  end

  def file_has_changed?
    @file_has_changed
  end

  def upload_file
    begin
      if file_id.nil?
        response = Services.attachment_api.create_asset(file: @uploaded_file)
        self.file_id = response["id"].split("/").last
      else
        response = Services.attachment_api.update_asset(file_id, file: @uploaded_file)
      end
      self.file_url = response["file_url"]
    rescue GdsApi::HTTPNotFound => e
      raise "Error uploading file. Is the Asset Manager service available?\n#{e.message}"
    rescue StandardError
      errors.add(:file_id, "could not be uploaded")
    end
  end

  def content_type
    return unless file_url
    extname = File.extname(file_url).delete(".")
    "application/#{extname}"
  end
end

Remove unnecessary guard condition and exception
Remove unnecessary guard condition and exception

I can't see how `Services.attachment_api` would ever not return
something and so I think this guard condition is unnecessary.

What's more the raising of a `ApiClientNotPresent` exception is not
covered in the tests and this exception is never explicitly handled.

If it turns out that `Services.attachment_api` does return `nil` for
some reason, we won't be any worse off - we'll just see a
`NoMethodError` instead of the custom exception.
