Class: TiendaNube::Auth

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

Constant Summary collapse

Auth_URL =
{ :ar => "https://www.tiendanube.com/apps",
:br => "https://www.nuvemshop.com.br/apps"}

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(country = :ar) ⇒ Auth

Returns a new instance of Auth.



11
12
13
14
15
# File 'lib/tiendanube/auth.rb', line 11

def initialize(country = :ar)
  @client_id = Auth.client_id
  @client_secret = Auth.client_secret
  @country = country
end

Class Attribute Details

.client_idObject

Returns the value of attribute client_id.



4
5
6
# File 'lib/tiendanube/auth.rb', line 4

def client_id
  @client_id
end

.client_secretObject

Returns the value of attribute client_secret.



4
5
6
# File 'lib/tiendanube/auth.rb', line 4

def client_secret
  @client_secret
end

Class Method Details

.config {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



8
9
10
# File 'lib/tiendanube/auth.rb', line 8

def self.config
  yield self
end

Instance Method Details

#authorize_urlObject



16
17
18
# File 'lib/tiendanube/auth.rb', line 16

def authorize_url
  [Auth_URL[@country], @client_id, "authorize"].join("/")  
end

#get_access_token(code) ⇒ Object



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

def get_access_token(code)
  params = {
    :client_id => @client_id,
    :client_secret => @client_secret,
    :grant_type => 'authorization_code',
    :code => code
  }
  uri = URI([Auth_URL[@country], 'authorize/token'].join('/'))
  request = Net::HTTP.post_form(uri ,params)
  if request.code != "200"
    return {:error => "server error"}
  else
    json = JSON.parse(request.body)
    if json["error_description"]
      {:error => json["error_description"]}
    else
      { :store_id => json["user_id"],
        :access_token => json["access_token"],
        :scope => json["scope"] }
    end
  end
end