Class: Eso::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/eso/service.rb

Direct Known Subclasses

Conductor

Constant Summary collapse

CONTENT_TYPE_JSON =
'application/json; charset-utf-8'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port: 3780, nsc:) ⇒ Service

Returns a new instance of Service.



11
12
13
14
15
# File 'lib/eso/service.rb', line 11

def initialize(host:, port: 3780, nsc:)
  @host = host
  @port = port
  @nexpose_console = nsc
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



3
4
5
# File 'lib/eso/service.rb', line 3

def host
  @host
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/eso/service.rb', line 5

def port
  @port
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/eso/service.rb', line 7

def url
  @url
end

Instance Method Details

#add_nexpose_session(request:) ⇒ Object



58
59
60
61
62
# File 'lib/eso/service.rb', line 58

def add_nexpose_session(request:)
  request.add_field('nexposeCCSessionID', @nexpose_console.session_id)
  request.add_field('Cookie', "nexposeCCSessionID=#{@nexpose_console.session_id}")
  request.add_field('X-Requested-With', 'XMLHttpRequest')
end

#delete(url:, content_type: CONTENT_TYPE_JSON) ⇒ Object



37
38
39
40
41
# File 'lib/eso/service.rb', line 37

def delete(url:, content_type: CONTENT_TYPE_JSON)
  delete = Net::HTTP::Delete.new(url)
  delete.set_content_type(content_type)
  request(request: delete)
end

#get(url:, content_type: CONTENT_TYPE_JSON) ⇒ Object



17
18
19
20
21
# File 'lib/eso/service.rb', line 17

def get(url:, content_type: CONTENT_TYPE_JSON)
  get = Net::HTTP::Get.new(url)
  get.set_content_type(content_type)
  request(request: get)
end

#http(timeout:) ⇒ Object



43
44
45
46
47
48
# File 'lib/eso/service.rb', line 43

def http(timeout:)
  http = Net::HTTP.new(@host, @port)
  http.read_timeout = timeout if timeout
  http.use_ssl = false
  http
end

#https(timeout:) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/eso/service.rb', line 50

def https(timeout:)
  http = Net::HTTP.new(@host, @port)
  http.read_timeout = timeout if timeout
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  http
end

#post(url:, payload: nil, content_type: CONTENT_TYPE_JSON) ⇒ Object



30
31
32
33
34
35
# File 'lib/eso/service.rb', line 30

def post(url:, payload: nil, content_type: CONTENT_TYPE_JSON)
  post = Net::HTTP::Post.new(url)
  post.set_content_type(content_type)
  post.body = payload.to_s if payload
  request(request: post)
end

#put(url:, payload:, content_type: CONTENT_TYPE_JSON) ⇒ Object



23
24
25
26
27
28
# File 'lib/eso/service.rb', line 23

def put(url:, payload:, content_type: CONTENT_TYPE_JSON)
  put = Net::HTTP::Put.new(url)
  put.set_content_type(content_type)
  put.body = payload.to_s if payload
  request(request: put)
end

#request(request:, timeout: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/eso/service.rb', line 64

def request(request:, timeout: nil)
  http = https(timeout: timeout)
  add_nexpose_session(request: request)
  response = http.request(request)
  case response
    when Net::HTTPOK, Net::HTTPCreated
      rv = nil
      if response.content_type == "application/json" && !response.body.empty?
        json_data = JSON.parse(response.body, symbolize_names: true)
        json_data[:data].nil? ? rv = json_data : rv = json_data[:data]
      end
      rv
    when Net::HTTPForbidden
      raise "Access denied. Response was #{response.body}"
    else
      raise "There was an error sending the request. Response was #{response.body}"
  end
end