Class: ApiAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/basis-band/api-auth.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#useridObject

Returns the value of attribute userid.



6
7
8
# File 'lib/basis-band/api-auth.rb', line 6

def userid
  @userid
end

Instance Method Details

#auth_request(username, password) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/basis-band/api-auth.rb', line 13

def auth_request(username, password)
  u = URI.parse('https://app.mybasis.com/login')
  http = Net::HTTP.new(u.host, u.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  request = Net::HTTP::Post.new(u.request_uri)
  request.set_form_data({'next' => 'https://app.mybasis.com',
                        'submit' => 'Login',
                        'username' => username,
                        'password' => password})
  response = http.request(request)
  tok_match = response['set-cookie'].match /access_token=([0-9a-f]+);/
  tok_match[1]
end

#get_user_id(access_token) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/basis-band/api-auth.rb', line 28

def get_user_id(access_token)
  u = URI.parse('https://app.mybasis.com/api/v1/user/me.json')
  http = Net::HTTP.new(u.host, u.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  request = Net::HTTP::Get.new(u.request_uri)
  request.add_field('X-Basis-Authorization', "OAuth %s" % access_token)
  json = JSON.parse(http.request(request).body)
  @userid = json['id']
end

#login(username, password) ⇒ Object



8
9
10
11
# File 'lib/basis-band/api-auth.rb', line 8

def (username, password)
  token = auth_request(username, password)
  [token, get_user_id(token)]
end