Class: Bizside::Redmine::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/bizside/redmine/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(overrides = {}) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
# File 'lib/bizside/redmine/connection.rb', line 7

def initialize(overrides = {})
  @host = overrides[:host] || Bizside::Redmine::Client.config[:host]
  @api_key = overrides[:api_key] || Bizside::Redmine::Client.config[:api_key]
  @verify_ssl = overrides.has_key?(:verify_ssl) ?
    overrides[:verify_ssl] :
    Bizside::Redmine::Client.config[:verify_ssl]
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/bizside/redmine/connection.rb', line 5

def api_key
  @api_key
end

#hostObject (readonly)

Returns the value of attribute host.



5
6
7
# File 'lib/bizside/redmine/connection.rb', line 5

def host
  @host
end

#verify_sslObject (readonly)

Returns the value of attribute verify_ssl.



5
6
7
# File 'lib/bizside/redmine/connection.rb', line 5

def verify_ssl
  @verify_ssl
end

Instance Method Details

#get(path, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/bizside/redmine/connection.rb', line 15

def get(path, params = {})
  begin
    connection.get(path, params) do |request|
      request.headers['X-Redmine-API-Key'] = api_key
    end
  rescue Faraday::ConnectionFailed
    raise 'Could not connect to the Redmine server.'
  end
end

#post(path, params = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/bizside/redmine/connection.rb', line 25

def post(path, params = {})
  begin
    connection.post(path) do |request|
      request.headers['X-Redmine-API-Key'] = api_key
      request.headers['Content-Type'] = 'application/json'
      request.body = params.to_json
    end
  rescue Faraday::ConnectionFailed
    raise 'Could not connect to the Redmine server.'
  end
end

#post_or_put(path, content) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/bizside/redmine/connection.rb', line 49

def post_or_put(path, content)
  begin
    connection.put(path) do |request|
      request.headers['X-Redmine-API-Key'] = api_key
      request.headers['Content-Type'] = 'application/xml'
      request.body = content
    end
  rescue Faraday::ConnectionFailed
    raise 'Could not connect to the Redmine server.'
  end
end

#post_with_multipart(path, params = {}) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bizside/redmine/connection.rb', line 61

def post_with_multipart(path, params = {})
  begin
    connection(:multipart => true).post(path) do |request|
      request.headers['X-Redmine-API-Key'] = api_key
      request.headers['Content-Type'] = 'application/octet-stream'
      request.body = File.binread(params[:file].path)
    end
  rescue Faraday::ConnectionFailed
    raise 'Could not connect to the Redmine server.'
  end
end

#put(path, params = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bizside/redmine/connection.rb', line 37

def put(path, params = {})
  begin
    connection.put(path) do |request|
      request.headers['X-Redmine-API-Key'] = api_key
      request.headers['Content-Type'] = 'application/json'
      request.body = params.to_json
    end
  rescue Faraday::ConnectionFailed
    raise 'Could not connect to the Redmine server.'
  end
end