Module: SvxBox::Amazoni

Defined in:
lib/svxbox/amazoni.rb

Instance Method Summary collapse

Instance Method Details

#get_response(cat, search) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/svxbox/amazoni.rb', line 18

def get_response(cat, search)
  req = AmazonProduct["us"]

  req.configure do |c|
    c.key = ENV['AMAZON_KEY']
    c.secret = ENV['AMAZON_SECRET']
    c.tag = ENV['AMAZON_TAG']
  end

  puts "aaws: #{search} #{cat}" if ENV['RACK_ENV'] == 'development'

  req.search(cat, :operation => 'ItemSearch', :response_group => ['Small','Images'], :keywords => search, :total_results => 5 )
end

#new_search_aaws(cat, search) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/svxbox/amazoni.rb', line 32

def new_search_aaws(cat, search)
  resp = get_response(cat, search)
  if resp.valid?
    products = []
    i = 0 
    resp.each('Item') do |item|
      i = i + 1
      unless i > 4
        mfr = Array.new
        imfr = item['Manufacturer']
        product = {}
        unless mfr.include?(imfr)
          mfr << imfr
          product[:url] = url_unescape(item['ItemLinks']['ItemLink'][0]["URL"].to_s).force_encoding('UTF-8')
          if item['SmallImage']
            product[:small_image] = item['SmallImage']['URL']
            if item['LargeImage']
              product[:large_image] = item['LargeImage']['URL']
            end
          end
          product[:title] = item['ItemAttributes']['Title']

          if item['ItemAttributes']['Author']
            author = item['ItemAttributes']['Author']
            authors = author.is_a?(Array) ? author.map { |author| author.to_s } : [author]
            product[:authors] = authors.join(", ")
          end

          products << product
        end
      end
    end
    products
  end

end

#search_aaws(cat, search) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
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
# File 'lib/svxbox/amazoni.rb', line 69

def search_aaws(cat, search)

  resp = get_response(cat, search)

  #puts resp.to_hash.to_yaml if ENV['RACK_ENV'] == 'development'

  #puts resp.errors.inspect if ENV['RACK_ENV'] == 'development'
  if resp.valid?
    attribs = '<div>'
    puts resp.to_hash.to_yaml if ENV['RACK_ENV'] == 'development'
    i = 0 
    resp.each('Item') do |item|
      i = i + 1
      unless i > 4
        mfr = Array.new
        imfr = item['Manufacturer']
        unless mfr.include?(imfr)
          mfr << imfr
          iurl = url_unescape(item['ItemLinks']['ItemLink'][0]["URL"].to_s).force_encoding('UTF-8')
          link = "<a rel='nofollow' href='#{iurl}'>"
          attribs << '<div style="margin-bottom:1em;" class="clearadiv">'
          if item['SmallImage']
            attribs << %Q{<div class="flrt" style="clear:left;">#{link}<img alt="#{search}" src="#{item['SmallImage']['URL']}"/></a><br />'}
            if item['LargeImage']
              attribs << %Q{<a class="thickbox" href="#{item['LargeImage']['URL']}">zoom</a> / }
            end
            attribs << "<a href='#{iurl}'>buy</a>"
            attribs << '</div>'
          end
          puts attribs
          puts attribs.encoding.name
          attribs << link
          attribs << item['ItemAttributes']['Title']
          attribs << '</a><br />'

          if item['ItemAttributes']['Author']
            author = item['ItemAttributes']['Author']
            authors = author.is_a?(Array) ? author.map { |author| author.to_s } : [author]
            attribs << ' by ' << authors.join(", ")
          end
          attribs << '</div>'
        end
      end
    end
    attribs << '</div>'
  end
  return attribs.gsub('&','&amp;')
end

#url_unescape(string) ⇒ Object

From Rack::Utils / Camping



12
13
14
15
16
# File 'lib/svxbox/amazoni.rb', line 12

def url_unescape(string)
  string.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n) do
    [$1.delete('%')].pack('H*')
  end
end