Class: MatrixDBus::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/matrix_dbus/network.rb

Overview

get, post

Example:

get = Network.gen :get, 'http://localhost:5700'
json = get.call '/get_login_info'

Instance Method Summary collapse

Constructor Details

#initialize(host, error) ⇒ Network

gen lambda

type: Symbol or String, ‘get’, ‘form’ or ‘json; host: String: API address, like ’localhost:5700



12
13
14
15
# File 'lib/matrix_dbus/network.rb', line 12

def initialize(host, error) # => Hash<Symble, Lambda>
  @host = host
  @error = error
end

Instance Method Details

#delete(uri, params = nil) ⇒ Object

delete to url

uri: String params (optional): Hash, url query



78
79
80
81
82
83
84
85
86
87
# File 'lib/matrix_dbus/network.rb', line 78

def delete(uri, params = nil) # => Hash
  uri = URI(@host + uri)
  puts 'DELETE URL:', uri if $DEBUG
  uri.query = String.encode_www_form params if params
  error RestClient.delete(uri.to_s)
rescue RestClient::Exceptions::OpenTimeout
  retry
rescue RestClient::BadGateway
  retry
end

#get(uri, params = nil) ⇒ Object

get url

uri: String params (optional): Hash, url query



21
22
23
24
25
26
27
28
29
30
# File 'lib/matrix_dbus/network.rb', line 21

def get(uri, params = nil) # => Hash
  uri = URI(@host + uri)
  uri.query = URI.encode_www_form params if params
  puts 'GET URL:', uri if $DEBUG
  error RestClient.get(uri.to_s)
rescue RestClient::Exceptions::OpenTimeout
  retry
rescue RestClient::BadGateway
  retry
end

#post(uri, body) ⇒ Object

post json to url

uri: String body: Hash, post body



36
37
38
39
40
41
42
43
44
# File 'lib/matrix_dbus/network.rb', line 36

def post(uri, body)
  uri = @host + uri
  puts 'POST URL:', uri if $DEBUG
  error RestClient.post(uri.to_s, body.to_json, content_type: :json)
rescue RestClient::Exceptions::OpenTimeout
  retry
rescue RestClient::BadGateway
  retry
end

#post_raw(uri, body) ⇒ Object

post raw to url

uri: String body: Anything



50
51
52
53
54
55
56
57
58
# File 'lib/matrix_dbus/network.rb', line 50

def post_raw(uri, body)
  uri = URI(@host) + URI(uri)
  puts 'POST URL:', uri if $DEBUG
  error RestClient.post(uri.to_s, body)
rescue RestClient::Exceptions::OpenTimeout
  retry
rescue RestClient::BadGateway
  retry
end

#put(uri, body) ⇒ Object

put to url

uri: String body: Hash, put body



64
65
66
67
68
69
70
71
72
# File 'lib/matrix_dbus/network.rb', line 64

def put(uri, body)
  uri = @host + uri
  puts 'PUT URL:', uri.to_s if $DEBUG
  error RestClient.put(uri.to_s, body.to_json, content_type: :json)
rescue RestClient::Exceptions::OpenTimeout
  retry
rescue RestClient::BadGateway
  retry
end