Class: SimpleReq
- Inherits:
-
Object
- Object
- SimpleReq
- Defined in:
- lib/simplereq.rb
Class Method Summary collapse
- .get(url, get = {}) ⇒ Object
- .get_el(url, el, get = {}) ⇒ Object
- .j_get(url, get = {}) ⇒ Object
- .j_post(url, post) ⇒ Object
- .post(url, post) ⇒ Object
- .post_el(url, el, post) ⇒ Object
Class Method Details
.get(url, get = {}) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/simplereq.rb', line 7 def self.get(url, get={}) if get.empty? url = URI.encode(url) return RestClient.get(url) else args = '?' first = true get.each {|p, v| args << '&' unless first args << "#{p}=#{v}" first = false } url = URI.encode("#{url}#{args}") return RestClient.get(url) end end |
.get_el(url, el, get = {}) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/simplereq.rb', line 36 def self.get_el(url, el, get={}) if get.empty? req = get(url).body doc = Nokogiri.HTML(req) element = doc.at_css(el) return element.inner_html else args = '?' first = true get.each {|p, v| args << '&' unless first args << "#{p}=#{v}" first = false } req = get(url, get).body doc = Nokogiri.HTML(req) element = doc.at_css(el) return element.inner_html end end |
.j_get(url, get = {}) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/simplereq.rb', line 26 def self.j_get(url, get={}) if get.empty? return JSON.parse(get(url).body) else return JSON.parse(get(url, get).body) end end |
.j_post(url, post) ⇒ Object
33 34 35 |
# File 'lib/simplereq.rb', line 33 def self.j_post(url, post) JSON.parse(post(url, post).body) end |
.post(url, post) ⇒ Object
23 24 25 |
# File 'lib/simplereq.rb', line 23 def self.post(url, post) return RestClient.post(URI.encode(url), post) end |
.post_el(url, el, post) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/simplereq.rb', line 56 def self.post_el(url, el, post) req = post(url, post).body doc = Nokogiri.HTML(req) element = doc.at_css(el) return element.inner_html end |