Class: XiamiRadio::Client
- Inherits:
-
Object
- Object
- XiamiRadio::Client
- 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
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http ⇒ Object
Returns the value of attribute http.
-
#user ⇒ Object
Returns the value of attribute user.
Instance Method Summary collapse
- #get(uri, format: :json, headers: {}) ⇒ Object
-
#initialize(headers: {}, user: nil, host: HOST) ⇒ Client
constructor
A new instance of Client.
- #post(uri, form_data, format: :json, headers: {}) ⇒ Object
- #uri(**args) ⇒ Object
Constructor Details
#initialize(headers: {}, user: nil, host: HOST) ⇒ Client
Returns a new instance of Client.
20 21 22 23 24 25 26 27 28 |
# File 'lib/xiami_radio/client.rb', line 20 def initialize(headers: {}, user: nil, host: HOST) @user = user || User.new @headers = HEADERS.merge headers @uri = URI.parse host # @http = Net::HTTP.new(@uri.host, @uri.port, '127.0.0.1', '8888') @http = Net::HTTP.new @uri.host, @uri.port @http.use_ssl = @uri.scheme == 'https' end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
18 19 20 |
# File 'lib/xiami_radio/client.rb', line 18 def headers @headers end |
#http ⇒ Object
Returns the value of attribute http.
18 19 20 |
# File 'lib/xiami_radio/client.rb', line 18 def http @http end |
#user ⇒ Object
Returns the value of attribute user.
18 19 20 |
# File 'lib/xiami_radio/client.rb', line 18 def user @user end |
Instance Method Details
#get(uri, format: :json, headers: {}) ⇒ Object
34 35 36 37 38 |
# File 'lib/xiami_radio/client.rb', line 34 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 |
#post(uri, form_data, format: :json, headers: {}) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/xiami_radio/client.rb', line 40 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
30 31 32 |
# File 'lib/xiami_radio/client.rb', line 30 def uri(**args) @uri.class.build scheme: @uri.scheme, host: @uri.host, **args end |