Class: DeltavistaCrifDvaInterface::RestfulService

Inherits:
Object
  • Object
show all
Defined in:
lib/deltavista_crif_dva_interface/restful_service.rb

Direct Known Subclasses

DeltaVistaService

Instance Method Summary collapse

Instance Method Details

#check_response!(response) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/deltavista_crif_dva_interface/restful_service.rb', line 48

def check_response!(response)
  unless valid_response? response
    if response.class == Net::HTTPNotFound
      ex = NotFoundError.new response.class, response.body
      raise ex if response.class == Net::HTTPNotFound
    end
    ex = ServiceNotAvailableError.new response.class, response.body
    raise ex
  end
  response
end

#create_http_request(url) ⇒ Net::HTTP

Generates new HTTP request

Parameters:

  • url (URI)

    request URI

Returns:

  • (Net::HTTP)

    HTTP request



39
40
41
42
43
44
45
46
# File 'lib/deltavista_crif_dva_interface/restful_service.rb', line 39

def create_http_request(url)
  http = Net::HTTP.new(url.host, url.port)
  if url.scheme == 'https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http
end

#get_http_response(url_string) {|resp.body| ... } ⇒ Object

Yields:

  • (resp.body)


7
8
9
10
11
# File 'lib/deltavista_crif_dva_interface/restful_service.rb', line 7

def get_http_response(url_string)
  resp = Net::HTTP.get_response(to_url(url_string))
  check_response! resp
  yield resp.body
end

#post_http_request(url_string, data, headers = {}) {|resp.body| ... } ⇒ Object

Yields:

  • (resp.body)


13
14
15
16
17
18
19
# File 'lib/deltavista_crif_dva_interface/restful_service.rb', line 13

def post_http_request(url_string, data, headers = {})
  url = to_url url_string
  http = create_http_request url
  resp = http.post(url.path, data, headers)
  check_response! resp
  yield resp.body
end

#put_http_request(url_string, data, headers = {}) {|resp.body| ... } ⇒ Object

Yields:

  • (resp.body)


21
22
23
24
25
26
27
# File 'lib/deltavista_crif_dva_interface/restful_service.rb', line 21

def put_http_request(url_string, data, headers = {})
  url = to_url url_string
  http = create_http_request url
  resp = http.put(url.path, data, headers)
  check_response! resp.body
  yield resp.body
end

#to_url(url_string) ⇒ URI

Generates URI depending on URI string

Parameters:

  • url_string (String)

    URI string

Returns:

  • (URI)

    URI object



32
33
34
# File 'lib/deltavista_crif_dva_interface/restful_service.rb', line 32

def to_url(url_string)
  URI.parse(url_string)
end

#valid_response?(response) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/deltavista_crif_dva_interface/restful_service.rb', line 60

def valid_response?(response)
  response.class == Net::HTTPOK ? true : false
end