File: interface.rb

package info (click to toggle)
ruby-fogbugz 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: ruby: 199; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
40
41
42
43
44
45
46
module Fogbugz
  class Interface
    class RequestError < StandardError; end
    class InitializationError < StandardError; end

    attr_accessor :options, :http, :xml, :token

    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]
      @http = Fogbugz.adapter[:http].new(uri: options[:uri], ca_file: options[:ca_file])
      @xml = Fogbugz.adapter[:xml]
    end

    def authenticate
      response = @http.request :logon,
                               params: {
                                 email: @options[:email],
                                 password: @options[:password]
                               }
      begin
        @token ||= @xml.parse(response)['token']
        if @token.nil? || @token == ''
          raise Fogbugz::AuthenticationException, @xml.parse(response)['error']
        end
      rescue REXML::ParseException
        raise Fogbugz::AuthenticationException, "Looks like there was an issue with authentication (to #{@options[:uri]} as #{@options[:email]}) - probably the host/url."
      end
      @token
    end

    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
  end

  class AuthenticationException < Exception
  end
end