Class: XiamiRadio::User

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/xiami_radio/user.rb

Overview

There is a user as you saw

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUser

Returns a new instance of User.



11
12
13
14
15
# File 'lib/xiami_radio/user.rb', line 11

def initialize
  @cookie_jar = HTTP::CookieJar.new
  @cookie_jar.load cookie_file if File.exist? cookie_file
  
end

Instance Attribute Details

Returns the value of attribute cookie_jar.



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

def cookie_jar
  @cookie_jar
end

#is_vipObject (readonly)

Returns the value of attribute is_vip.



8
9
10
# File 'lib/xiami_radio/user.rb', line 8

def is_vip
  @is_vip
end

#levelObject (readonly)

Returns the value of attribute level.



8
9
10
# File 'lib/xiami_radio/user.rb', line 8

def level
  @level
end

#nick_nameObject (readonly)

Returns the value of attribute nick_name.



8
9
10
# File 'lib/xiami_radio/user.rb', line 8

def nick_name
  @nick_name
end

#signObject (readonly)

Returns the value of attribute sign.



8
9
10
# File 'lib/xiami_radio/user.rb', line 8

def sign
  @sign
end

#user_idObject (readonly)

Returns the value of attribute user_id.



8
9
10
# File 'lib/xiami_radio/user.rb', line 8

def user_id
  @user_id
end

Instance Method Details

#login?Boolean

Returns:

  • (Boolean)


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

def login?
  !user_id.to_s.empty?
end

#login_by_email(email, password) ⇒ Object



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

def (email, password)
  page_uri = .uri(path: '/member/login')
  form_uri = .uri(path: '/passport/login')
  .get page_uri, format: :html
  form_data = {
    _xiamitoken: xiami_token,
    done: 'http%3A%2F%2Fwww.xiami.com%2F%2F',
    verifycode: '',
    account: email,
    pw: password,
    submit: '登录'
  }
  .post form_uri, form_data, headers: {
    'Content-Type' =>	'application/x-www-form-urlencoded; charset=UTF-8',
    'Referer' => page_uri.to_s
  }
  @cookie_jar.save cookie_file
  
end

#login_by_member_auth(member_auth) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/xiami_radio/user.rb', line 41

def (member_auth)
  uri = client.uri path: '/index/home'
  @cookie_jar.parse "member_auth=#{member_auth}; path=/; domain=.xiami.com", uri
  client.get uri, format: :head
  @cookie_jar.save cookie_file
  
end

#xiami_tokenObject



49
50
51
# File 'lib/xiami_radio/user.rb', line 49

def xiami_token
  cookie_jar.cookies.select { |c| c.name == '_xiamitoken' }.first&.value
end