Class: TopTravelDestinations::Region

Inherits:
Object
  • Object
show all
Defined in:
lib/top_travel_destinations/region.rb

Constant Summary collapse

@@all =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region_hash) ⇒ Region

Returns a new instance of Region.



7
8
9
10
11
12
# File 'lib/top_travel_destinations/region.rb', line 7

def initialize(region_hash)
  self.create_attributes_from_hash(region_hash)
  self.destinations = []
  self.get_destinations
  self.class.all << self
end

Instance Attribute Details

#destinationsObject

Returns the value of attribute destinations.



3
4
5
# File 'lib/top_travel_destinations/region.rb', line 3

def destinations
  @destinations
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/top_travel_destinations/region.rb', line 3

def name
  @name
end

#region_urlObject

Returns the value of attribute region_url.



3
4
5
# File 'lib/top_travel_destinations/region.rb', line 3

def region_url
  @region_url
end

Class Method Details

.allObject



30
31
32
# File 'lib/top_travel_destinations/region.rb', line 30

def self.all
  @@all
end

.create_from_array(regions_array) ⇒ Object



14
15
16
17
18
# File 'lib/top_travel_destinations/region.rb', line 14

def self.create_from_array(regions_array)
  regions_array.each do |region_hash|
    TopTravelDestinations::Region.new(region_hash)
  end
end

.get_regionsObject



34
35
36
# File 'lib/top_travel_destinations/region.rb', line 34

def self.get_regions
  TopTravelDestinations::Scraper.scrape_regions_array
end

Instance Method Details

#add_destinations(destinations_array) ⇒ Object

creates “has many” relationship to instance of Region



63
64
65
66
67
68
69
# File 'lib/top_travel_destinations/region.rb', line 63

def add_destinations(destinations_array)
  destinations_array.each do |destination_hash|
    destination = TopTravelDestinations::Destination.new(destination_hash) 
    self.destinations << destination
    destination.region = self
  end
end

#create_attribute_from_array(destinations_array) ⇒ Object



26
27
28
# File 'lib/top_travel_destinations/region.rb', line 26

def create_attribute_from_array(destinations_array)
  self.destinations = destinations_array
end

#create_attributes_from_hash(region_hash) ⇒ Object



20
21
22
23
24
# File 'lib/top_travel_destinations/region.rb', line 20

def create_attributes_from_hash(region_hash)
  region_hash.each do |key, value|
    self.send(("#{key}="), value)
  end
end

#destination_descriptionsObject



79
80
81
82
83
84
85
# File 'lib/top_travel_destinations/region.rb', line 79

def destination_descriptions
  destinations_descriptions_array = []
  self.destinations.each do |destination|
    destinations_descriptions_array << destination.description 
  end
  destinations_descriptions_array
end

#destination_namesObject



71
72
73
74
75
76
77
# File 'lib/top_travel_destinations/region.rb', line 71

def destination_names
  destinations_names_array = [] 
  self.destinations.each do |destination|
    destinations_names_array << destination.name
  end
  destinations_names_array 
end

#get_destinationsObject



38
39
40
41
# File 'lib/top_travel_destinations/region.rb', line 38

def get_destinations
  destinations_array = self.scrape_destinations_array
  self.add_destinations(destinations_array)
end

#scrape_destinations_arrayObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/top_travel_destinations/region.rb', line 43

def scrape_destinations_array
  html = open(self.region_url)
  doc = Nokogiri::HTML(html)

  destinations_array = []

  doc.css("div#WINNERVIEWER").children.each do |destination|
    name = destination.search("div.mainName a").text
    description = destination.search("div.descr_lb").text      
    
    if name != "" && description !=""
      destinations_array << {
        :name => name,
        :description => description}
    end
  end #end iterator
  destinations_array
end