Class: XiamiRadio::User

Inherits:
Object
  • Object
show all
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.



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

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.



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

def cookie_jar
  @cookie_jar
end

#is_vipObject (readonly)

Returns the value of attribute is_vip.



6
7
8
# File 'lib/xiami_radio/user.rb', line 6

def is_vip
  @is_vip
end

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/xiami_radio/user.rb', line 6

def level
  @level
end

#nick_nameObject (readonly)

Returns the value of attribute nick_name.



6
7
8
# File 'lib/xiami_radio/user.rb', line 6

def nick_name
  @nick_name
end

#signObject (readonly)

Returns the value of attribute sign.



6
7
8
# File 'lib/xiami_radio/user.rb', line 6

def sign
  @sign
end

#user_idObject (readonly)

Returns the value of attribute user_id.



6
7
8
# File 'lib/xiami_radio/user.rb', line 6

def user_id
  @user_id
end

Instance Method Details

#clientObject



55
56
57
# File 'lib/xiami_radio/user.rb', line 55

def client
  @client ||= Client.new user: self
end

#login?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/xiami_radio/user.rb', line 19

def login?
  !user_id.to_s.empty?
end

#login_by_email(email, password) ⇒ Object



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

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



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

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

#radio(**args) ⇒ Object



15
16
17
# File 'lib/xiami_radio/user.rb', line 15

def radio(**args)
  XiamiRadio::Radio.new user: self, **args
end

#xiami_tokenObject



51
52
53
# File 'lib/xiami_radio/user.rb', line 51

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