redminer 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -2,6 +2,6 @@ require 'net/http'
2
2
  require 'uri'
3
3
  require 'json'
4
4
  require 'yaml'
5
- require 'redminer/base'
6
- require 'redminer/issue'
7
-
5
+ require File.dirname(__FILE__) + '/redminer/base'
6
+ require File.dirname(__FILE__) + '/redminer/issue'
7
+ require File.dirname(__FILE__) + '/redminer/server'
@@ -1,46 +1,22 @@
1
1
  module Redminer
2
2
  class Base
3
- attr_accessor :server, :access_key
3
+ attr_accessor :server
4
4
 
5
5
  def initialize(host, access_key, options = {})
6
- options = {:port => 80}.merge(options)
7
- @server = Net::HTTP.new(host, options[:port])
8
- @access_key = access_key
6
+ @server = Server.new(host, access_key, options)
9
7
  end
10
8
 
11
- def request(path, params = nil, obj = Net::HTTP::Get)
12
- req = obj.new(path)
13
- req.add_field('X-Redmine-API-Key', access_key)
14
- req.body = hash_to_querystring(params) if not params.nil? and not params.empty?
15
- begin
16
- if block_given?
17
- yield server.request(req)
18
- else
19
- server.request(req).body
9
+ def all=(hash)
10
+ hash.each_pair { |k, v|
11
+ if respond_to?("#{k}=")
12
+ send("#{k}=", v)
20
13
  end
21
- rescue Timeout::Error
22
- raise "#{host}:#{port} does not respond"
23
- end
14
+ }
15
+ self
24
16
  end
25
17
 
26
- def hash_to_querystring(hash)
27
- hash.keys.inject('') do |query_string, key|
28
- value = case hash[key]
29
- when Hash then hash[key].to_json
30
- else hash[key].to_s
31
- end
32
- query_string << '&' unless key == hash.keys.first
33
- query_string << "#{URI.encode(key.to_s)}=#{URI.encode(value)}"
34
- end
35
- end
36
-
37
- def get(path, params = nil, &block); request(path, params, &block) end
38
- def put(path, params = nil, &block); request(path, params, Net::HTTP::Put, &block) end
39
- def post(path, params = nil, &block); request(path, params, Net::HTTP::Post, &block) end
40
- def delete(path, params = nil, &block); request(path, params, Net::HTTP::Delete, &block) end
41
-
42
- def issue(issue_key = nil)
43
- Redminer::Issue.new(self, issue_key)
18
+ def method_missing(m, *args, &blk)
19
+ @server.send(m, *args, &blk)
44
20
  end
45
21
  end
46
22
  end
@@ -1,56 +1,48 @@
1
1
  module Redminer
2
2
  class Issue < Redminer::Base
3
- attr_reader :base, :id
4
- attr_accessor :author, :project
5
- attr_accessor :tracker, :status, :priority, :category
6
- attr_accessor :subject, :description
7
- attr_accessor :start_date, :due_date
8
- attr_accessor :created_on, :updated_on
3
+ attr_reader :id
4
+ attr_accessor :author, :project,
5
+ :tracker, :status, :priority, :category,
6
+ :subject, :description,
7
+ :start_date, :due_date,
8
+ :created_on, :updated_on
9
9
 
10
- def initialize(base, id = nil)
11
- @base = base
10
+ def initialize(server, id = nil)
11
+ @server = server
12
12
  unless id.nil?
13
13
  @id = id
14
- retrieve
14
+ self.retrieve
15
15
  end
16
16
  end
17
17
 
18
18
  def retrieve
19
- response = base.get("/issues/#{id}.json")
19
+ response = server.get("/issues/#{id}.json")
20
20
  raise "#{id} issue does not exists" if response.nil?
21
- origin = JSON.parse(response)["issue"]
22
- origin.each_pair { |k, v|
23
- if respond_to?("#{k}=")
24
- send("#{k}=", v)
25
- end
26
- }
21
+ origin = response["issue"]
22
+ self.all = origin
27
23
  self
28
24
  end
29
25
 
30
- def self.exists?(id)
31
- (self.new(id) and true) rescue false
32
- end
33
-
34
26
  def sync
35
27
  (@id.nil? ? create : update)
36
28
  end
37
29
 
38
30
  def craete
39
- params = {
40
- :subject => @subject,
41
- :description => @description,
42
- :project => @project
43
- }
44
- base.post("/issues.json", params)
31
+ server.post("/issues.json", to_hash)
45
32
  end
33
+
46
34
  def update(note = nil)
47
- params = {
48
- :subject => @subject,
49
- :description => @description,
50
- :project => @project
51
- }
52
- params.merge!(:notes => note) unless note.nil?
53
- base.put("/issues/#{id}.json", params)
35
+ params = to_hash.merge(:notes => note) unless note.nil?
36
+ server.put("/issues/#{id}.json", params)
54
37
  end
38
+
39
+ private
40
+ def to_hash
41
+ {
42
+ :subject => @subject,
43
+ :description => @description,
44
+ :project => @project
45
+ }
46
+ end
55
47
  end
56
48
  end
@@ -0,0 +1,70 @@
1
+ module Redminer
2
+ class Server
3
+ attr_accessor :http, :access_key
4
+ attr_accessor :verbose, :reqtrace
5
+
6
+ def initialize(host, access_key, options = {})
7
+ options = {:port => 80}.merge(options)
8
+ @http = Net::HTTP.new(host, options[:port])
9
+ @access_key = access_key
10
+ @verbose = options[:verbose]
11
+ @reqtrace = options[:reqtrace]
12
+ end
13
+
14
+ def request(path, params = nil, obj = Net::HTTP::Get)
15
+ puts "requesting... #{http.address}:#{http.port}#{path} by #{obj}" if verbose
16
+ puts caller.join("\n ") if reqtrace
17
+ req = obj.new(path)
18
+ req.add_field('X-Redmine-API-Key', access_key)
19
+ req.body = hash_to_querystring(params) if not params.nil? and not params.empty?
20
+ begin
21
+ if block_given?
22
+ yield http.request(req)
23
+ else
24
+ response = http.request(req)
25
+ if response.code != "200"
26
+ raise "fail to request #{response.code}:#{response.message}"
27
+ end
28
+ return {} if response.body.nil? or response.body.strip.empty?
29
+ JSON.parse(response.body)
30
+ end
31
+ rescue Timeout::Error => e
32
+ raise e, "#{host}:#{port} timeout error", e.backtrace
33
+ rescue JSON::ParserError => e
34
+ raise e, "response json parsing error", e.backtrace
35
+ end
36
+ end
37
+
38
+ def hash_to_querystring(hash)
39
+ hash.keys.inject('') do |query_string, key|
40
+ value = case hash[key]
41
+ when Hash then hash[key].to_json
42
+ else hash[key].to_s
43
+ end
44
+ query_string << '&' unless key == hash.keys.first
45
+ query_string << "#{URI.encode(key.to_s)}=#{URI.encode(value)}"
46
+ end
47
+ end
48
+
49
+ def get(path, params = nil, &block); request(path, params, &block) end
50
+ def put(path, params = nil, &block); request(path, params, Net::HTTP::Put, &block) end
51
+ def post(path, params = nil, &block); request(path, params, Net::HTTP::Post, &block) end
52
+ def delete(path, params = nil, &block); request(path, params, Net::HTTP::Delete, &block) end
53
+
54
+ def current_user
55
+ Redminer::User.current(self)
56
+ end
57
+
58
+ def working?
59
+ begin
60
+ current_user and true
61
+ rescue
62
+ false
63
+ end
64
+ end
65
+
66
+ def issue(issue_key = nil)
67
+ Redminer::Issue.new(self, issue_key)
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,19 @@
1
+ module Redminer
2
+ class User
3
+ attr_reader :id
4
+ attr_accessor :login, :firstname, :lastname, :mail
5
+
6
+ def initialize(server, id = nil)
7
+ @id = id
8
+ end
9
+
10
+ def self.current(server)
11
+ response = server.get('/users/current.json')
12
+ origin = response["user"]
13
+ c_user = self.new(server)
14
+ c_user.all = origin
15
+ c_user.instance_variable_get(:@id, origin["id"])
16
+ c_user
17
+ end
18
+ end
19
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "redminer"
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["insoul"]
12
- s.date = "2012-04-05"
12
+ s.date = "2012-04-10"
13
13
  s.description = "Redmine rest api client"
14
14
  s.email = "insoul@thinkreals.com"
15
15
  s.extra_rdoc_files = [
@@ -26,6 +26,8 @@ Gem::Specification.new do |s|
26
26
  "lib/redminer.rb",
27
27
  "lib/redminer/base.rb",
28
28
  "lib/redminer/issue.rb",
29
+ "lib/redminer/server.rb",
30
+ "lib/redminer/user.rb",
29
31
  "redminer.gemspec",
30
32
  "test/helper.rb",
31
33
  "test/test_redminer.rb"
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redminer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 1
10
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - insoul
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-04-05 00:00:00 Z
18
+ date: 2012-04-10 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  version_requirements: &id001 !ruby/object:Gem::Requirement
@@ -125,6 +125,8 @@ files:
125
125
  - lib/redminer.rb
126
126
  - lib/redminer/base.rb
127
127
  - lib/redminer/issue.rb
128
+ - lib/redminer/server.rb
129
+ - lib/redminer/user.rb
128
130
  - redminer.gemspec
129
131
  - test/helper.rb
130
132
  - test/test_redminer.rb