Class: Xiaomipush::HttpBase

Inherits:
Object
  • Object
show all
Defined in:
lib/xiaomipush/http_base.rb

Direct Known Subclasses

Feedback, Stats, Subscription, Tracer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = "", secret_key = "", connection_adapter = :net_http, headers = {}) ⇒ HttpBase

Returns a new instance of HttpBase.



8
9
10
11
12
13
# File 'lib/xiaomipush/http_base.rb', line 8

def initialize(host="", secret_key="", connection_adapter = :net_http, headers={})
  @host = host
  @secret_key = secret_key
  @connection_adapter = connection_adapter
  @headers = headers
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/xiaomipush/http_base.rb', line 6

def host
  @host
end

#secret_keyObject

Returns the value of attribute secret_key.



6
7
8
# File 'lib/xiaomipush/http_base.rb', line 6

def secret_key
  @secret_key
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/xiaomipush/http_base.rb', line 6

def url
  @url
end

Instance Method Details

#connObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/xiaomipush/http_base.rb', line 18

def conn
  headers = {authorization: "key=#{@secret_key}"}.merge(@headers)
  Faraday.new(:url => @host, :headers => headers) do |faraday|
    faraday.request  :url_encoded
    # faraday.response :logger
    faraday.options.timeout = 5           # open/read timeout in seconds
    faraday.options.open_timeout = 2      # connection open timeout in seconds
    faraday.adapter  @connection_adapter 
  end
end

#get(url, params = {}) ⇒ Object



16
# File 'lib/xiaomipush/http_base.rb', line 16

def get(url, params={}); conn.get url, params end

#post(url, body = {}) ⇒ Object



15
# File 'lib/xiaomipush/http_base.rb', line 15

def post(url, body={});  conn.post url, body end