Class: Socialcast::CommandLine::ProvisionPhoto

Inherits:
Object
  • Object
show all
Includes:
Provisioner
Defined in:
lib/socialcast/command_line/provision_photo.rb

Defined Under Namespace

Classes: ApiSyncStrategy

Constant Summary

Constants included from Provisioner

Socialcast::CommandLine::Provisioner::DEFAULT_OUTPUT_FILE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Provisioner

#initialize

Instance Attribute Details

#sync_strategyObject

Returns the value of attribute sync_strategy.



6
7
8
# File 'lib/socialcast/command_line/provision_photo.rb', line 6

def sync_strategy
  @sync_strategy
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/socialcast/command_line/provision_photo.rb', line 58

def configured?
  unsupported_configurations.none?
end

#default_profile_photo_idObject



54
55
56
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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_configurationsObject



62
63
64
65
66
# 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