class Socialcast::CommandLine::ProvisionPhoto

Attributes

sync_strategy[RW]

Public Instance Methods

configured?() click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 58
def configured?
  unsupported_configurations.none?
end
default_profile_photo_id() click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 54
def default_profile_photo_id
  @default_profile_photo_id ||= Socialcast::CommandLine::Authenticate.current_user['community']['default_profile_photo_id']
end
photo_data_to_file(profile_photo_data) click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 31
def photo_data_to_file(profile_photo_data)
  if profile_photo_data.start_with?('http')
    profile_photo_data = download_photo_data(profile_photo_data)
    return unless profile_photo_data
  end

  ## FORCE ENCODING
  profile_photo_data = profile_photo_data.force_encoding('binary')

  ## CONTENT TYPE
  unless content_type = binary_to_content_type(profile_photo_data)
    log "Skipping photo: unknown image format (supports .gif, .png, .jpg)"
    return
  end

  ## WRITE TEMP FILE
  Tempfile.new(["photo_upload", ".#{content_type}"]).tap do |tempfile|
    tempfile.binmode
    tempfile.write(profile_photo_data)
    tempfile.rewind
  end
end
sync(strategy_klass = ApiSyncStrategy) click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 8
def sync(strategy_klass = ApiSyncStrategy)
  assert_no_unsupported_configurations

  sync_strategy = strategy_klass.new(self)
  process_options = {
    :http_config => http_config,
    :force_sync => @options[:force_sync]
  }

  user_photos = {}

  each_photo_hash do |photo_hash|
    email = photo_hash[LDAPConnector::EMAIL_ATTRIBUTE]
    user_photos[email] = photo_hash[LDAPConnector::PROFILE_PHOTO_ATTRIBUTE]
    if user_photos.size >= sync_strategy.batch_size
      sync_strategy.process(user_photos, process_options)
      user_photos = {}
    end
  end

  sync_strategy.process(user_photos, process_options) if user_photos.any?
end
unsupported_configurations() click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 62
def unsupported_configurations
  @unsupported_configurations ||= @ldap_config['connections'].reject do |connection_name, _|
    LDAPConnector.attribute_mappings_for(connection_name, @ldap_config).key? LDAPConnector::PROFILE_PHOTO_ATTRIBUTE
  end.keys
end

Protected Instance Methods

assert_no_unsupported_configurations() click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 78
def assert_no_unsupported_configurations
  unless configured?
    message = "Cannot sync photos: #{unsupported_configurations.join(', ')} do not have a mapping for the profile photo field."
    log(message)
    raise Socialcast::CommandLine::Provisioner::ProvisionError, message
  end
end
binary_to_content_type(binary_photo_data) click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 93
def binary_to_content_type(binary_photo_data)
  case binary_photo_data
  when Regexp.new("^GIF8", nil, 'n')
    'gif'
  when Regexp.new('^\x89PNG', nil, 'n')
    'png'
  when Regexp.new("^\xff\xd8\xff\xe0\x00\x10JFIF", nil, 'n'), Regexp.new("^\xff\xd8\xff\xe1(.*){2}Exif", nil, 'n')
    'jpg'
  end
end
download_photo_data(profile_photo_data) click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 86
def download_photo_data(profile_photo_data)
  RestClient.get(profile_photo_data)
rescue => e
  log "Unable to download photo #{profile_photo_data}"
  log e.response
end
each_photo_hash() { |photo_hash| ... } click to toggle source
# File lib/socialcast/command_line/provision_photo.rb, line 70
def each_photo_hash
  each_ldap_connector do |connector|
    connector.each_photo_hash do |photo_hash|
      yield photo_hash
    end
  end
end