Class: YouTrack::Client::Real

Inherits:
Object
  • Object
show all
Defined in:
lib/you_track/client/real.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Real

Returns a new instance of Real.



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
# File 'lib/you_track/client/real.rb', line 4

def initialize(options={})
  options.merge!(YouTrack.defaults)

  requires(options, :url, :username, :password)

  @url           = URI.parse(options[:url])
  adapter        = options[:adapter] || Faraday.default_adapter
  logger         = options[:logger]  || Logger.new(nil)
  custom_builder = options[:builder] || lambda { |*| }

  @username, @password = options.values_at(:username, :password)
  @authenticated, @authenticating = false, false

  @authenticate_mutex = Mutex.new

  connection_options = options[:connection_options] || {}

  @connection = Faraday.new({url: @url}.merge(connection_options)) do |builder|
    # response
    builder.response :xml, content_type: /\bxml$/

    # request
    builder.request :retry,
      :max                 => 5,
      :interval            => 1,
      :interval_randomness => 0.1,
      :backoff_factor      => 2

    builder.use :cookie_jar
    builder.request :multipart

    builder.use Faraday::Response::RaiseError
    builder.response :logger, logger

    custom_builder.call(builder)

    builder.adapter(*adapter)
  end
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



2
3
4
# File 'lib/you_track/client/real.rb', line 2

def adapter
  @adapter
end

#authenticatedObject (readonly)

Returns the value of attribute authenticated.



2
3
4
# File 'lib/you_track/client/real.rb', line 2

def authenticated
  @authenticated
end

#connectionObject (readonly)

Returns the value of attribute connection.



2
3
4
# File 'lib/you_track/client/real.rb', line 2

def connection
  @connection
end

#urlObject (readonly)

Returns the value of attribute url.



2
3
4
# File 'lib/you_track/client/real.rb', line 2

def url
  @url
end

#usernameObject (readonly)

Returns the value of attribute username.



2
3
4
# File 'lib/you_track/client/real.rb', line 2

def username
  @username
end

Instance Method Details

#authenticate!Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/you_track/client/real.rb', line 51

def authenticate!
  # @note first request gets the cookie
  if !@authenticated && !@authenticating

    @authenticate_mutex.synchronize {
      next if @authenticated
      @authenticating = true

      begin
        (@username, @password)
      ensure
        @authenticating = false
      end

      @authenticated = true
    }
  end
end

#current_userObject



102
103
104
# File 'lib/you_track/client/real.rb', line 102

def current_user
  @current_user ||= users.current
end

#request(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/you_track/client/real.rb', line 70

def request(options={})
  authenticate!

  method    = options[:method] || :get
  query     = options[:query]
  url       = URI.parse(options[:url] || File.join(self.url.to_s, "/rest", options.fetch(:path)))
  url.query = query.map { |k,v| "#{URI.escape(k)}=#{URI.escape(v.to_s)}" }.join('&') if query
  params    = options[:params] || {}
  body      = options[:body]
  headers   = options[:headers] || {}
  parser    = options[:parser]

  headers["Content-Type"] ||= if body.nil?
                                if !params.empty?
                                  "application/x-www-form-urlencoded"
                                else # Rails infers a Content-Type and we must set it here for the canonical string to match
                                  "text/plain"
                                end
                              end

  response = @connection.send(method) do |req|
    req.url(url.to_s)
    req.headers.merge!(headers)
    req.params.merge!(params)
    req.body = body
  end

  response.env.body = parser.new(response.body).parse if parser

  response
end

#requires(options, *required) ⇒ Object

Raises:

  • (RuntimeError)


44
45
46
47
48
49
# File 'lib/you_track/client/real.rb', line 44

def requires(options, *required)
  missing = required.map do |required_param|
    required_param if options[required_param].nil?
  end.compact
  raise RuntimeError, "Missing required options: #{missing.inspect}" unless missing.empty?
end