Class: Dmm::Com

Inherits:
Object
  • Object
show all
Includes:
Configuration
Defined in:
lib/dmm/core.rb

Instance Method Summary collapse

Methods included from Configuration

#initialize

Instance Method Details

#create_uri(word, options = {}) ⇒ Object

util



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/dmm/core.rb', line 105

def create_uri(word, options = {})
  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}"
  encode_uri = ("#{@url}?#{arr.join('&')}").encode("EUC-JP","UTF-8")
  URI.escape(encode_uri)
end

#from_xml(rexml) ⇒ Object

rexml



139
140
141
# File 'lib/dmm/core.rb', line 139

def from_xml(rexml)
  xml_elem_to_hash rexml.root
end

#get_api(uri) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dmm/core.rb', line 118

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]}>"
      else
        xml << l
      end
    end

  end
  xml
end

#get_items(h = nil) ⇒ Hash

Returns requestの下を返す.

Parameters:

  • h (Hash) (defaults to: nil)

    DMMAPIのHash化したもの。nil ok

Returns:

  • (Hash)

    requestの下を返す



56
57
58
59
# File 'lib/dmm/core.rb', line 56

def get_items(h = nil)
  h ||= @hashdoc
  h[:response][:result][:items][:item]
end

#get_request(h = nil) ⇒ Hash

Hash Analyze

Parameters:

  • h (Hash) (defaults to: nil)

    DMMAPIのHash化したもの。nil ok

Returns:

  • (Hash)

    requestの下を返す



40
41
42
43
# File 'lib/dmm/core.rb', line 40

def get_request(h = nil)
  h ||= @hashdoc
  h[:response][:request]
end

#get_result(h = nil) ⇒ Hash

Returns requestの下を返す.

Parameters:

  • h (Hash) (defaults to: nil)

    DMMAPIのHash化したもの。nil ok

Returns:

  • (Hash)

    requestの下を返す



48
49
50
51
# File 'lib/dmm/core.rb', line 48

def get_result(h = nil)
  h ||= @hashdoc
  h[:response][:result]
end

#get_sample_images(h = nil) ⇒ Array

Returns 複数のimageとそのTitleを返すarr [ => “”, :images => [“url”],…].

Parameters:

  • h (Hash) (defaults to: nil)

    DMMAPIのHash化したもの。nil ok

Returns:

  • (Array)

    複数のimageとそのTitleを返すarr [ => “”, :images => [“url”],…]



93
94
95
96
97
98
99
100
101
102
# File 'lib/dmm/core.rb', line 93

def get_sample_images(h = nil)
  h ||= @hashdoc
  arr = []
  items = get_items
  titles = get_titles(h)
  items.each_with_index do |m, i|
    arr << { :title => titles[i], :images => m[:sampleImageURL][:sample_s][:image]}
  end
  arr
end

#get_title_images(h = nil) ⇒ Array

Returns 複数のimageとそのTitleを返すarr [ => “”, :images => [“url”],…].

Parameters:

  • h (Hash) (defaults to: nil)

    DMMAPIのHash化したもの。nil ok

Returns:

  • (Array)

    複数のimageとそのTitleを返すarr [ => “”, :images => [“url”],…]



78
79
80
81
82
83
84
85
86
87
# File 'lib/dmm/core.rb', line 78

def get_title_images(h = nil)
  h ||= @hashdoc
  arr = []
  items = get_items(h)
  titles = get_titles(h)
  items.each_with_index do |m, i|
    arr << { :title => titles[i], :list => m[:imageURL][:list], :small => m[:imageURL][:small], :large => m[:imageURL][:large] }
  end
  arr
end

#get_titles(h = nil) ⇒ Array

Returns titleを返す.

Parameters:

  • h (Hash) (defaults to: nil)

    DMMAPIのHash化したもの。nil ok

Returns:

  • (Array)

    titleを返す



64
65
66
67
68
69
70
71
72
# File 'lib/dmm/core.rb', line 64

def get_titles(h = nil)
  h ||= @hashdoc
  items = get_items(h)
  arr = []
  items.each do |i|
    arr << i[:title]
  end
  arr
end

#keyword(word, options = {:service => nil, :floor => nil, :hits => 20, :offset => 1, :sort => 'rank'}) ⇒ Hash

Search 検索

Parameters:

  • word (String)

    検索キーワード

Returns:

  • (Hash)

    APIのレスポンスをXML形式からHash形式に変更したもの



27
28
29
30
31
32
33
# File 'lib/dmm/core.rb', line 27

def keyword(word, options = {:service => nil, :floor => nil, :hits => 20, :offset => 1, :sort => 'rank'})
  uri = create_uri(word)
  xmlbody = get_api(uri)
  @xmldoc = REXML::Document.new(xmlbody)
  @hashdoc = from_xml(@xmldoc)
  @hashdoc
end