Module: Dmm::Util
Instance Method Summary collapse
-
#create_uri(word, service: nil, floor: nil, hits: 1, offset: 1, sort: 'rank') ⇒ Object
util.
-
#from_xml(rexml) ⇒ Object
rexml.
- #get_api(uri) ⇒ Object
-
#get_items(h = nil) ⇒ Hash
Requestの下を返す.
-
#get_request(h = nil) ⇒ Hash
Hash Analyze.
-
#get_result(h = nil) ⇒ Hash
Requestの下を返す.
- #get_result_count(h = nil) ⇒ Object
-
#get_sample_images(h = nil) ⇒ Array
複数のimageとそのTitleを返す arr [ => “”, :images => [“url”],…].
-
#get_title_images(h = nil) ⇒ Array
複数のimageとそのTitleを返す arr [ => “”, :images => [“url”],…].
-
#get_titles(h = nil) ⇒ Array
Titleを返す.
-
#keyword(word, service: nil, floor: nil, hits: 20, offset: 1, sort: 'date') ⇒ Hash
Search 検索.
-
#no_image(i) ⇒ Object
util_valid.
- #what_keyword ⇒ Object
Instance Method Details
#create_uri(word, service: nil, floor: nil, hits: 1, offset: 1, sort: 'rank') ⇒ Object
util
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/dmm/core.rb', line 158 def create_uri(word, service: nil, floor: nil, hits: 1, offset: 1, sort: 'rank') arr = [] arr << "api_id=#{@api}" arr << "affiliate_id=#{@id}-991" arr << "operation=ItemList" arr << "version=#{@version}" arr << "timestamp=#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}" arr << "site=#{@site}" arr << "keyword=#{word}" arr << "service=#{service}" if service arr << "floor=#{floor}" if floor arr << "hits=#{hits}" if hits arr << "offset=#{offset}" if offset arr << "sort=#{sort}" if sort encode_uri = ("#{@url}?#{arr.join('&')}").encode("EUC-JP","UTF-8") URI.escape(encode_uri) end |
#from_xml(rexml) ⇒ Object
rexml
198 199 200 |
# File 'lib/dmm/core.rb', line 198 def from_xml(rexml) xml_elem_to_hash rexml.root end |
#get_api(uri) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/dmm/core.rb', line 176 def get_api(uri) xml = "" open(uri) do |o| o.each do |l| if /\<parameter\sname/ =~ l # なんでParameterの中に入れるんだろうね(´・ω・`) # 取り出そうよ b = l.scan(/\"(.*?)\"/).flatten xml << "<#{b[0]}>" xml << "#{b[1]}" xml << "</#{b[0]}>" xml << "\n" else xml << l end end end xml end |
#get_items(h = nil) ⇒ Hash
Returns requestの下を返す.
60 61 62 63 |
# File 'lib/dmm/core.rb', line 60 def get_items(h = nil) h ||= @hashdoc h[:response][:result][:items][:item] end |
#get_request(h = nil) ⇒ Hash
Hash Analyze
44 45 46 47 |
# File 'lib/dmm/core.rb', line 44 def get_request(h = nil) h ||= @hashdoc h[:response][:request] end |
#get_result(h = nil) ⇒ Hash
Returns requestの下を返す.
52 53 54 55 |
# File 'lib/dmm/core.rb', line 52 def get_result(h = nil) h ||= @hashdoc h[:response][:result] end |
#get_result_count(h = nil) ⇒ Object
130 131 132 133 |
# File 'lib/dmm/core.rb', line 130 def get_result_count(h=nil) h ||= @hashdoc h[:response][:result][:result_count] end |
#get_sample_images(h = nil) ⇒ Array
Returns 複数のimageとそのTitleを返す arr [ => “”, :images => [“url”],…].
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 |
# File 'lib/dmm/core.rb', line 96 def get_sample_images(h = nil) h ||= @hashdoc arr = [] if h.nil? return no_image(1) end if get_result_count(h) == "0" return no_image(2) end items = get_items(h) items.each do |m| #Valid if m[:sampleImageURL].nil? next end if m[:sampleImageURL][:sample_s].nil? next end if m[:sampleImageURL][:sample_s][:image].nil? next end # あった arr << { :title => m[:title], :affiliateURL => m[:affiliateURL], :images => m[:sampleImageURL][:sample_s][:image] } end if arr.empty? arr << no_image(3) end arr end |
#get_title_images(h = nil) ⇒ Array
Returns 複数のimageとそのTitleを返す arr [ => “”, :images => [“url”],…].
82 83 84 85 86 87 88 89 90 |
# File 'lib/dmm/core.rb', line 82 def get_title_images(h = nil) h ||= @hashdoc arr = [] items = get_items(h) items.each do |m| arr << { :title => m[:title], :affiliate_id => m[:affiliate_id], :list => m[:imageURL][:list], :small => m[:imageURL][:small], :large => m[:imageURL][:large] } end arr end |
#get_titles(h = nil) ⇒ Array
Returns titleを返す.
68 69 70 71 72 73 74 75 76 |
# File 'lib/dmm/core.rb', line 68 def get_titles(h = nil) h ||= @hashdoc items = get_items(h) arr = [] items.each do |m| arr << { :title => m[:title], :affiliate_id => m[:affiliate_id] } end arr end |
#keyword(word, service: nil, floor: nil, hits: 20, offset: 1, sort: 'date') ⇒ Hash
Search 検索
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dmm/core.rb', line 24 def keyword(word, service: nil, floor: nil, hits: 20, offset: 1, sort: 'date') uri = create_uri(word, service:service, floor:floor, hits:hits , offset:offset, sort:sort) @keyword = word xmlbody = get_api(uri) # EUC-JPのまま通過 # Railsだとなぜか自動的にUTF-8にしている気が。。 xmlbody_enc = (xmlbody.encoding.to_s == "EUC-JP" ? xmlbody : xmlbody.encode("EUC-JP","UTF-8")) @xmldoc = REXML::Document.new(xmlbody_enc) @hashdoc = from_xml(@xmldoc) @hashdoc end |
#no_image(i) ⇒ Object
util_valid
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/dmm/core.rb', line 136 def no_image(i) err_str = case i when 1 "検索してないよ" when 2 "検索結果は0件" when 3 "すまない。sample_imageはないんだ(´・ω・`)" else "no_imageは直接呼び出さないでくれ" end hash = { :title => 'I do not have an image', :affiliateURL => 'http://www.dmm.com/top/#{@id}-001', :images => 'http://pics.dmm.com/af/c_top/125_125.jpg', :err => err_str } hash end |
#what_keyword ⇒ Object
36 37 38 |
# File 'lib/dmm/core.rb', line 36 def what_keyword @keyword end |