Class: Insta::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
# File 'lib/insta.rb', line 10

def initialize(options = {})
  @redirect_uri  = options[:redirect_uri]
  @client_id     = options[:client_id]
  @client_secret = options[:client_secret]
  @scope         = options[:scope] || 'user_profile,user_media'
end

Instance Attribute Details

#client_idObject

Returns the value of attribute client_id.



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

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



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

def client_secret
  @client_secret
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



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

def redirect_uri
  @redirect_uri
end

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
end

Instance Method Details

#access_token(code = '') ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/insta.rb', line 21

def access_token(code = '')
  uri = URI.parse("https://api.instagram.com/oauth/access_token")
  request = Net::HTTP::Post.new(uri)
  request.set_form_data(
    "client_id" => client_id,
    "client_secret" => client_secret,
    "code" => code,
    "grant_type" => "authorization_code",
    "redirect_uri" => redirect_uri,
  )

  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end

  response = JSON.parse(response.body)
  OpenStruct.new(response)
end

#auth_urlObject



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

def auth_url
  "https://api.instagram.com/oauth/authorize?client_id=#{client_id}&redirect_uri=#{redirect_uri}&scope=#{scope}&response_type=code"
end

#long_lived_access_token(access_token) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/insta.rb', line 44

def long_lived_access_token(access_token)
  url = "https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret=#{client_secret}&access_token=#{access_token}"
  uri = URI.parse(url)
  request = Net::HTTP::Get.new(uri)
  req_options = {
    use_ssl: uri.scheme == "https",
  }

  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  response = JSON.parse(response.body)
  OpenStruct.new(response)
end