Class: Redmine::Client
- Inherits:
-
Object
- Object
- Redmine::Client
- Defined in:
- lib/redmine/client.rb,
lib/redmine/client/version.rb,
lib/redmine/client/authorization_token.rb
Defined Under Namespace
Classes: AuthorizationToken, ResponseError
Constant Summary collapse
- VERSION =
"0.1.3"
Class Attribute Summary collapse
-
.base_url ⇒ Object
Returns the value of attribute base_url.
-
.raise_on_error ⇒ Object
writeonly
Sets the attribute raise_on_error.
Instance Attribute Summary collapse
-
#access_key ⇒ Object
readonly
Returns the value of attribute access_key.
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
Class Method Summary collapse
Instance Method Summary collapse
- #add_member_to_project(user_id, project_id, role_ids = [3]) ⇒ Object
- #check_errors(response) ⇒ Object
- #faraday ⇒ Object
-
#initialize(access_key, base_url = nil) ⇒ Client
constructor
A new instance of Client.
Constructor Details
#initialize(access_key, base_url = nil) ⇒ Client
35 36 37 38 39 40 41 42 |
# File 'lib/redmine/client.rb', line 35 def initialize(access_key, base_url=nil) @access_key = access_key @base_url = base_url || self.class.base_url unless @base_url raise ArgumentError, "You must provide an api base url, either Redmine::Client.new(token, base_url) or Redmine::Client.base_url = base_url" end end |
Class Attribute Details
.base_url ⇒ Object
Returns the value of attribute base_url.
24 25 26 |
# File 'lib/redmine/client.rb', line 24 def base_url @base_url end |
.raise_on_error=(value) ⇒ Object (writeonly)
Sets the attribute raise_on_error
25 26 27 |
# File 'lib/redmine/client.rb', line 25 def raise_on_error=(value) @raise_on_error = value end |
Instance Attribute Details
#access_key ⇒ Object (readonly)
Returns the value of attribute access_key.
33 34 35 |
# File 'lib/redmine/client.rb', line 33 def access_key @access_key end |
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
33 34 35 |
# File 'lib/redmine/client.rb', line 33 def base_url @base_url end |
Class Method Details
.crud(plural, singular) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/redmine/client.rb', line 53 def self.crud(plural, singular) class_eval <<-EOF, __FILE__, __LINE__ def create_#{singular}(params, full_response=false) resp = faraday.post("/#{plural}.json", {"#{singular}" => params}) check_errors(resp) full_response ? resp : resp.body end def find_#{singular}(id, full_response=false) resp = faraday.get("/#{plural}/\#{id}.json") check_errors(resp) full_response ? resp : resp.body end def update_#{singular}(id, params, full_response=false) resp = faraday.put("/#{plural}/\#{id}.json", {"#{singular}" => params}) check_errors(resp) full_response ? resp : resp.body end def delete_#{singular}(id, full_response=false, raise_on_error=true) resp = faraday.delete("/#{plural}/\#{id}.json") check_errors(resp) full_response ? resp : resp.body end EOF end |
.raise_on_error? ⇒ Boolean
26 27 28 29 30 |
# File 'lib/redmine/client.rb', line 26 def raise_on_error? return @raise_on_error if defined?(@raise_on_error) @raise_on_error = true end |
Instance Method Details
#add_member_to_project(user_id, project_id, role_ids = [3]) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/redmine/client.rb', line 86 def add_member_to_project(user_id, project_id, role_ids=[3]) faraday.post("/projects/#{project_id}/memberships.json", { "membership" => { "user_id" => user_id, "role_ids" => Array(role_ids), }}) end |
#check_errors(response) ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/redmine/client.rb', line 94 def check_errors(response) return if response.success? $stderr.puts "REDMINE ERROR (#{response.status}): #{response.body}" if self.class.raise_on_error? raise ResponseError.new(response) end end |
#faraday ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/redmine/client.rb', line 44 def faraday @faraday ||= Faraday.new(:url => base_url) do |f| f.request :json f.request :authorization_token, access_key f.adapter Faraday::default_adapter f.response :json, :content_type => /\bjson$/ end end |