Module: ZombieFans::Actions::Avatar

Included in:
Robot
Defined in:
lib/zombie_fans/actions/avatar.rb

Instance Method Summary collapse

Instance Method Details

#upload_avatarObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zombie_fans/actions/avatar.rb', line 3

def upload_avatar
  page = agent.get('http://randomavatar.com')
  avatar_url = page.at('.RAFade').attr('src')

  log_action 'UploadAvatar', "with #{avatar_url}."

  page = agent.get('https://github.com/settings/profile')
  form = page.at('form.js-upload-avatar-image')
  owner_id = form.attr('data-alambic-owner-id')
  owner_type = form.attr('data-alambic-owner-type')
  upload_policy_url = form.attr('data-upload-policy-url')
  authenticity_token = form.at('input[name=authenticity_token]').attr('value')
  avatar_filename = "#{}.jpg"
  avatar = open(avatar_url)

  query = {
    name: avatar_filename,
    size: avatar.size,
    content_type: 'image/jpeg',
    authenticity_token: authenticity_token,
    owner_type: owner_type,
    owner_id: owner_id
  }

  page = agent.post(upload_policy_url, query)
  response = JSON.parse page.body

  File.open avatar.path do |avatar_file|
    query = response['asset'].merge({
      authenticity_token: authenticity_token,
      owner_type: owner_type,
      owner_id: owner_id,
      size: avatar.size,
      content_type: 'image/jpeg',
      file: avatar_file
    })

    # agent.agent.allowed_error_codes = [500]
    page = agent.post(response['upload_url'], query, response['header'])
    response = JSON.parse page.body
  end

  agent.get("https://github.com/settings/avatars/#{response['id']}") do |page|
    page = page.form_with(action: "/settings/avatars/#{response['id']}").submit
  end
end