Class: JPush::Http::Client

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

Constant Summary collapse

DEFAULT_USER_AGENT =
'jpush-api-ruby-client/' + JPush::VERSION
DEFAULT_OPEN_TIMEOUT =
20
DEFAULT_READ_TIMEOUT =
120
DEFAULT_RETRY_TIMES =
3
RETRY_SLEEP_TIME =
3
HTTP_VERB_MAP =
{
  get:    Net::HTTP::Get,
  post:   Net::HTTP::Post,
  put:    Net::HTTP::Put,
  delete: Net::HTTP::Delete
}
DEFAULT_HEADERS =
{
  'user-agent' => DEFAULT_USER_AGENT,
  'accept' => 'application/json',
  'content-type' => 'application/json',
  'connection' => 'close'
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jpush, method, url, params: nil, body: nil, headers: {}, opts: {}) ⇒ Client

Returns a new instance of Client.



64
65
66
67
68
69
70
71
# File 'lib/jpush/http/client.rb', line 64

def initialize(jpush, method, url, params: nil, body: nil, headers: {}, opts: {})
  method = method.downcase.to_sym
  @uri = URI(url)
  @uri.query = URI.encode_www_form(params) unless params.nil?
  @request = prepare_request(method, body, headers)
  @request.basic_auth(jpush.app_key, jpush.master_secret)
  @opts = opts
end

Class Method Details

.delete(jpush, url, params: nil, headers: {}) ⇒ Object



24
25
26
# File 'lib/jpush/http/client.rb', line 24

def delete(jpush, url, params: nil, headers: {})
  request(jpush, :delete, url, params: params, headers: headers)
end

.get(jpush, url, params: nil, headers: {}) ⇒ Object



12
13
14
# File 'lib/jpush/http/client.rb', line 12

def get(jpush, url, params: nil, headers: {})
  request(jpush, :get, url, params: params, headers: headers)
end

.post(jpush, url, body:, headers: {}) ⇒ Object



16
17
18
# File 'lib/jpush/http/client.rb', line 16

def post(jpush, url, body: , headers: {})
  request(jpush, :post, url, body: body, headers: headers)
end

.put(jpush, url, body:, headers: {}) ⇒ Object



20
21
22
# File 'lib/jpush/http/client.rb', line 20

def put(jpush, url, body: , headers: {})
  request(jpush, :put, url, body: body, headers: headers)
end

.request(jpush, method, url, params: nil, body: nil, headers: {}, opts: {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jpush/http/client.rb', line 28

def request(jpush, method, url, params: nil, body: nil, headers: {}, opts: {})
  raw_response = self.new(
    jpush,
    method,
    url,
    params: params,
    body: body,
    headers: headers,
    opts: opts
  ).send_request

  Response.new(raw_response)
end

Instance Method Details

#send_requestObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/jpush/http/client.rb', line 73

def send_request
  tries ||= DEFAULT_RETRY_TIMES
  opts ||=  {
    use_ssl: 'https' == @uri.scheme,
    open_timeout: DEFAULT_OPEN_TIMEOUT,
    read_timeout: DEFAULT_READ_TIMEOUT
  }.merge @opts
  Net::HTTP.start(@uri.host, @uri.port, opts) do |http|
    http.request(@request)
  end
# if raise Timeout::Error retry it for 3 times
rescue Net::OpenTimeout, Net::ReadTimeout => e
  (tries -= 1).zero? ? (raise Utils::Exceptions::TimeOutError.new(e)) : retry
end