class Redminer::Server

Attributes

access_key[RW]
http[RW]
reqtrace[RW]
verbose[RW]

Public Class Methods

new(host, access_key, options = {}) click to toggle source
# 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

Public Instance Methods

current_user() click to toggle source
# File lib/redminer/server.rb, line 54
def current_user
  Redminer::User.current(self)
end
delete(path, params = nil, &block) click to toggle source
# 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) click to toggle source
# File lib/redminer/server.rb, line 49
def get(path, params = nil, &block); request(path, params, &block) end
hash_to_querystring(hash) click to toggle source
# 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) click to toggle source
# File lib/redminer/server.rb, line 66
def issue(issue_key = nil)
  Redminer::Issue.new(self, issue_key)
end
post(path, params = nil, &block) click to toggle source
# 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) click to toggle source
# 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) { |request| ... } click to toggle source
# 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?() click to toggle source
# File lib/redminer/server.rb, line 58
def working?
  begin
    current_user and true
  rescue
    false
  end
end