Class: Gitlab::FogbugzImport::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/fogbugz_import/interface.rb

Constant Summary collapse

RequestError =
Class.new(StandardError)
InitializationError =
Class.new(StandardError)
AuthenticationError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Interface

Returns a new instance of Interface.



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/fogbugz_import/interface.rb', line 12

def initialize(options = {})
  @options = {}.merge(options)
  raise InitializationError, 'Must supply URI (e.g. https://fogbugz.company.com)' unless options[:uri]

  @token = options[:token] if options[:token]
  # Custom adapter to validate the URL before each request
  # This way we avoid DNS rebinds or other unsafe requests
  @http = HttpAdapter.new(uri: options[:uri], ca_file: options[:ca_file])
  # Custom adapter to validate size of incoming XML before
  # attempting to parse it.
  @xml = XmlAdapter
end

Instance Attribute Details

#httpObject

Returns the value of attribute http.



10
11
12
# File 'lib/gitlab/fogbugz_import/interface.rb', line 10

def http
  @http
end

#optionsObject

Returns the value of attribute options.



10
11
12
# File 'lib/gitlab/fogbugz_import/interface.rb', line 10

def options
  @options
end

#tokenObject

Returns the value of attribute token.



10
11
12
# File 'lib/gitlab/fogbugz_import/interface.rb', line 10

def token
  @token
end

#xmlObject

Returns the value of attribute xml.



10
11
12
# File 'lib/gitlab/fogbugz_import/interface.rb', line 10

def xml
  @xml
end

Instance Method Details

#authenticateObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gitlab/fogbugz_import/interface.rb', line 25

def authenticate
  response = @http.request(
    :logon,
    params: {
      email: @options[:email],
      password: @options[:password]
    })

  parsed_response = @xml.parse(response)
  @token ||= parsed_response['token']
  raise AuthenticationError, parsed_response['error'] if @token.blank?

  @token
end

#command(action, parameters = {}) ⇒ Object

Raises:



40
41
42
43
44
45
46
47
48
# File 'lib/gitlab/fogbugz_import/interface.rb', line 40

def command(action, parameters = {})
  raise RequestError, 'No token available, #authenticate first' unless @token

  parameters[:token] = @token

  response = @http.request action, params: parameters.merge(options[:params] || {})

  @xml.parse(response)
end