Class: Zendesk2::Real
- Inherits:
-
Object
- Object
- Zendesk2::Real
- Defined in:
- lib/zendesk2/real.rb
Instance Attribute Summary collapse
-
#jwt_token ⇒ Object
Returns the value of attribute jwt_token.
-
#last_request ⇒ Object
Returns the value of attribute last_request.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#token ⇒ Object
Returns the value of attribute token.
-
#url ⇒ Object
Returns the value of attribute url.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #connection ⇒ Object
-
#initialize(options = {}) ⇒ Real
constructor
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #request(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Real
rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/zendesk2/real.rb', line 6 def initialize( = {}) url = [:url] || Zendesk2.defaults[:url] @url = (URI.parse(url).to_s if url) @logger = [:logger] || Logger.new(nil) @adapter = [:adapter] || Faraday.default_adapter @username = [:username] || Zendesk2.defaults[:username] @token = .fetch(:token, Zendesk2.defaults[:token]) password = [:password] || Zendesk2.defaults[:password] @auth_token = password || @token @auth_id = "#{@username}/token" if @auth_token == @token @jwt_token = [:jwt_token] raise 'Missing required options: :url' unless @url raise 'Missing required options: :username' unless @auth_id raise 'Missing required options: :password or :token' unless password || @token connection end |
Instance Attribute Details
#jwt_token ⇒ Object
Returns the value of attribute jwt_token.
3 4 5 |
# File 'lib/zendesk2/real.rb', line 3 def jwt_token @jwt_token end |
#last_request ⇒ Object
Returns the value of attribute last_request.
3 4 5 |
# File 'lib/zendesk2/real.rb', line 3 def last_request @last_request end |
#logger ⇒ Object
Returns the value of attribute logger.
3 4 5 |
# File 'lib/zendesk2/real.rb', line 3 def logger @logger end |
#token ⇒ Object
Returns the value of attribute token.
3 4 5 |
# File 'lib/zendesk2/real.rb', line 3 def token @token end |
#url ⇒ Object
Returns the value of attribute url.
3 4 5 |
# File 'lib/zendesk2/real.rb', line 3 def url @url end |
#username ⇒ Object
Returns the value of attribute username.
3 4 5 |
# File 'lib/zendesk2/real.rb', line 3 def username @username end |
Instance Method Details
#connection ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/zendesk2/real.rb', line 28 def connection @connection ||= Faraday.new(url: @url) do |connection| # response connection.use Faraday::Request::BasicAuthentication, @auth_id, @auth_token connection.use Faraday::Response::RaiseError connection.response :json, content_type: /\bjson/ # request connection.request :multipart connection.request :json # idempotency connection.request :retry, max: 30, interval: 1, interval_randomness: 0.2, backoff_factor: 2 # rate limit connection.use Zendesk2::RateLimit, logger: @logger connection.use Zendesk2::Logger, @logger connection.adapter(*@adapter) end end |
#request(options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/zendesk2/real.rb', line 54 def request( = {}) method = [:method] || :get url = [:url] || File.join(@url, '/api/v2', [:path]) params = [:params] || {} body = [:body] headers = { 'User-Agent' => Zendesk2::USER_AGENT }.merge([:headers] || {}) connection.send(method) do |req| req.url(url) req.headers.merge!(headers) req.params.merge!(params) req.body = @last_request = body end rescue Faraday::ConnectionFailed raise rescue Faraday::Error::ClientError => e raise Zendesk2::Error, e end |