module RedmineAPIHelper::APIHelper
Constants
- LIMIT_REDIRECTS
- USER_AGENT
Public Instance Methods
create_object(object, params={})
click to toggle source
creates a new object with params, corresponds to controller#create
# File lib/redmine_api_helper/api_helper.rb, line 84 def create_object(object, params={}) jpost( {object => params}, :url => send("#{object.to_s.pluralize}_url") ).send(object) rescue Exception => err error(err) end
create_project_object(project_id, object, params={})
click to toggle source
# File lib/redmine_api_helper/api_helper.rb, line 90 def create_project_object(project_id, object, params={}) jpost( {object => params}, :url => send("project_#{object.to_s.pluralize}_url", project_id) ).send(object) rescue Exception => err error(err) end
destroy_object(object, id, params={})
click to toggle source
deletes an existing object with params, corresponds to controller#destroy
# File lib/redmine_api_helper/api_helper.rb, line 114 def destroy_object(object, id, params={}) jdel(:url => send("#{object}_url", id), :params => params ) rescue Exception => err error(err) end
destroy_project_object(project_id, object, id, params={})
click to toggle source
# File lib/redmine_api_helper/api_helper.rb, line 120 def destroy_project_object(project_id, object, id, params={}) jdel(:url => send("project_#{object}_url", project_id, id), :params => params ) rescue Exception => err error(err) end
error(err)
click to toggle source
returns error
# File lib/redmine_api_helper/api_helper.rb, line 36 def error(err) OpenStruct.new(:error => err.message, :backtrace => err.backtrace) end
fetch(options={})
click to toggle source
fetch(options), get request
# File lib/redmine_api_helper/api_helper.rb, line 129 def fetch(options={}) # create query parameters params = options[:params].to_h.to_query url = options[:url] # create GET request uri = URI.parse([url, params.presence].compact.join("?")) req = Net::HTTP::Get.new(uri.request_uri) # create HTTP handler http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme.downcase == "https" # get request @http_response = http.request(req) case @http_response when Net::HTTPSuccess @http_response.body.present? ? @http_response.body : serialize(:response => "OK") when Net::HTTPRedirection options[:redirects] = options[:redirects].to_i + 1 raise TooManyRedirects if options[:redirects] > LIMIT_REDIRECTS fetch( options.merge(:url => response['location']) ) else serialize(:response => @http_response.code) end end
jdel(options={})
click to toggle source
jdel(options), delete request
# File lib/redmine_api_helper/api_helper.rb, line 274 def jdel(options={}) index = options[:index].to_i json = options[:json].nil? || !!options[:json] params = json ? options[:params].to_h.merge(:format => "json").to_query : options[:params].to_h.to_query content_type = json ? "application/json" : options[:content_type] api = options[:api_key].nil? ? true : !!options[:api_key] url = options[:url].presence || args.objects[index].object_url # create DELETE request uri = URI.parse( [url, params.presence].compact.join("?")) req = Net::HTTP::Delete.new(uri.request_uri) req["Content-Type"] = content_type req['X-Redmine-API-Key'] = args.api_key if api req["Referer"] = args.deep_try(:eparams, :url) || args.deep_try(:urls, :back) req["User-Agent"] = USER_AGENT req["Accept"] = "*/*" req.basic_auth(args.site_user, args.site_password) if args.site_user.present? || args.site_password.present? # create HTTP handler http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme.downcase == "https" # get request @http_response = http.request(req) # follow redirection or get result code handle_response(options) end
jget(options={})
click to toggle source
jget(options), get request
# File lib/redmine_api_helper/api_helper.rb, line 166 def jget(options={}) index = options[:index].to_i json = options[:json].nil? || !!options[:json] params = json ? options[:params].to_h.merge(:format => "json").to_query : options[:params].to_h.to_query content_type = json ? "application/json" : options[:content_type] api = options[:api_key].nil? ? true : !!options[:api_key] url = options[:url].presence || args.objects[index].object_url # create GET request uri = URI.parse( [url, params.presence].compact.join("?")) req = Net::HTTP::Get.new(uri.request_uri) req["Content-Type"] = content_type req['X-Redmine-API-Key'] = args.api_key if api req["Referer"] = args.deep_try(:eparams, :url) || args.deep_try(:urls, :back) req["User-Agent"] = USER_AGENT req["Accept"] = "*/*" req.basic_auth(args.site_user, args.site_password) if args.site_user.present? || args.site_password.present? # create HTTP handler http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme.downcase == "https" # get request @http_response = http.request(req) # follow redirection or get result code handle_response(options) end
jpost(body, options={})
click to toggle source
jpost(body, options), post request
# File lib/redmine_api_helper/api_helper.rb, line 237 def jpost(body, options={}) index = options[:index].to_i json = options[:json].nil? || !!options[:json] params = json ? options[:params].to_h.merge(:format => "json").to_query : options[:params].to_h.to_query content_type = json ? "application/json" : options[:content_type] api = options[:api_key].nil? ? true : !!options[:api_key] url = options[:url].presence || args.objects[index].object_url # create POST request uri = URI.parse( [url, params.presence].compact.join("?")) req = Net::HTTP::Post.new(uri.request_uri) req["Content-Type"] = content_type req['X-Redmine-API-Key'] = args.api_key if api req["Referer"] = args.deep_try(:eparams, :url) || args.deep_try(:urls, :back) req["User-Agent"] = USER_AGENT req["Accept"] = "*/*" req.basic_auth(args.site_user, args.site_password) if args.site_user.present? || args.site_password.present? # create body req.body = deserialize(body).to_json # create HTTP handler http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme.downcase == "https" # get request @http_response = http.request(req) # follow redirection or get result code handle_response(options) end
jput(body, options={})
click to toggle source
jput(body, options), put request
# File lib/redmine_api_helper/api_helper.rb, line 200 def jput(body, options={}) index = options[:index].to_i json = options[:json].nil? || !!options[:json] params = json ? options[:params].to_h.merge(:format => "json").to_query : options[:params].to_h.to_query content_type = json ? "application/json" : options[:content_type] api = options[:api_key].nil? ? true : !!options[:api_key] url = options[:url].presence || args.objects[index].object_url # create PUT request uri = URI.parse( [url, params.presence].compact.join("?")) req = Net::HTTP::Put.new(uri.request_uri) req["Content-Type"] = content_type req['X-Redmine-API-Key'] = args.api_key if api req["Referer"] = args.deep_try(:eparams, :url) || args.deep_try(:urls, :back) req["User-Agent"] = USER_AGENT req["Accept"] = "*/*" req.basic_auth(args.site_user, args.site_password) if args.site_user.present? || args.site_password.present? # create body req.body = deserialize(body).to_json # create HTTP handler http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme.downcase == "https" # get request @http_response = http.request(req) # follow redirection or get result code handle_response(options) end
list_objects(objects, params={})
click to toggle source
lists objects, corresponds to controller#index
# File lib/redmine_api_helper/api_helper.rb, line 54 def list_objects(objects, params={}) jget(:url => send("#{objects}_url"), :params => params ).send(objects) rescue Exception => err error(err) end
list_project_objects(project_id, objects, params={})
click to toggle source
# File lib/redmine_api_helper/api_helper.rb, line 60 def list_project_objects(project_id, objects, params={}) jget(:url => send("project_#{objects}_url", project_id), :params => params ).send(objects) rescue Exception => err error(err) end
read_object(object, id, params={})
click to toggle source
reads object having id, corresponds to controller#show
# File lib/redmine_api_helper/api_helper.rb, line 69 def read_object(object, id, params={}) jget(:url => send("#{object}_url", id), :params => params ).send(object) rescue Exception => err error(err) end
read_project_object(project_id, object, id, params={})
click to toggle source
# File lib/redmine_api_helper/api_helper.rb, line 75 def read_project_object(project_id, object, id, params={}) jget(:url => send("project_#{object}_url", project_id, id), :params => params ).send(object) rescue Exception => err error(err) end
update_object(object, id, params={})
click to toggle source
updates an existing object with params, corresponds to controller#update
# File lib/redmine_api_helper/api_helper.rb, line 99 def update_object(object, id, params={}) jput( {object => params}, :url => send("#{object}_url", id) ) rescue Exception => err error(err) end
update_project_object(project_id, object, id, params={})
click to toggle source
# File lib/redmine_api_helper/api_helper.rb, line 105 def update_project_object(project_id, object, id, params={}) jput( {object => params}, :url => send("project_#{object}_url", project_id, id) ) rescue Exception => err error(err) end
url_path(*fragments, **options)
click to toggle source
assembles url from fragments
# File lib/redmine_api_helper/api_helper.rb, line 43 def url_path(*fragments, **options) [fragments.map do |fragment| fragment.to_s.gsub(/\/\z/,"") end.join("/"), options.to_query.presence ].compact.join("?") end
Private Instance Methods
handle_response(options)
click to toggle source
# File lib/redmine_api_helper/api_helper.rb, line 313 def handle_response(options) case @http_response when Net::HTTPSuccess @http_response.body.present? ? serialize(JSON.parse(@http_response.body)) : serialize(:result => @http_response.code) when Net::HTTPRedirection options[:redirects] = options[:redirects].to_i + 1 raise TooManyRedirects if options[:redirects] > LIMIT_REDIRECTS function = caller_locations(1,1)[0].label.to_sym send(function, options) else @http_response.body.present? ? serialize(JSON.parse(@http_response.body)) : serialize(:result => @http_response.code) end end