class Campfire
  KEYS = ["campfire_subdomain", "campfire_token", "campfire_room"]

  def self.notify_mocks_created(mocks, url)
    room = find_room
    if use? && room
      mock = mocks.first
      mocks_text = mocks.size == 1 ? "a new mock" : "#{mocks.size} new mocks"
      room.speak "#{mock.author.name} posted #{mocks_text}:"
      room.speak mock.image.url
      room.speak url
    end
  # Handle invalid token and invalid subdomain errors respectively.
  rescue Tinder::AuthenticationFailed, NoMethodError                
    # Do nothing.
  end

  def self.find_room
    campfire = Tinder::Campfire.new settings["campfire_subdomain"], self.options
    campfire.find_room_by_name(settings["campfire_room"])
  end

  def self.options
    {
      :ssl => true,
      :token => settings["campfire_token"]
    }
  end

  def self.settings
    settings = {}
    KEYS.each do |key|
      settings[key] = Setting[key]
    end
    settings
  end
  
  def self.use?
    !self.settings["campfire_subdomain"].blank?
  end
end

class Campfire
  KEYS = ["campfire_subdomain", "campfire_token", "campfire_room"]

  def self.notify_mocks_created(mocks, url)
    if use?
      room = find_room
      mock = mocks.first
      mocks_text = mocks.size == 1 ? "a new mock" : "#{mocks.size} new mocks"
      room.speak "#{mock.author.name} posted #{mocks_text}:"
      room.speak mock.image.url
      room.speak url
    end
  # Handle invalid token and invalid subdomain errors respectively.
  rescue Tinder::AuthenticationFailed, NoMethodError
    # Do nothing.
  end

  def self.find_room
    campfire = Tinder::Campfire.new settings["campfire_subdomain"], self.options
    campfire.find_room_by_name(settings["campfire_room"])
  end

  def self.options
    {
      :ssl => true,
      :token => settings["campfire_token"]
    }
  end

  def self.settings
    settings = {}
    KEYS.each do |key|
      settings[key] = Setting[key]
    end
    settings
  end

  def self.use?
    !self.settings["campfire_subdomain"].blank?
  end
end

Fix uploading flow for Mockr instances without Campfire configured
Fix uploading flow for Mockr instances without Campfire configured

Users without a Campfire subdomain set were seeing an error message when
uploading a mock. The mock went through, but we were encountering an error in
attempting to parse the URI ".campfire.now", which does not exist. We now only
parse if a proper campfire subdomain has been configured.

Change-Id: I18af41801b9115baf159869dbb11593d1c96c662
Reviewed-on: https://gerrit.causes.com/5217
Tested-by: Chris Chan <711c73f64afdce07b7e38039a96d2224209e9a6c@causes.com>
Reviewed-by: Adam Derewecki <0e18f44c1fec03ec4083422cb58ba6a09ac4fb2a@causes.com>
