module Dmm::Util
Public Instance Methods
create_uri(word, service: nil, floor: nil, hits: 1, offset: 1, sort: 'rank')
click to toggle source
util
# File lib/dmm/core.rb, line 157 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)
click to toggle source
rexml
# File lib/dmm/core.rb, line 197 def from_xml(rexml) xml_elem_to_hash rexml.root end
get_api(uri)
click to toggle source
# File lib/dmm/core.rb, line 175 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)
click to toggle source
@param [Hash] h DMMAPIのHash化したもの。
nil ok
@return [Hash] requestの下を返す
# File lib/dmm/core.rb, line 59 def get_items(h = nil) h ||= @hashdoc h[:response][:result][:items][:item] end
get_request(h = nil)
click to toggle source
Hash Analyze
@param [Hash] h DMMAPIのHash化したもの。
nil ok
@return [Hash] requestの下を返す
# File lib/dmm/core.rb, line 43 def get_request(h = nil) h ||= @hashdoc h[:response][:request] end
get_result(h = nil)
click to toggle source
@param [Hash] h DMMAPIのHash化したもの。
nil ok
@return [Hash] requestの下を返す
# File lib/dmm/core.rb, line 51 def get_result(h = nil) h ||= @hashdoc h[:response][:result] end
get_result_count(h=nil)
click to toggle source
# File lib/dmm/core.rb, line 129 def get_result_count(h=nil) h ||= @hashdoc h[:response][:result][:result_count] end
get_sample_images(h = nil)
click to toggle source
@param [Hash] h DMMAPIのHash化したもの。
nil ok
@return [Array] 複数のimageとそのTitleを返す
arr [ {:title => "", :images => ["url"]},...]
# File lib/dmm/core.rb, line 95 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)
click to toggle source
@param [Hash] h DMMAPIのHash化したもの。
nil ok
@return [Array] 複数のimageとそのTitleを返す
arr [ {:title => "", :images => ["url"]},...]
# File lib/dmm/core.rb, line 81 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)
click to toggle source
@param [Hash] h DMMAPIのHash化したもの。
nil ok
@return [Array] titleを返す
# File lib/dmm/core.rb, line 67 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')
click to toggle source
Search
検索 @param [String] word 検索キーワード @return [Hash] APIのレスポンスをXML形式からHash形式に変更したもの
# File lib/dmm/core.rb, line 23 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)
click to toggle source
util_valid
# File lib/dmm/core.rb, line 135 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()
click to toggle source
# File lib/dmm/core.rb, line 35 def what_keyword @keyword end
Private Instance Methods
xml_elem_to_hash(elem)
click to toggle source
# File lib/dmm/core.rb, line 202 def xml_elem_to_hash(elem) value = if elem.has_elements? children = {} elem.each_element do |e| children.merge!(xml_elem_to_hash(e)) do |k,v1,v2| v1.class == Array ? v1 << v2 : [v1,v2] end end children else elem.text end { elem.name.to_sym => value } end