Module: StwEngine::Helpers::Common
Instance Method Summary collapse
- #add_size(size, params) ⇒ Object
-
#createsig(body) ⇒ Object
function to create a hash.
-
#http_get(addr, params) ⇒ Object
struc = {stwaccesskeyid: api_key, stwu: api_secret }.
- #nokogiri_val(xml, tag) ⇒ Object
- #sigflat(body) ⇒ Object
-
#stw_show(url, options = {}, only_url = false) ⇒ Object
function to store the image and return the image url.
-
#stw_show_info ⇒ Object
show account info.
-
#stw_show_url(url, options = {}) ⇒ Object
function to return only url.
Instance Method Details
#add_size(size, params) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/stw_engine/helpers/common.rb', line 60 def add_size size, params case size when 'xlg' params['stwxmax'] = 320 when 'lg' params['stwxmax'] = 200 when 'sm' params['stwxmax'] = 120 when 'vsm' params['stwxmax'] = 100 when 'tny' params['stwxmax'] = 90 when 'mcr' params['stwxmax'] = 75 end return params end |
#createsig(body) ⇒ Object
function to create a hash
12 13 14 |
# File 'lib/stw_engine/helpers/common.rb', line 12 def createsig(body) Digest::MD5.hexdigest( sigflat body ) end |
#http_get(addr, params) ⇒ Object
struc = {stwaccesskeyid: api_key, stwu: api_secret }
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/stw_engine/helpers/common.rb', line 40 def http_get(addr, params) uri = URI.parse(addr) # Add params to URI uri.query = URI.encode_www_form( params ) begin uri.open.read rescue OpenURI::HTTPError => ex return 'http_error' end end |
#nokogiri_val(xml, tag) ⇒ Object
160 161 162 163 164 165 166 |
# File 'lib/stw_engine/helpers/common.rb', line 160 def nokogiri_val(xml, tag) xml_doc = Nokogiri::XML(xml) a = xml_doc.xpath("//stw:#{tag}").inner_text.strip end |
#sigflat(body) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/stw_engine/helpers/common.rb', line 16 def sigflat(body) if body.class == Hash arr = [] body.each do |key, value| arr << "#{sigflat key}=>#{sigflat value}" end body = arr end if body.class == Array str = '' body.map! do |value| sigflat value end.sort!.each do |value| str << value end end if body.class != String body = body.to_s << body.class.to_s end body end |
#stw_show(url, options = {}, only_url = false) ⇒ Object
function to store the image and return the image url
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/stw_engine/helpers/common.rb', line 80 def stw_show url, ={}, only_url=false sizes = ['xlg', 'lg', 'sm', 'vsm', 'tny', 'mcr'] params = {:stwaccesskeyid => StwEngine.api_key , :stwu => StwEngine.api_secret} if StwEngine.size params = add_size StwEngine.size, params end if .key?(:size) params = add_size [:size], params end #return params params[:stwurl] = url filename = createsig(params) filename << '.jpg' #return the file if exists if FileTest.exist?("#{Rails.root}/public/images/#{filename}") img = "<img src=\"/stw/#{filename}\"/>" img.respond_to?(:html_safe) ? img.html_safe : img return img end #else send http request to retrieve the image res = http_get(StwEngine.image_url , params) if res == 'http_error' return 'http_error' end #puts res.body xml_doc = Nokogiri::XML(res) elems = xml_doc.xpath("//stw:ThumbnailResult/stw:Thumbnail") image = elems[0].inner_text.strip if elems[0].attr('Exists') == 'true' image = elems[0].inner_text.strip end #if not http error show the error image not save status_codes = ['fix_and_retry','noretry','noexist'] stat = elems[1].inner_text.strip if status_codes.include?stat img = "<img src=\"#{image}\"/>" return img.respond_to?(:html_safe) ? img.html_safe : img end directory = "#{Rails.root}/public/stw" Dir.mkdir("#{Rails.root}/public/stw") unless Dir.exist? "#{Rails.root}/public/stw" path = File.join(directory, filename) open(path, 'wb') do |file| file << open(image).read end img_path = "/stw/#{filename}" if only_url return img_path end img = "<img src=\"/stw/#{filename}\"/>" img.respond_to?(:html_safe) ? img.html_safe : img end |
#stw_show_info ⇒ Object
show account info
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/stw_engine/helpers/common.rb', line 170 def stw_show_info info = ['Status', 'Account_Level', 'Inside_Pages', 'Custom_Size', 'Full_Length', 'Refresh_OnDemand', 'Custom_Delay', 'Custom_Quality', 'Custom_Messages', 'Custom_Resolution'] info_h = Hash.new #get account info params = {:stwaccesskeyid => StwEngine.api_key , :stwu => StwEngine.api_secret } res = http_get(StwEngine.account_url , params) info.each {|x| info_h[x] = nokogiri_val(res,x) } if info_h['Account_Level'] == '1' info_h['Account_Level'] = 'Basic' elsif info_h['Account_Level'] == '2' info_h['Account_Level'] = 'Plus' else info_h['Account_Level'] = 'Free' end return info_h #http://images.shrinktheweb.com/account.php?".http_build_query($args) end |
#stw_show_url(url, options = {}) ⇒ Object
function to return only url
56 57 58 |
# File 'lib/stw_engine/helpers/common.rb', line 56 def stw_show_url url, ={} stw_show url, , only_url=true end |