Module: Itesttool
- Defined in:
- lib/itesttool.rb,
lib/itesttool/version.rb
Constant Summary collapse
- VERSION =
"0.1.1"
Class Method Summary collapse
- .add_headers(request) ⇒ Object
- .decorate_response(res, method, url, res_format) ⇒ Object
- .execute_request(url_obj, data, &block) ⇒ Object
- .get(url, res_format = "text", query = "") ⇒ Object
- .post(url, data, res_format) ⇒ Object
Instance Method Summary collapse
- #as_html ⇒ Object (also: #res_is_html)
- #as_json ⇒ Object (also: #res_is_json)
- #as_text ⇒ Object
- #as_xml ⇒ Object (also: #res_is_xml)
- #body(data = "") ⇒ Object
- #body_as_form(data = {}) ⇒ Object
- #body_as_json(data = {}) ⇒ Object
- #delete(url, data, res_format) ⇒ Object
- #headers(h = {}) ⇒ Object
- #put(url, data, res_format) ⇒ Object
- #query(h = {}) ⇒ Object
Class Method Details
.add_headers(request) ⇒ Object
89 90 91 |
# File 'lib/itesttool.rb', line 89 def add_headers(request) if @headers then @headers.each{|k, v| request.add_field k, v} end end |
.decorate_response(res, method, url, res_format) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/itesttool.rb', line 112 def decorate_response(res, method, url, res_format) class << res attr_accessor :url, :res_format, :method def [](path) select(path) end def select(path, &block) result = if res_format && res_format == "xml" Nokogiri::XML(body).xpath(path).map{|x| x.text} elsif res_format && res_format == "html" Nokogiri::HTML(body).css(path).map{|x| x.text} elsif res_format && res_format == "json" JsonPath.on(body, path) end block.call(result) unless block.nil? result end def to_s method + " " + url end end res.url = url res.res_format = res_format res.method = method res end |
.execute_request(url_obj, data, &block) ⇒ Object
140 141 142 143 144 145 146 147 |
# File 'lib/itesttool.rb', line 140 def execute_request(url_obj, data, &block) Net::HTTP.start(url_obj.host, url_obj.port, :use_ssl => /https/ === url_obj.scheme) {|http| request = block.call set_body(request, data) unless data.nil? add_headers(request) http.request(request) } end |
.get(url, res_format = "text", query = "") ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/itesttool.rb', line 55 def get(url, res_format="text", query="") url_obj = URI.parse(url) queries = [] queries.push(query) unless query.empty? queries.push(url_obj.query) unless url_obj.query.nil? path_with_query = if queries.empty? url_obj.path else url_obj.path + "?" + queries.join("&") end res = execute_request(url_obj, nil){Net::HTTP::Get.new(path_with_query)} decorate_response(res, "GET", url, res_format) end |
.post(url, data, res_format) ⇒ Object
70 71 72 73 74 |
# File 'lib/itesttool.rb', line 70 def post(url, data, res_format) url_obj = URI.parse(url) res = execute_request(url_obj, data){Net::HTTP::Post.new(url_obj.path)} decorate_response(res, "POST", url, res_format) end |
Instance Method Details
#as_html ⇒ Object Also known as: res_is_html
22 |
# File 'lib/itesttool.rb', line 22 def as_html() "html" end |
#as_json ⇒ Object Also known as: res_is_json
20 |
# File 'lib/itesttool.rb', line 20 def as_json() "json" end |
#as_text ⇒ Object
19 |
# File 'lib/itesttool.rb', line 19 def as_text() "text" end |
#as_xml ⇒ Object Also known as: res_is_xml
21 |
# File 'lib/itesttool.rb', line 21 def as_xml() "xml" end |
#body(data = "") ⇒ Object
44 45 46 |
# File 'lib/itesttool.rb', line 44 def body(data = "") {:body => data} end |
#body_as_form(data = {}) ⇒ Object
47 48 49 |
# File 'lib/itesttool.rb', line 47 def body_as_form(data = {}) {:form => data} end |
#body_as_json(data = {}) ⇒ Object
50 51 52 |
# File 'lib/itesttool.rb', line 50 def body_as_json(data = {}) {:json => data} end |
#delete(url, data, res_format) ⇒ Object
82 83 84 85 86 |
# File 'lib/itesttool.rb', line 82 def delete(url, data, res_format) url_obj = URI.parse(url) res = execute_request(url_obj, data){Net::HTTP::DELETE.new(url_obj.path)} decorate_response(res, "DELETE", url, res_format) end |
#headers(h = {}) ⇒ Object
28 29 30 |
# File 'lib/itesttool.rb', line 28 def headers(h = {}) @headers = h end |
#put(url, data, res_format) ⇒ Object
76 77 78 79 80 |
# File 'lib/itesttool.rb', line 76 def put(url, data, res_format) url_obj = URI.parse(url) res = execute_request(url_obj, data){Net::HTTP::Put.new(url_obj.path)} decorate_response(res, "PUT", url, res_format) end |
#query(h = {}) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/itesttool.rb', line 32 def query(h = {}) q = [] h.each do |k, v| if v.is_a? Array q.concat( v.map { | p | URI.encode(k.to_s) + "=" + URI.encode(p.to_s) } ) else q << URI.encode(k.to_s) + "=" + URI.encode(v.to_s) end end q.join("&") end |