Class: Jmessage::Http

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHttp

Returns a new instance of Http.



8
9
10
11
12
13
14
# File 'lib/jmessage/http.rb', line 8

def initialize
  self.conn = Faraday.new(url: remote_path) do |faraday|
    faraday.request  :url_encoded             # form-encode POST params
    faraday.response :logger                  # log requests to STDOUT
    faraday.adapter  Faraday.default_adapter  # make requests with Net::HTTP
  end
end

Instance Attribute Details

#connObject

Returns the value of attribute conn.



6
7
8
# File 'lib/jmessage/http.rb', line 6

def conn
  @conn
end

#responseObject

Returns the value of attribute response.



6
7
8
# File 'lib/jmessage/http.rb', line 6

def response
  @response
end

Instance Method Details

#delete(uri, params = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/jmessage/http.rb', line 47

def delete(uri, params = {})
  self.response = conn.delete do |req|
    req.url uri
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Basic #{Jmessage::Sign.authorize}"
    req.options.timeout = 10
    req.body = JSON(params)
  end
end

#get(uri) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/jmessage/http.rb', line 16

def get(uri)
  self.response = conn.get do |req|
    req.url uri
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Basic #{Jmessage::Sign.authorize}"
    req.options.timeout = 10
  end
  parse_body
end

#parse_bodyObject



57
58
59
# File 'lib/jmessage/http.rb', line 57

def parse_body
  JSON(response.body)
end

#post(uri, params = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/jmessage/http.rb', line 26

def post(uri, params = {})
  self.response = conn.post do |req|
    req.url uri
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Basic #{Jmessage::Sign.authorize}"
    req.options.timeout = 10
    req.body = JSON(params)
  end
  parse_body
end

#put(uri, params = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/jmessage/http.rb', line 37

def put(uri, params = {})
  self.response = conn.put do |req|
    req.url uri
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Basic #{Jmessage::Sign.authorize}"
    req.options.timeout = 10
    req.body = JSON(params)
  end
end

#remote_pathObject



61
62
63
# File 'lib/jmessage/http.rb', line 61

def remote_path
  'https://api.im.jpush.cn'
end