Class: YouTrack::Client::Real
- Inherits:
-
Object
- Object
- YouTrack::Client::Real
- Defined in:
- lib/you_track/client/real.rb
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#authenticated ⇒ Object
readonly
Returns the value of attribute authenticated.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #authenticate! ⇒ Object
- #current_user ⇒ Object
-
#initialize(options = {}) ⇒ Real
constructor
A new instance of Real.
- #request(options = {}) ⇒ Object
- #requires(options, *required) ⇒ Object
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(={}) .merge!(YouTrack.defaults) requires(, :url, :username, :password) @url = URI.parse([:url]) adapter = [:adapter] || Faraday.default_adapter logger = [:logger] || Logger.new(nil) custom_builder = [:builder] || lambda { |*| } @username, @password = .values_at(:username, :password) @authenticated, @authenticating = false, false @authenticate_mutex = Mutex.new = [:connection_options] || {} @connection = Faraday.new({url: @url}.merge()) 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
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
2 3 4 |
# File 'lib/you_track/client/real.rb', line 2 def adapter @adapter end |
#authenticated ⇒ Object (readonly)
Returns the value of attribute authenticated.
2 3 4 |
# File 'lib/you_track/client/real.rb', line 2 def authenticated @authenticated end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
2 3 4 |
# File 'lib/you_track/client/real.rb', line 2 def connection @connection end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
2 3 4 |
# File 'lib/you_track/client/real.rb', line 2 def url @url end |
#username ⇒ Object (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 login(@username, @password) ensure @authenticating = false end @authenticated = true } end end |
#current_user ⇒ Object
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(={}) authenticate! method = [:method] || :get query = [:query] url = URI.parse([:url] || File.join(self.url.to_s, "/rest", .fetch(:path))) url.query = query.map { |k,v| "#{URI.escape(k)}=#{URI.escape(v.to_s)}" }.join('&') if query params = [:params] || {} body = [:body] headers = [:headers] || {} parser = [: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
44 45 46 47 48 49 |
# File 'lib/you_track/client/real.rb', line 44 def requires(, *required) missing = required.map do |required_param| required_param if [required_param].nil? end.compact raise RuntimeError, "Missing required options: #{missing.inspect}" unless missing.empty? end |