Class: Instagram::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, Media, Users
Defined in:
lib/instagram_api/client.rb,
lib/instagram_api/client/media.rb,
lib/instagram_api/client/users.rb

Overview

Client for the Instagram API.

Defined Under Namespace

Modules: Media, Users

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Users

#feed, #liked, #recent, #search, #user

Methods included from Media

#media, #media_search, #popular_media

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
24
25
26
27
# File 'lib/instagram_api/client.rb', line 19

def initialize(options={})
  if options[:access_token]
    @access_token = options[:access_token]
  else
    @client_id = options[:client_id]
    @client_secret = options[:client_secret]
    @callback_url = options[:callback_url]
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



17
18
19
# File 'lib/instagram_api/client.rb', line 17

def access_token
  @access_token
end

#callback_urlObject

Returns the value of attribute callback_url.



17
18
19
# File 'lib/instagram_api/client.rb', line 17

def callback_url
  @callback_url
end

#client_idObject

Returns the value of attribute client_id.



17
18
19
# File 'lib/instagram_api/client.rb', line 17

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



17
18
19
# File 'lib/instagram_api/client.rb', line 17

def client_secret
  @client_secret
end

Instance Method Details

#authorize_urlString

Return the authorize URL for a client.

Returns:

  • (String)

    The authorization URL.

See Also:



33
34
35
# File 'lib/instagram_api/client.rb', line 33

def authorize_url
  "#{Default::AUTH_URL}?client_id=#{@client_id}&redirect_uri=#{@callback_url}&response_type=code"
end

#get_access_token(code = nil) ⇒ String

Get an access token for a user.

Examples:

client.get_access_token('88fb89ab65454da2a06f2c6dacd09436')
# => '1313345.3fedf64.a0fcb7f40e02fe3da50500'

Parameters:

  • code (String) (defaults to: nil)

    The code retrieved after authorizing a user.

Returns:

  • (String)

    The Access Token.

See Also:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/instagram_api/client.rb', line 45

def get_access_token(code=nil)
  unless @access_token
    params = { 
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: 'authorization_code',
      redirect_uri: @callback_url,
      code: code
    } 
    response = post "https://api.instagram.com/oauth/access_token", {}, params, { 'Content-type' => 'application/x-www-form-urlencoded' }
    @access_token = response['access_token']
    @access_token
  else
    @access_token
  end
end