Class: RubyIsds::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_isds/request.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Request

Returns a new instance of Request.



3
4
5
6
7
# File 'lib/ruby_isds/request.rb', line 3

def initialize(params = {})
  params.each do |k, v|
    instance_variable_set("@#{k}", v)
  end if params.any?
end

Class Method Details

.call(params = {}) ⇒ Object



9
10
11
# File 'lib/ruby_isds/request.rb', line 9

def self.call(params = {})
  new(params).call
end

Instance Method Details

#api_urlObject

relative part of url because they differ…

domain such as https://ws1.czebox.cz is take from config as such:
  `RubyIsds.configuration.api_domain`
it includes `https://` so only ending is supposed to be here

Raises:

  • (NotImplementedError)


101
102
103
# File 'lib/ruby_isds/request.rb', line 101

def api_url
  raise NotImplementedError, "#{self.class} must implement #api_url!"
end

#bodyObject

Each subclass must implement

this is whole body of message which differs and also needs to be
accompanied with att_accessors

Raises:

  • (NotImplementedError)


91
92
93
# File 'lib/ruby_isds/request.rb', line 91

def body
  raise NotImplementedError, "#{self.class} must implement #body!"
end

#callObject

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_isds/request.rb', line 15

def call
  uri = URI(full_url)

  request = Net::HTTP::Post.new(uri)
  default_headers.each { |k, v| request[k] = v }
  request.body = to_xml

  https = Net::HTTP.new(uri.hostname, uri.port)
  https.use_ssl = true
  https.ssl_version = :TLSv1_2_client
  if RubyIsds.configuration.cert_file
    https.verify_mode = OpenSSL::SSL::VERIFY_PEER
    https.cert = RubyIsds.configuration.cert_file
    https.key = RubyIsds.configuration.private_key
    https.verify_depth = 5
    request.basic_auth(RubyIsds.configuration.data_box, '')
  else
    request.basic_auth(
      RubyIsds.configuration.username,
      RubyIsds.configuration.password
    )
  end
  response = https.request(request)
  call_reponse_wrapper(response)
end

#call_reponse_wrapper(response) ⇒ Object

rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize



43
44
45
# File 'lib/ruby_isds/request.rb', line 43

def call_reponse_wrapper(response)
  response_wrapper.new(response)
end

#default_headersObject



47
48
49
50
51
52
# File 'lib/ruby_isds/request.rb', line 47

def default_headers
  {
    'Content-Type' =>  'text/xml;charset=UTF-8',
    'Accept-Encoding' => 'gzip,deflate'
  }
end

#envelope_namespacesObject



65
66
67
68
69
70
# File 'lib/ruby_isds/request.rb', line 65

def envelope_namespaces
  {
    'xmlns:soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/',
    'xmlns:v20' => xml_url
  }
end

#full_urlObject



72
73
74
# File 'lib/ruby_isds/request.rb', line 72

def full_url
  "#{RubyIsds.configuration.api_domain}#{api_url}"
end

#response_wrapperObject

Here you can change which response object will wrap the reponse

reponse object has to inherit from `::RubyIsds::Response`


116
117
118
# File 'lib/ruby_isds/request.rb', line 116

def response_wrapper
  ::RubyIsds::Response
end

#to_xmlObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby_isds/request.rb', line 54

def to_xml
  Nokogiri::XML::Builder.new(encoding: 'utf-8') do |xml|
    xml[:soapenv].Envelope(envelope_namespaces) do
      xml[:soapenv].Header
      xml[:soapenv].Body do
        body(xml)
      end
    end
  end.to_xml
end

#values(xml) ⇒ Object

each subclass needs to have availbale attrs put in constant ‘ATTRS`



79
80
81
82
83
84
# File 'lib/ruby_isds/request.rb', line 79

def values(xml)
  self.class::ATTRS.each do |attribute|
    value = instance_variable_get("@#{attribute}") || ''
    xml[:v20].public_send(attribute, value)
  end
end

#xml_urlObject

URL will differ based on endpoint that it accesses



108
109
110
# File 'lib/ruby_isds/request.rb', line 108

def xml_url
  "https://#{RubyIsds.configuration.xml_url}"
end