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(uberclient, lat, lon, name) ⇒ Business

Returns a new instance of Business.



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

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

Instance Attribute Details

#addressObject

Returns the value of attribute address.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def address
  @address
end

#categoriesObject

Returns the value of attribute categories.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def categories
  @categories
end

#checkinsObject

Returns the value of attribute checkins.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def checkins
  @checkins
end

#cityObject

Returns the value of attribute city.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def city
  @city
end

#coordsObject

Returns the value of attribute coords.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def coords
  @coords
end

#countryObject

Returns the value of attribute country.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def country
  @country
end

#idsObject

Returns the value of attribute ids.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def ids
  @ids
end

#likesObject

Returns the value of attribute likes.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def likes
  @likes
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def name
  @name
end

#phoneObject

Returns the value of attribute phone.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def phone
  @phone
end

#ratingsObject

Returns the value of attribute ratings.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def ratings
  @ratings
end

#review_countsObject

Returns the value of attribute review_counts.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def review_counts
  @review_counts
end

#stateObject

Returns the value of attribute state.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def state
  @state
end

#twitterObject

Returns the value of attribute twitter.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def twitter
  @twitter
end

#usersObject

Returns the value of attribute users.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def users
  @users
end

#websiteObject

Returns the value of attribute website.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def website
  @website
end

#zipObject

Returns the value of attribute zip.



5
6
7
# File 'lib/bizratr/business.rb', line 5

def zip
  @zip
end

Instance Method Details

#==(other) ⇒ Object



153
154
155
156
157
158
159
# File 'lib/bizratr/business.rb', line 153

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



106
107
108
# File 'lib/bizratr/business.rb', line 106

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

#add_checkins(connector, checkins) ⇒ Object



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

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

#add_id(connector, id) ⇒ Object



94
95
96
# File 'lib/bizratr/business.rb', line 94

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

#add_likes(connector, likes) ⇒ Object



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

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

#add_rating(connector, rating) ⇒ Object



119
120
121
# File 'lib/bizratr/business.rb', line 119

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

#add_review_counts(connector, counts) ⇒ Object



123
124
125
# File 'lib/bizratr/business.rb', line 123

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

#add_users(connector, users) ⇒ Object



102
103
104
# File 'lib/bizratr/business.rb', line 102

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

#any_equal_ids?(other) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
# File 'lib/bizratr/business.rb', line 127

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

#distance_to(other) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/bizratr/business.rb', line 134

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.



111
112
113
# File 'lib/bizratr/business.rb', line 111

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

#merge(other) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bizratr/business.rb', line 73

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
  @website ||= other.website
  @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
  @categories = @categories.merge(other.categories)
  return self
end

#name_distance_to(other) ⇒ Object



148
149
150
151
# File 'lib/bizratr/business.rb', line 148

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

#ratingObject



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

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

#to_sObject



20
21
22
23
24
# File 'lib/bizratr/business.rb', line 20

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

#total_checkinsObject



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

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

#total_likesObject



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

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

#total_reviewsObject



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

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

#total_usersObject



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

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

#website_likesObject

Get all of the website like information from Facebook. If there’s no website, or an issue, return {} - otherwise you’ll get something of the form “like_count”=>10, “comment_count”=>9, “click_count”=>6



52
53
54
55
56
57
58
59
# File 'lib/bizratr/business.rb', line 52

def website_likes
  fb = @uberclient.get_connector(:facebook)
  # normalize URL first
  url = website_normalized
  return {} if fb.nil? or url.nil?
  results = fb.get_url_likes(url)
  (results.length > 0) ? results.first : {}
end

#website_normalizedObject

remove path and query info from the businesses website (some website’s have blah.com/index.html in some places and blah.com in others and blah.com/ in yet others

  • all three are equivalent)



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bizratr/business.rb', line 38

def website_normalized
  return nil if @website.nil?
  begin
    uri = URI(@website)
    uri.query = nil
    uri.path = ''
    uri.to_s
  rescue
    nil
  end
end