Class: InstagramUpload::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/instagram_upload/client.rb

Constant Summary collapse

API_URL =
'https://instagram.com/api/v1/'.freeze
LOGIN =
API_URL + 'accounts/login/'
UPLOAD_PHOTO =
API_URL + 'media/upload/'
CONFIGURE_PHOTO =
API_URL + 'media/configure/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
18
# File 'lib/instagram_upload/client.rb', line 11

def initialize(username, password)
  @username = username
  @password = password
  @agent = Agent.new
  @request = Request.new(agent)

  
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



9
10
11
# File 'lib/instagram_upload/client.rb', line 9

def agent
  @agent
end

#passwordObject (readonly)

Returns the value of attribute password.



9
10
11
# File 'lib/instagram_upload/client.rb', line 9

def password
  @password
end

#requestObject (readonly)

Returns the value of attribute request.



9
10
11
# File 'lib/instagram_upload/client.rb', line 9

def request
  @request
end

#usernameObject (readonly)

Returns the value of attribute username.



9
10
11
# File 'lib/instagram_upload/client.rb', line 9

def username
  @username
end

Instance Method Details

#configure_photo(media_id, caption) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/instagram_upload/client.rb', line 34

def configure_photo(media_id, caption)
  body = agent.body(
    device_timestamp: device_timestamp,
    media_id: media_id,
    caption: caption,
    source_type: 5,
    filter_type: 0,
    extra: {}
  )

  request.post(CONFIGURE_PHOTO, body) do |http|
    http.cookiefile = request.cookies.path
  end
end

#upload_photo(image, caption) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/instagram_upload/client.rb', line 20

def upload_photo(image, caption)
  data = [
    Curl::PostField.file('photo', image),
    Curl::PostField.content('device_timestamp', device_timestamp)
  ]

  result = request.post(UPLOAD_PHOTO, data) do |http|
    http.multipart_form_post = true
    http.cookiefile = request.cookies.path
  end

  configure_photo(result['media_id'], caption)
end