Class: Insta::Client
- Inherits:
-
Object
- Object
- Insta::Client
- Defined in:
- lib/insta.rb
Instance Attribute Summary collapse
-
#client_id ⇒ Object
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
Returns the value of attribute client_secret.
-
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
-
#scope ⇒ Object
Returns the value of attribute scope.
Instance Method Summary collapse
- #access_token(code = '') ⇒ Object
- #auth_url ⇒ Object
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
- #long_lived_access_token(access_token) ⇒ Object
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( = {}) @redirect_uri = [:redirect_uri] @client_id = [:client_id] @client_secret = [:client_secret] @scope = [:scope] || 'user_profile,user_media' end |
Instance Attribute Details
#client_id ⇒ Object
Returns the value of attribute client_id.
9 10 11 |
# File 'lib/insta.rb', line 9 def client_id @client_id end |
#client_secret ⇒ Object
Returns the value of attribute client_secret.
9 10 11 |
# File 'lib/insta.rb', line 9 def client_secret @client_secret end |
#redirect_uri ⇒ Object
Returns the value of attribute redirect_uri.
9 10 11 |
# File 'lib/insta.rb', line 9 def redirect_uri @redirect_uri end |
#scope ⇒ Object
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, ) = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end response = JSON.parse(response.body) OpenStruct.new(response) end |
#auth_url ⇒ Object
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) = { use_ssl: uri.scheme == "https", } response = Net::HTTP.start(uri.hostname, uri.port, ) do |http| http.request(request) end response = JSON.parse(response.body) OpenStruct.new(response) end |