Class: XiamiRadio::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/xiami_radio/client.rb

Overview

There is a client as you saw

Constant Summary collapse

HOST =
'https://www.xiami.com'.freeze
LOGIN_HOST =
'https://login.xiami.com'.freeze
HEADERS =
{
  'Accept' => '*/*',
  'Accept-Encoding' => '*',
  'Accept-Language' => 'en-US,en;q=0.8,zh;q=0.6,zh-TW;q=0.4',
  'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers: {}, user: nil, host: HOST) ⇒ Client

Returns a new instance of Client.



21
22
23
24
25
26
# File 'lib/xiami_radio/client.rb', line 21

def initialize(headers: {}, user: nil, host: HOST)
  @user = user || User.new
  @headers = HEADERS.merge headers
  @uri = URI.parse host
  init_http
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#httpObject

Returns the value of attribute http.



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

def http
  @http
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#get(uri, format: :json, headers: {}) ⇒ Object



32
33
34
35
36
# File 'lib/xiami_radio/client.rb', line 32

def get(uri, format: :json, headers: {})
  request uri, format, headers do |_headers|
    @http.start { |http| http.request(Net::HTTP::Get.new uri, _headers) }
  end
end

#init_httpObject



46
47
48
49
50
51
52
# File 'lib/xiami_radio/client.rb', line 46

def init_http
  @http = Net::HTTP.new @uri.host, @uri.port
  # -- OR --
  # @http = Net::HTTP.new(@uri.host, @uri.port, '127.0.0.1', '8888')
  # @http.verify_mode = ::OpenSSL::SSL::VERIFY_NONE
  @http.use_ssl = @uri.scheme == 'https'
end

#post(uri, form_data, format: :json, headers: {}) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/xiami_radio/client.rb', line 38

def post(uri, form_data, format: :json, headers: {})
  request uri, format, headers do |_headers|
    req = Net::HTTP::Post.new uri, _headers
    req.set_form_data form_data
    @http.start { |http| http.request req }
  end
end

#uri(**args) ⇒ Object



28
29
30
# File 'lib/xiami_radio/client.rb', line 28

def uri(**args)
  @uri.class.build scheme: @uri.scheme, host: @uri.host, **args
end