Class: ZammadAPI::Transport

Inherits:
Object
  • Object
show all
Defined in:
lib/zammad_api/transport.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, logger) ⇒ Transport

Returns a new instance of Transport.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/zammad_api/transport.rb', line 9

def initialize(config, logger)
  @logger = logger
  @logger.debug "Transport to #{config[:url]} with #{config[:user]}:#{config[:password]}"
  @conn = Faraday.new(url: config[:url]) do |faraday|
    #faraday.request  :url_encoded             # form-encode POST params
    #faraday.response :logger                  # log requests to STDOUT
    faraday.adapter Faraday.default_adapter  # make requests with Net::HTTP
  end
  @conn.headers[:user_agent] = 'Zammad API Ruby'
  if config[:http_token] && !config[:http_token].empty?
    @conn.token_auth(config[:http_token])
  elsif config[:oauth2_token] && !config[:oauth2_token].empty?
    @conn.authorization :Bearer, config[:oauth2_token]
  else
    @conn.basic_auth(config[:user], config[:password])
  end
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



7
8
9
# File 'lib/zammad_api/transport.rb', line 7

def password
  @password
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/zammad_api/transport.rb', line 7

def url
  @url
end

#userObject

Returns the value of attribute user.



7
8
9
# File 'lib/zammad_api/transport.rb', line 7

def user
  @user
end

Instance Method Details

#delete(param) ⇒ Object



57
58
59
60
61
62
# File 'lib/zammad_api/transport.rb', line 57

def delete(param)
  @logger.debug "DELETE: #{@url}#{param[:url]}"
  response = @conn.delete param[:url]
  @logger.debug "Response: #{response.body}"
  response
end

#get(param) ⇒ Object



27
28
29
30
31
# File 'lib/zammad_api/transport.rb', line 27

def get(param)
  @logger.debug "GET: #{@url}#{param[:url]}"
  response = @conn.get param[:url]
  response
end

#post(param) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zammad_api/transport.rb', line 33

def post(param)
  @logger.debug "POST: #{@url}#{param[:url]}"
  @logger.debug "Params: #{param[:params].inspect}"
  response = @conn.post do |req|
    req.url param[:url]
    req.headers['Content-Type'] = 'application/json'
    req.body = param[:params].to_json
  end
  @logger.debug "Response: #{response.body}"
  response
end

#put(param) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zammad_api/transport.rb', line 45

def put(param)
  @logger.debug "PUT: #{@url}#{param[:url]}"
  @logger.debug "Params: #{param[:params].inspect}"
  response = @conn.put do |req|
    req.url param[:url]
    req.headers['Content-Type'] = 'application/json'
    req.body = param[:params].to_json
  end
  @logger.debug "Response: #{response.body}"
  response
end