Class: Redminer::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/redminer/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, access_key, options = {}) ⇒ Server

Returns a new instance of Server.



6
7
8
9
10
11
12
# File 'lib/redminer/server.rb', line 6

def initialize(host, access_key, options = {})
  options = {:port => 80}.merge(options)
  @http = Net::HTTP.new(host, options[:port])
  @access_key = access_key
  @verbose = options[:verbose]
  @reqtrace = options[:reqtrace]
end

Instance Attribute Details

#access_keyObject

Returns the value of attribute access_key.



3
4
5
# File 'lib/redminer/server.rb', line 3

def access_key
  @access_key
end

#httpObject

Returns the value of attribute http.



3
4
5
# File 'lib/redminer/server.rb', line 3

def http
  @http
end

#reqtraceObject

Returns the value of attribute reqtrace.



4
5
6
# File 'lib/redminer/server.rb', line 4

def reqtrace
  @reqtrace
end

#verboseObject

Returns the value of attribute verbose.



4
5
6
# File 'lib/redminer/server.rb', line 4

def verbose
  @verbose
end

Instance Method Details

#current_userObject



54
55
56
# File 'lib/redminer/server.rb', line 54

def current_user
  Redminer::User.current(self)
end

#delete(path, params = nil, &block) ⇒ Object



52
# File 'lib/redminer/server.rb', line 52

def delete(path, params = nil, &block); request(path, params, Net::HTTP::Delete, &block) end

#get(path, params = nil, &block) ⇒ Object



49
# File 'lib/redminer/server.rb', line 49

def get(path, params = nil, &block); request(path, params, &block) end

#hash_to_querystring(hash) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/redminer/server.rb', line 38

def hash_to_querystring(hash)
  hash.keys.inject('') do |query_string, key|
    value = case hash[key]
      when Hash then hash[key].to_json
      else hash[key].to_s
    end
    query_string << '&' unless key == hash.keys.first
    query_string << "#{URI.encode(key.to_s)}=#{URI.encode(value)}"
  end
end

#issue(issue_key = nil) ⇒ Object



66
67
68
# File 'lib/redminer/server.rb', line 66

def issue(issue_key = nil)
  Redminer::Issue.new(self, issue_key)
end

#post(path, params = nil, &block) ⇒ Object



51
# File 'lib/redminer/server.rb', line 51

def post(path, params = nil, &block); request(path, params, Net::HTTP::Post, &block) end

#put(path, params = nil, &block) ⇒ Object



50
# File 'lib/redminer/server.rb', line 50

def put(path, params = nil, &block); request(path, params, Net::HTTP::Put, &block) end

#request(path, params = nil, obj = Net::HTTP::Get) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/redminer/server.rb', line 14

def request(path, params = nil, obj = Net::HTTP::Get)
  puts "requesting... #{http.address}:#{http.port}#{path} by #{obj}" if verbose
  puts caller.join("\n  ") if reqtrace
  req = obj.new(path)
  req.add_field('X-Redmine-API-Key', access_key)
  req.body = hash_to_querystring(params) if not params.nil? and not params.empty?
  begin
    if block_given?
      yield http.request(req)
    else
      response = http.request(req)
      if response.code != "200"
        raise "fail to request #{response.code}:#{response.message}"
      end
      return {} if response.body.nil? or response.body.strip.empty?
      JSON.parse(response.body)
    end
  rescue Timeout::Error => e
    raise e, "#{host}:#{port} timeout error", e.backtrace
  rescue JSON::ParserError => e
    raise e, "response json parsing error", e.backtrace
  end
end

#working?Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
# File 'lib/redminer/server.rb', line 58

def working?
  begin
    current_user and true
  rescue
    false
  end
end