Class: Ramazon::BrowseNode

Inherits:
Object
  • Object
show all
Includes:
HTTParty, HappyMapper
Defined in:
lib/ramazon/browse_node.rb

Constant Summary collapse

DEFAULT_ROOT_FILE =
File.join(File.dirname(__FILE__), '..', 'root_nodes.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#childObject

Returns the value of attribute child.



13
14
15
# File 'lib/ramazon/browse_node.rb', line 13

def child
  @child
end

Class Method Details

.find(node_id) ⇒ Object

find a browse node based on its id

Parameters:

  • node_id (String)

    the browse node you’re looking for



46
47
48
49
50
# File 'lib/ramazon/browse_node.rb', line 46

def self.find(node_id)
  req = Ramazon::Request.new(:operation => "BrowseNodeLookup", :browse_node_id => node_id)
  res = req.submit
  parse(res.to_s)[0]
end

.generate_root_nodes(file_name = DEFAULT_ROOT_FILE) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ramazon/browse_node.rb', line 17

def self.generate_root_nodes(file_name = DEFAULT_ROOT_FILE)
  if Ramazon::Configuration.locale == :us
    doc = Nokogiri::HTML(get('http://www.amazon.com').body)
    root_nodes = {}
    doc.search(".navSaMenu .navSaChildItem a").each do |element|
      if element["href"] =~ /node=(\d+)\&/
        root_nodes[element.content] = $1
      end
    end

    unless root_nodes.empty?
      FileUtils.rm_f(file_name)
      File.open(file_name, 'w') do |f|
        f.write(root_nodes.to_yaml)
      end
    end
  else
    #todo correlate ECS locales to actual amazon.* urls
    raise "generating root nodes for locale's other than the US is not supported"
  end
end

.parse(xml, options = {}) ⇒ Object



65
66
67
# File 'lib/ramazon/browse_node.rb', line 65

def self.parse(xml, options = {})
  super
end

.root_nodes(file_name = DEFAULT_ROOT_FILE) ⇒ Object



39
40
41
# File 'lib/ramazon/browse_node.rb', line 39

def self.root_nodes(file_name = DEFAULT_ROOT_FILE)
  @root_nodes ||= File.open(file_name) { |yf| YAML::load(yf) }
end

Instance Method Details

#child_hashObject

get a hash of name -> child browse_nodes



54
55
56
57
58
59
60
61
62
63
# File 'lib/ramazon/browse_node.rb', line 54

def child_hash
  if !@child_hash
    @child_hash = {}
    self.children.each do |i|
      @child_hash[i.name] = i
    end
  end

  @child_hash
end