Module: Kernel

Defined in:
lib/mdpreview.rb

Overview

require “iconv”

class String

def to_gbk  
  Iconv.iconv("GBK//IGNORE", "UTF-8//IGNORE", self).to_s  
end  

def to_utf8  
  #p "my own string"  
  Iconv.iconv("UTF-8//IGNORE", "GBK//IGNORE", self).to_s  
end  

def to_utf8_valid  

  if !self.valid_encoding?  
    ic = Iconv.new('UTF-8//IGNORE', 'UTF-8')  
    return ic.iconv(self)  
  end  
  self  
end

end

Instance Method Summary collapse

Instance Method Details

#easy_get(url) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/mdpreview.rb', line 32

def easy_get(url)
  puts "\n[GET]  #{url}\n"
  # p Net::HTTP.get_print URI.parse(url)
  
  resp = Net::HTTP.get_response(URI.parse(url))
  data = resp.body
  p "json = "+data

  Mdpreview.create_json_file(data)
  # we convert the returned JSON data to native Ruby
  # data structure - a hash
  # result = JSON.parse(data)
  
end

#easy_post(url, params, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mdpreview.rb', line 48

def easy_post(url,params,&block)
  res = Net::HTTP.post_form(URI.parse(url),params)
  puts "\n\n[POST] #{url}?#{params.to_s}\n"
  p "#{res}\n#{res.body}"
  
  Mdpreview.create_json_file(res.body)
  
  if block_given?
    block.call(res)
  end
end