Class: BizRatr::Business

Inherits:
Object
  • Object
show all
Defined in:
lib/bizratr/business.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, lon, name) ⇒ Business

Returns a new instance of Business.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/bizratr/business.rb', line 5

def initialize(lat, lon, name)
  @ids = {}
  @checkins = {}
  @users = {}
  @likes = {}
  @ratings = {}
  @review_counts = {}
  @categories = {}
  @coords = [lat, lon]
  @name = name
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def address
  @address
end

#categoriesObject

Returns the value of attribute categories.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def categories
  @categories
end

#checkinsObject

Returns the value of attribute checkins.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def checkins
  @checkins
end

#cityObject

Returns the value of attribute city.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def city
  @city
end

#coordsObject

Returns the value of attribute coords.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def coords
  @coords
end

#countryObject

Returns the value of attribute country.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def country
  @country
end

#idsObject

Returns the value of attribute ids.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def ids
  @ids
end

#likesObject

Returns the value of attribute likes.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def likes
  @likes
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def name
  @name
end

#phoneObject

Returns the value of attribute phone.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def phone
  @phone
end

#ratingsObject

Returns the value of attribute ratings.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def ratings
  @ratings
end

#review_countsObject

Returns the value of attribute review_counts.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def review_counts
  @review_counts
end

#stateObject

Returns the value of attribute state.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def state
  @state
end

#twitterObject

Returns the value of attribute twitter.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def twitter
  @twitter
end

#usersObject

Returns the value of attribute users.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def users
  @users
end

#zipObject

Returns the value of attribute zip.



3
4
5
# File 'lib/bizratr/business.rb', line 3

def zip
  @zip
end

Instance Method Details

#==(other) ⇒ Object



121
122
123
124
125
126
127
# File 'lib/bizratr/business.rb', line 121

def ==(other)
  return false if other.nil?
  return true if any_equal_ids?(other)
  return true if @phone == other.phone and not @phone.nil?
  return true if distance_to(other) < 0.3 and name_distance_to(other) < 0.4
  false
end

#add_categories(connector, categories) ⇒ Object



74
75
76
# File 'lib/bizratr/business.rb', line 74

def add_categories(connector, categories)
  @categories[connector] = categories
end

#add_checkins(connector, checkins) ⇒ Object



66
67
68
# File 'lib/bizratr/business.rb', line 66

def add_checkins(connector, checkins)
  @checkins[connector] = checkins
end

#add_id(connector, id) ⇒ Object



62
63
64
# File 'lib/bizratr/business.rb', line 62

def add_id(connector, id)
  @ids[connector] = id
end

#add_likes(connector, likes) ⇒ Object



83
84
85
# File 'lib/bizratr/business.rb', line 83

def add_likes(connector, likes)
  @users[connector] = likes
end

#add_rating(connector, rating) ⇒ Object



87
88
89
# File 'lib/bizratr/business.rb', line 87

def add_rating(connector, rating)
  @ratings[connector] = rating
end

#add_review_counts(connector, counts) ⇒ Object



91
92
93
# File 'lib/bizratr/business.rb', line 91

def add_review_counts(connector, counts)
  @review_counts[connector] = counts
end

#add_users(connector, users) ⇒ Object



70
71
72
# File 'lib/bizratr/business.rb', line 70

def add_users(connector, users)
  @users[connector] = users
end

#any_equal_ids?(other) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
100
# File 'lib/bizratr/business.rb', line 95

def any_equal_ids?(other)
  @ids.each { |k,v|
    return true if other.ids[k] == v
  }
  false
end

#distance_to(other) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bizratr/business.rb', line 102

def distance_to(other)
  rpd = 0.017453293  #  PI/180
  dlat = other.coords[0] - @coords[0]
  dlon = other.coords[1] - @coords[1]
  dlon_rad = dlon * rpd 
  dlat_rad = dlat * rpd
  lat1_rad = @coords[0] * rpd
  lon1_rad = @coords[1] * rpd
  lat2_rad = other.coords[0] * rpd
  lon2_rad = other.coords[1] * rpd
  a = (Math.sin(dlat_rad/2))**2 + Math.cos(lat1_rad) * Math.cos(lat2_rad) * (Math.sin(dlon_rad/2))**2
  2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a)) * 3956 # 3956 is the radius of the great circle in miles
end

#flattened_categoriesObject

Get all categories from all connectors.



79
80
81
# File 'lib/bizratr/business.rb', line 79

def flattened_categories
  @categories.values.flatten.map { |c| c.downcase }.uniq
end

#merge(other) ⇒ Object



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

def merge(other)
  @ids = @ids.merge(other.ids)
  @phone ||= other.phone
  @address ||= other.address
  @state ||= other.state
  @country ||= other.country
  @zip ||= other.zip
  @twitter ||= other.twitter
  @city ||= other.city
  @checkins = @checkins.merge(other.checkins)
  @users = @users.merge(other.users)
  @likes = @likes.merge(other.likes)
  @ratings = @ratings.merge(other.ratings)
  @review_counts = @review_counts.merge(other.review_counts)
  @coords[0] = (@coords[0].to_f + other.coords[0].to_f) / 2.0
  @coords[1] = (@coords[1].to_f + other.coords[1].to_f) / 2.0
  return self
end

#name_distance_to(other) ⇒ Object



116
117
118
119
# File 'lib/bizratr/business.rb', line 116

def name_distance_to(other)
  name = other.is_a?(Business) ? other.name : other
  Levenshtein::normalized_distance(@name, name)
end

#ratingObject



23
24
25
# File 'lib/bizratr/business.rb', line 23

def rating
  @ratings.values.inject(:+) / @ratings.length
end

#to_sObject



17
18
19
20
21
# File 'lib/bizratr/business.rb', line 17

def to_s
  attrs = [:name, :phone, :address, :state, :country, :zip, :twitter, :ids, :checkins, :users, :likes, :ratings, :review_counts, :coords, :city, :categories]
  args = attrs.map { |k| "#{k.to_s}=#{send(k)}" }.join(", ")
  "<Business [#{args}]>"
end

#total_checkinsObject



35
36
37
# File 'lib/bizratr/business.rb', line 35

def total_checkins
  @checkins.values.inject { |a,b| a+b } || 0
end

#total_likesObject



39
40
41
# File 'lib/bizratr/business.rb', line 39

def total_likes
  @likes.values.inject { |a,b| a+b } || 0
end

#total_reviewsObject



31
32
33
# File 'lib/bizratr/business.rb', line 31

def total_reviews
  @review_counts.values.inject { |a,b| a+b } || 0
end

#total_usersObject



27
28
29
# File 'lib/bizratr/business.rb', line 27

def total_users
  @users.values.inject { |a,b| a+b } || 0
end