social_framework 0.0.3 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -33
- data/app/helpers/social_framework/network_helper.rb +31 -14
- data/app/helpers/social_framework/route_helper.rb +81 -74
- data/app/helpers/social_framework/schedule_helper.rb +2 -1
- data/app/models/social_framework/event.rb +18 -14
- data/app/models/social_framework/user.rb +7 -3
- data/db/migrate/20160406165523_create_social_framework_participant_events.rb +2 -0
- data/db/migrate/20160516122239_create_social_framework_routes.rb +1 -0
- data/db/migrate/20160516122649_create_social_framework_locations.rb +2 -2
- data/db/tmp/migrate/20160406165523_create_social_framework_participant_events.rb +2 -0
- data/db/tmp/migrate/20160516122239_create_social_framework_routes.rb +1 -0
- data/db/tmp/migrate/20160516122649_create_social_framework_locations.rb +2 -2
- data/lib/generators/templates/initializers/social_framework.rb +0 -6
- data/lib/social_framework.rb +0 -8
- data/lib/social_framework/graphs/graph_elements.rb +3 -1
- data/lib/social_framework/version.rb +1 -1
- data/spec/controllers/social_framework/sessions_controller_spec.rb +10 -0
- data/spec/dummy/db/schema.rb +12 -10
- data/spec/dummy/log/development.log +871 -0
- data/spec/dummy/log/test.log +0 -0
- data/spec/dummy/social_framework_dev +0 -0
- data/spec/dummy/social_framework_test +0 -0
- data/spec/helpers/social_framework/route_helper_spec.rb +64 -35
- data/spec/models/social_framework/event_spec.rb +18 -4
- data/spec/models/social_framework/user_spec.rb +7 -0
- data/spec/spec_helper.rb +3 -0
- metadata +2 -5
- data/db/tmp/migrate/20160129004111_devise_create_social_framework_users.rb +0 -44
- data/spec/dummy/script.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b68a8daea3ad1c3e49e41c401b8b6a1d52c0315
|
4
|
+
data.tar.gz: 95a801e3d44a12c5515580a96bc6eea370fbff8f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98ddf314dd490d392fca2165a2ecc9b18b5dd19dd5f58bb16b922870adb318a107d95a43f569ea2a52506fe2d8ab43c5bbac4b574579f2093897dcdc74799ba0
|
7
|
+
data.tar.gz: 813d11b7f320a82cea98618059d369f299fa5ebf9d9d8f9620b56fe7db2a37df713bb8e5910dd4cfb4d263feebd879664df354f7e376834ca2345a48374af0c8
|
data/README.md
CHANGED
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
> SocialFramework is divided into three modules, which are: Users, Routes and Schedulers.
|
18
18
|
In Users module the principal resources to users are provided, like authentication, register, relationships and searchs.
|
19
|
-
In Routes the Framework provides resources to
|
19
|
+
In Routes module the Framework provides resources to check the compatibility routes.
|
20
20
|
And in Schedulers, provides resources to define schedules to users and attempts to relate this schedules.
|
21
21
|
|
22
22
|
> Therefore, the SocialFramework can help build general or specifics social networks in a way faster and practical and without not worry with recurring problems in this type of system.
|
@@ -30,8 +30,7 @@ And in Schedulers, provides resources to define schedules to users and attempts
|
|
30
30
|
gem 'social_framework'
|
31
31
|
```
|
32
32
|
|
33
|
-
>
|
34
|
-
After adding gem in your Gemfile, intall it with the command:
|
33
|
+
> After adding the gem in your Gemfile, intall it with the command:
|
35
34
|
|
36
35
|
```console
|
37
36
|
bundle install
|
@@ -43,10 +42,10 @@ bundle install
|
|
43
42
|
# Getting started
|
44
43
|
|
45
44
|
> The SocialFramework is based on Devise, which is a flexible authentication solution for Rails. For a full documentation to Devise see: https://github.com/plataformatec/devise.
|
46
|
-
The User class already is implemented in SocialFramework and some changes have been applied, like adding username attribute and the behaviors to relationships
|
45
|
+
The User class already is implemented in SocialFramework and some changes have been applied, like adding username attribute and the behaviors to relationships between users.
|
47
46
|
The controllers and views of the Devise also has been changed to add new features.
|
48
47
|
|
49
|
-
> Initially, some files should be added to app. These files represent the settings to SocialFramework and Devise
|
48
|
+
> Initially, some files should be added to app. These files represent the settings to SocialFramework and Devise as initializers, the i18n file to Devise, the routes and the views registrations and sessions to create and authenticate users.
|
50
49
|
To this you should execute:
|
51
50
|
|
52
51
|
```console
|
@@ -147,7 +146,7 @@ devise_for :users, class_name: 'OtherUserClass',
|
|
147
146
|
## Configuring Migrations
|
148
147
|
|
149
148
|
> The User class provides the default attributes username, email and password.
|
150
|
-
To add or remove attributes to this or any other class you can add the Migrate in
|
149
|
+
To add or remove attributes to this or any other class you can add the Migrate in your app.
|
151
150
|
The SocialFramework provides a generator to do this, in this case you just need execute:
|
152
151
|
|
153
152
|
```console
|
@@ -194,7 +193,7 @@ All Devise controllers in SocialFramework have prefix Users.
|
|
194
193
|
## Configuring Routes
|
195
194
|
|
196
195
|
> When you override some Devise controller you must be define that new controller in your routes.
|
197
|
-
This can be done changing path to controller in
|
196
|
+
This can be done changing path to controller in devise_for, like this:
|
198
197
|
|
199
198
|
```ruby
|
200
199
|
devise_for :users, class_name: 'SocialFramework::User',
|
@@ -203,7 +202,7 @@ devise_for :users, class_name: 'SocialFramework::User',
|
|
203
202
|
passwords: 'users/passwords'}
|
204
203
|
```
|
205
204
|
|
206
|
-
>
|
205
|
+
> The registrations controller was replaced with a new controller and that controller was added in routes.
|
207
206
|
|
208
207
|
----
|
209
208
|
# Configuring Views
|
@@ -214,8 +213,6 @@ devise_for :users, class_name: 'SocialFramework::User',
|
|
214
213
|
rails generate social_framework:views
|
215
214
|
```
|
216
215
|
|
217
|
-
> This command will add all views to your app.
|
218
|
-
|
219
216
|
> To add specific views you can use '-v' parameter and pass the views names, like this:
|
220
217
|
|
221
218
|
```console
|
@@ -229,12 +226,12 @@ Initially the SocialFramework add views registrations and sessions to your app p
|
|
229
226
|
----
|
230
227
|
# SocialFramework's Modules
|
231
228
|
|
232
|
-
> Currently the SocialFramework has
|
229
|
+
> Currently the SocialFramework has modules Users, Schedules and Routes.
|
233
230
|
|
234
231
|
----
|
235
232
|
## Users Module
|
236
233
|
|
237
|
-
> This module provides the principal logic to social networks, like create, confirm and remove relationships between users, beyond searchs, registers, updates and
|
234
|
+
> This module provides the principal logic to social networks, like create, confirm and remove relationships between users, beyond searchs, registers, updates and authentications.
|
238
235
|
|
239
236
|
> The relationships structure was built with a many to many association between Users through Edges. The Edges has user origin, user destiny, status can be active or inactive, if is bidirectional or not to specify relationships like two-way or one-way and label with the relationship name.
|
240
237
|
May be exists multiple relationships between the same users. Each relationship is represent with an Edge.
|
@@ -274,10 +271,10 @@ The Graph can be accessed from the method 'graph' present in User class.
|
|
274
271
|
In sign_in action the Graph is built with the User logged like root. The Graph is built until the depth specified in initializer 'social_framework.rb' in variable 'depth_to_build', the value default is three. The following is the method signature to build graph.
|
275
272
|
|
276
273
|
```ruby
|
277
|
-
build(root, attributes = [:username, :email], relationships = "all")
|
274
|
+
build(root, attributes = [:username, :email, :title], relationships = "all")
|
278
275
|
```
|
279
276
|
|
280
|
-
> The attributes are user attributes thats will be mapped to vertices, for default contains 'username'
|
277
|
+
> The parameter root is a User that will be the root of graph, the attributes are user and event attributes thats will be mapped to vertices, for default contains 'username', 'email' and 'title'. Relationships are the type of relationships to build the Graph, should be a string or an array, "all" is to build Graph with any relationships. In sign_in action the Graph is built with attributes 'username', 'email' and 'title', beyond 'id'.
|
281
278
|
|
282
279
|
> With the Graph built it's possible suggest relationships. To this it's analyzed the third level in graph finding common relationships with type specified in initializer 'social_framework.rb' in variable 'relationship_type_to_suggest', the value default is 'friend', the variable 'amount_relationship_to_suggest' specifies the value to use to suggest relationships, the default value is five. The following is the method signature.
|
283
280
|
|
@@ -294,14 +291,14 @@ suggest_relationships(type_relationships = SocialFramework.relationship_type_to_
|
|
294
291
|
search(map, search_in_progress = false, elements_number = SocialFramework.elements_number_to_search)
|
295
292
|
```
|
296
293
|
|
297
|
-
> It's passed a map to be used in search, this map represent keys and values to compare vertices, for example, the map '{username: 'user', email: 'user'}' will cause an search with any vertice thats contains the string 'user' in username or email.
|
298
|
-
The param 'search_in_progress' is used to continue a search finding more results, to do this pass true. And, the param 'elements_number' define the quantity of
|
294
|
+
> It's passed a map to be used in search, this map represent keys and values to compare vertices, for example, the map '{username: 'user', email: 'user', title: 'event'}' will cause an search with any vertice thats contains the string 'user' in username or email or thats contains the string 'event' in title.
|
295
|
+
The param 'search_in_progress' is used to continue a search finding more results, to do this pass true. And, the param 'elements_number' define the quantity of results to return, this value is specified in initializer 'social_framework.rb' in variable 'elements_number_to_search', the value default is five.
|
299
296
|
|
300
297
|
> An example to continue searchs is shown below. For default, when you continue a search the 'elements_number' param is used to increase the maximum size to results found.
|
301
|
-
In this case the first call returns the first five
|
298
|
+
In this case the first call returns the first five elements found in graph, the second call returns more ten elements and the final size is fifteen. The return is a hash with two keys, the first is 'users' and its value is found array of users and the second is 'events' and its value is an array of events found.
|
302
299
|
|
303
300
|
```ruby
|
304
|
-
map = {username: 'user', email: 'user'}
|
301
|
+
map = {username: 'user', email: 'user', title: 'event'}
|
305
302
|
graph.search(map)
|
306
303
|
graph.search(map, true, 10)
|
307
304
|
```
|
@@ -309,21 +306,21 @@ graph.search(map, true, 10)
|
|
309
306
|
> You can change the default behavior to continue searchs passing a block to method, this block will indicate how the 'elements_number' must be increased. For example:
|
310
307
|
|
311
308
|
```ruby
|
312
|
-
map = {username: 'user', email: 'user'}
|
309
|
+
map = {username: 'user', email: 'user', title: 'event'}
|
313
310
|
graph.search(map, false, 1)
|
314
311
|
graph.search(map, true) { |elements_number| elements_number *= 2 }
|
315
312
|
```
|
316
313
|
|
317
|
-
> In that case the 'elements_number' will double every call search method with this block. Therefore, the first call returns one
|
314
|
+
> In that case the 'elements_number' will double every call search method with this block. Therefore, the first call returns one element due to the value passed, the second two, the third four, and so on.
|
318
315
|
|
319
|
-
> When the search reaches the end of the Graph and not yet found all required
|
316
|
+
> When the search reaches the end of the Graph and not yet found all required elements an search in database is done to complete the hash.
|
320
317
|
|
321
318
|
----
|
322
319
|
## Schedules Module
|
323
320
|
|
324
321
|
> This module provides the logic to work with the schedule of social network users, how to create, enter, invite, confirm, exit and remove events.
|
325
322
|
|
326
|
-
> The struct of relationships was built as follow: An Schedule belongs to a user and have a many to many association with Event through a Participant_Event, which has a confirmed attribute indicating whether the user has confirmed the event or is pending. Beyond an event be able to be present in various schedules, an event have a title, a description, a date and start time, a date and finish time, can or can not have an associated route and if the
|
323
|
+
> The struct of relationships was built as follow: An Schedule belongs to a user and have a many to many association with Event through a Participant_Event, which has a confirmed attribute indicating whether the user has confirmed the event or is pending. Beyond an event be able to be present in various schedules, an event have a title, a description, a date and start time, a date and finish time, can or can not have an associated route and if the event is particular an another user will only be able participate in the event if it is invited, if not particular any user can enter the event.
|
327
324
|
|
328
325
|
> To create a new event uses the method 'create_event', defined in Schedule Class. The following is the method signature.
|
329
326
|
|
@@ -381,7 +378,7 @@ invite(inviting, guest, relationship = SocialFramework.relationship_type_to_invi
|
|
381
378
|
|
382
379
|
> It is necessary to pass as a parameter the user inviting and the user guest, if not passed the type of the relationship between the users will be used the type defined in the configuration file. To invite an user to an event the inviting should be confirmed in event, have the permition to 'invite' and have the guest into your circle of relationships with the specificed type of relationship, should use the type 'all' to any type of relationship.
|
383
380
|
|
384
|
-
> To change the role of a participant in an event should invoke the method 'change_participant_role', defined in Schedule Class. The following is the method signature
|
381
|
+
> To change the role of a participant in an event should invoke the method 'change_participant_role', defined in Schedule Class. The following is the method signature:
|
385
382
|
|
386
383
|
```ruby
|
387
384
|
change_participant_role(maker, participant, action)
|
@@ -397,7 +394,7 @@ remove_participant(remover, participant)
|
|
397
394
|
|
398
395
|
> It is necessary to pass as a parameter the remover will remove an user and the participant will be removed from the event. To the removal is performed successfully the remover must have permission to execute the action.
|
399
396
|
|
400
|
-
> To add a route in an event should invoke the method 'add_route', defined in
|
397
|
+
> To add a route in an event should invoke the method 'add_route', defined in Event Class. The following is the method signature.
|
401
398
|
|
402
399
|
```ruby
|
403
400
|
add_route(user, route)
|
@@ -405,7 +402,15 @@ add_route(user, route)
|
|
405
402
|
|
406
403
|
> It is necessary to pass as a parameter the user will add a route in an event and the route will be added. To the addition is performed successfully the user must have permission to ':add_route'.
|
407
404
|
|
408
|
-
>
|
405
|
+
> To see all users in event confirmed or no it's necessarily call the method 'users' in event class, with the following signature:
|
406
|
+
|
407
|
+
```ruby
|
408
|
+
users(confirmed = true, role = "all")
|
409
|
+
```
|
410
|
+
|
411
|
+
> The param 'confirmed' is used to specify is users returned are confirmed in event or no. The param 'role' is used to specify users role, the value "all" define all papers.
|
412
|
+
|
413
|
+
> The Schedules Module uses a graph to provide slotes time ordered considering the greater amount of weight slotes. This functionality is present in the 'ScheduleHelper', which includes the class 'ScheduleContext' responssible to invoke the schedule module methods.
|
409
414
|
|
410
415
|
> In this graph the vertices with the user type only have connection with the vertices with the slot type, and the vertices with the slote type only have connection with the vertices with the user type, thus forming a bipartite graph.
|
411
416
|
|
@@ -433,7 +438,7 @@ build(users, start_day, finish_day, start_hour = Time.parse("00:00"), finish_hou
|
|
433
438
|
> Currently, it's available to users create routes. That can be done with the following method, present in User class:
|
434
439
|
|
435
440
|
```ruby
|
436
|
-
create_route(title, distance, locations, mode_of_travel = "driving")
|
441
|
+
create_route(title, distance, locations, mode_of_travel = "driving", accepted_deviation = 0)
|
437
442
|
```
|
438
443
|
|
439
444
|
> The params are route title, route distance, locations is a array with all points of latitude and longitude to build route, must be passed like this:
|
@@ -443,19 +448,15 @@ locations = [{latitude: -15.792740000000002, longitude: -47.876360000000005},
|
|
443
448
|
{latitude: -15.792520000000001, longitude: -47.876900000000006}]
|
444
449
|
```
|
445
450
|
|
446
|
-
> And mode_of_travel represents the type to build route, can be 'driving', 'bycicling', 'walking' or 'transit', the value default is 'driving' to travels with car.
|
451
|
+
> And mode_of_travel represents the type to build route, can be 'driving', 'bycicling', 'walking' or 'transit', the value default is 'driving' to travels with car. The param accepted_deviation has zero like default value and, represent the maximum accepted deviation to some route.
|
447
452
|
|
448
453
|
> That module also provides a feature to verify the compatibility between two routes. To do this call the method 'compare_routes' present in 'route_helper.rb'.
|
449
454
|
|
450
455
|
```ruby
|
451
|
-
compare_routes(principal_route, secondary_route
|
452
|
-
principal_deviation = SocialFramework.principal_deviation,
|
453
|
-
secondary_deviation = SocialFramework.secondary_deviation)
|
456
|
+
compare_routes(principal_route, secondary_route)
|
454
457
|
```
|
455
458
|
|
456
|
-
> This method is used to verify if two routes can be connected in just one route. The param principal_route represent the route that will be updated to auxliar the secondary_route if possible, when the principal_route can not be updated the on tries to update the secondary_route to achieve the goal.
|
457
|
-
The others params principal_deviation and secondary_deviation represent a map with mode_of_travel and maximum deviation to routes, the maximum deviation is the distance that a route can be diverted to achieve the goal.
|
458
|
-
The default values to principal_deviation and secondary_deviation are present in initializer 'social_framework.rb' and can be updated.
|
459
|
+
> This method is used to verify if two routes can be connected in just one route. The param principal_route represent the route that will be updated to auxliar the secondary_route if possible, when the principal_route can not be updated the on tries to update the secondary_route to achieve the goal. The method uses the attribute `accepted_deviation' in routes to verify if routes can be updated.
|
459
460
|
|
460
461
|
> It's returned a map with the compatibility information, like this:
|
461
462
|
|
@@ -20,23 +20,17 @@ module SocialFramework
|
|
20
20
|
# ====== Params:
|
21
21
|
# +id+:: +Integer+ Id of the user logged
|
22
22
|
# +elements_factory+:: +String+ Represent the factory class name to build
|
23
|
-
# Returns
|
23
|
+
# Returns NotImplementedError
|
24
24
|
def self.get_instance(id, elements_factory)
|
25
|
-
|
26
|
-
|
27
|
-
if @@instances[id].nil?
|
28
|
-
@@instances[id] = GraphStrategyDefault.new elements_factory
|
29
|
-
end
|
30
|
-
|
31
|
-
return @@instances[id]
|
25
|
+
raise 'Must implement method in subclass'
|
32
26
|
end
|
33
27
|
|
34
28
|
# Destroy graph instance with id passed
|
35
29
|
# ====== Params:
|
36
30
|
# +id+:: +Integer+ Id of the user logged
|
37
|
-
# Returns
|
31
|
+
# Returns NotImplementedError
|
38
32
|
def destroy(id)
|
39
|
-
|
33
|
+
raise 'Must implement method in subclass'
|
40
34
|
end
|
41
35
|
|
42
36
|
# Mount a graph from an user
|
@@ -83,6 +77,29 @@ module SocialFramework
|
|
83
77
|
|
84
78
|
# Represent the network on a Graph, with Vertices and Edges
|
85
79
|
class GraphStrategyDefault < GraphStrategy
|
80
|
+
# Get graph instance to user logged
|
81
|
+
# ====== Params:
|
82
|
+
# +id+:: +Integer+ Id of the user logged
|
83
|
+
# +elements_factory+:: +String+ Represent the factory class name to build
|
84
|
+
# Returns Graph object
|
85
|
+
def self.get_instance(id, elements_factory)
|
86
|
+
@@instances ||= {}
|
87
|
+
|
88
|
+
if @@instances[id].nil?
|
89
|
+
@@instances[id] = GraphStrategyDefault.new elements_factory
|
90
|
+
end
|
91
|
+
|
92
|
+
return @@instances[id]
|
93
|
+
end
|
94
|
+
|
95
|
+
# Destroy graph instance with id passed
|
96
|
+
# ====== Params:
|
97
|
+
# +id+:: +Integer+ Id of the user logged
|
98
|
+
# Returns Graph instance removed
|
99
|
+
def destroy(id)
|
100
|
+
@@instances.delete(id)
|
101
|
+
end
|
102
|
+
|
86
103
|
# Mount a graph from an user
|
87
104
|
# ====== Params:
|
88
105
|
# +root+:: +User+ Root user to mount graph
|
@@ -125,7 +142,7 @@ module SocialFramework
|
|
125
142
|
# +map+:: +Hash+ with keys and values to compare
|
126
143
|
# +search_in_progress+:: +Boolean+ to continue if true or start a new search
|
127
144
|
# +elements_number+:: +Integer+ to limit max search result
|
128
|
-
# Returns
|
145
|
+
# Returns Hash with users and events found
|
129
146
|
def search(map, search_in_progress = false, elements_number = SocialFramework.elements_number_to_search)
|
130
147
|
return {users: @users_found, events: @events_found} if @finished_search and search_in_progress == true
|
131
148
|
|
@@ -150,7 +167,7 @@ module SocialFramework
|
|
150
167
|
# ====== Params:
|
151
168
|
# +type_relationships+:: +Array+ labels to find relationships, can be multiple in array or just one in a simple String
|
152
169
|
# +amount_relationships+:: +Integer+ quantity of relationships to suggest a new relationship
|
153
|
-
# Returns +Array+ with
|
170
|
+
# Returns +Array+ with users to suggestions
|
154
171
|
def suggest_relationships(type_relationships = SocialFramework.relationship_type_to_suggest,
|
155
172
|
amount_relationships = SocialFramework.amount_relationship_to_suggest)
|
156
173
|
|
@@ -440,7 +457,7 @@ module SocialFramework
|
|
440
457
|
# +map+:: +Hash+ with keys and values to compare
|
441
458
|
# +search_in_progress+:: +Boolean+ to continue if true or start a new search
|
442
459
|
# +elements_number+:: +Integer+ to limit max search result
|
443
|
-
# Returns
|
460
|
+
# Returns Hash with users and events found
|
444
461
|
def search(map, search_in_progress = false, elements_number = SocialFramework.elements_number_to_search)
|
445
462
|
@graph.search(map, search_in_progress, elements_number)
|
446
463
|
end
|
@@ -449,7 +466,7 @@ module SocialFramework
|
|
449
466
|
# ====== Params:
|
450
467
|
# +type_relationships+:: +Array+ labels to find relationships, can be multiple in array or just one in a simple String
|
451
468
|
# +amount_relationships+:: +Integer+ quantity of relationships to suggest a new relationship
|
452
|
-
# Returns +Array+ with
|
469
|
+
# Returns +Array+ with users to suggestions
|
453
470
|
def suggest_relationships(type_relationships = SocialFramework.relationship_type_to_suggest,
|
454
471
|
amount_relationships = SocialFramework.amount_relationship_to_suggest)
|
455
472
|
@graph.suggest_relationships(type_relationships, amount_relationships)
|
@@ -9,10 +9,8 @@ module SocialFramework
|
|
9
9
|
# ====== Params:
|
10
10
|
# +principal_route+:: +Route+ who gives a lift
|
11
11
|
# +secondary_route+:: +Route+ who hitchhike
|
12
|
-
# +principal_deviation+:: +Hash+ with maximum deviation and mode of travel to principal route
|
13
|
-
# +secondary_deviation+:: +Hash+ with maximum deviation and mode of travel to secondary route
|
14
12
|
# Returns NotImplementedError
|
15
|
-
def compare_routes(principal_route, secondary_route
|
13
|
+
def compare_routes(principal_route, secondary_route)
|
16
14
|
raise 'Must implement method in subclass'
|
17
15
|
end
|
18
16
|
end
|
@@ -20,19 +18,16 @@ module SocialFramework
|
|
20
18
|
# Contains the methods to match routes
|
21
19
|
class RouteStrategyDefault < RouteStrategy
|
22
20
|
|
21
|
+
RATIO_OF_EARTH = 6500000
|
22
|
+
|
23
23
|
# Compare the routes to verify if are compatible
|
24
24
|
# ====== Params:
|
25
25
|
# +principal_route+:: +Route+ who gives a lift
|
26
26
|
# +secondary_route+:: +Route+ who hitchhike
|
27
|
-
# +principal_deviation+:: +Hash+ with maximum deviation and mode of travel to principal route
|
28
|
-
# +secondary_deviation+:: +Hash+ with maximum deviation and mode of travel to secondary route
|
29
27
|
# Returns Hash with information of compatibility and necessary distances
|
30
|
-
def compare_routes(principal_route, secondary_route
|
31
|
-
principal_deviation = SocialFramework.principal_deviation,
|
32
|
-
secondary_deviation = SocialFramework.secondary_deviation)
|
28
|
+
def compare_routes(principal_route, secondary_route)
|
33
29
|
|
34
|
-
principal_accepted = principal_accepted_deviation(principal_route, secondary_route
|
35
|
-
principal_deviation[:deviation], principal_deviation[:mode])
|
30
|
+
principal_accepted = principal_accepted_deviation(principal_route, secondary_route)
|
36
31
|
|
37
32
|
result = {compatible: false, principal_route: {deviation: :none,
|
38
33
|
distance: 0}, secondary_route: {deviation: :none, distance: 0}}
|
@@ -42,23 +37,18 @@ module SocialFramework
|
|
42
37
|
result[:principal_route][:deviation] = :both
|
43
38
|
result[:principal_route][:distance] = principal_accepted[:distance]
|
44
39
|
else
|
45
|
-
secondary_origin, secondary_destiny, secondary_accepted =
|
46
|
-
|
40
|
+
secondary_origin, secondary_destiny, secondary_accepted =
|
41
|
+
condictions_secondary_deviation(principal_route, secondary_route, principal_accepted)
|
47
42
|
|
48
43
|
if(secondary_origin)
|
49
|
-
result[:
|
50
|
-
|
51
|
-
result[:principal_route][:distance] = principal_accepted[:distance_destiny]
|
52
|
-
result[:secondary_route][:deviation] = :origin
|
53
|
-
result[:secondary_route][:distance] = secondary_accepted[:distance_origin]
|
44
|
+
build_result(result, true, :destiny, principal_accepted[:distance_destiny],
|
45
|
+
:origin, secondary_accepted[:distance_origin])
|
54
46
|
elsif(secondary_destiny)
|
55
|
-
result[:
|
56
|
-
|
57
|
-
result[:principal_route][:distance] = principal_accepted[:distance_origin]
|
58
|
-
result[:secondary_route][:deviation] = :destiny
|
59
|
-
result[:secondary_route][:distance] = secondary_accepted[:distance_destiny]
|
47
|
+
build_result(result, true, :origin, principal_accepted[:distance_origin],
|
48
|
+
:destiny, secondary_accepted[:distance_destiny])
|
60
49
|
end
|
61
50
|
end
|
51
|
+
|
62
52
|
return result
|
63
53
|
end
|
64
54
|
|
@@ -69,60 +59,63 @@ module SocialFramework
|
|
69
59
|
# +principal_route+:: +Route+ who gives a lift
|
70
60
|
# +secondary_route+:: +Route+ who hitchhike
|
71
61
|
# +principal_accepted_deviation+:: +Hash+ with point and smallest deviation
|
72
|
-
# +secondary_deviation+:: +Hash+ with maximum deviation and mode of travel to secondary route
|
73
62
|
# Returns If the secondary can change origin, If the secondary can change destiny and A Hash with point and smallest deviation
|
74
|
-
def condictions_secondary_deviation(principal_route, secondary_route, principal_accepted_deviation
|
75
|
-
secondary_accepted = secondary_accepted_deviation(principal_route, secondary_route
|
76
|
-
secondary_deviation[:deviation], secondary_deviation[:mode])
|
63
|
+
def condictions_secondary_deviation(principal_route, secondary_route, principal_accepted_deviation)
|
64
|
+
secondary_accepted = secondary_accepted_deviation(principal_route, secondary_route)
|
77
65
|
|
78
|
-
|
79
|
-
|
66
|
+
principal_destiny = (principal_accepted_deviation[:accept] == :any or
|
67
|
+
principal_accepted_deviation[:accept] == :destiny)
|
68
|
+
|
69
|
+
principal_origin = (principal_accepted_deviation[:accept] == :any or
|
70
|
+
principal_accepted_deviation[:accept] == :origin)
|
80
71
|
|
81
|
-
|
72
|
+
secondary_any = (secondary_accepted[:accept] == :both or secondary_accepted[:accept] == :any)
|
82
73
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
74
|
+
origin_lower_than_destiny = (secondary_any and (secondary_accepted[:distance_origin] < secondary_accepted[:distance_destiny]))
|
75
|
+
|
76
|
+
secondary_origin = ((principal_destiny and secondary_accepted[:accept] == :origin) or
|
77
|
+
(secondary_any and principal_destiny))
|
87
78
|
|
88
|
-
|
89
|
-
|
79
|
+
secondary_destiny = ((principal_origin and secondary_accepted[:accept] == :destiny) or
|
80
|
+
(secondary_any and principal_origin))
|
90
81
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
82
|
+
if secondary_origin and secondary_destiny
|
83
|
+
secondary_origin = origin_lower_than_destiny
|
84
|
+
secondary_destiny = (not origin_lower_than_destiny)
|
85
|
+
end
|
95
86
|
|
96
|
-
|
87
|
+
return secondary_origin, secondary_destiny, secondary_accepted
|
97
88
|
end
|
98
89
|
|
99
90
|
# Verify the deviations which can be made on principal route
|
100
91
|
# ====== Params:
|
101
92
|
# +principal_route+:: +Route+ who gives a lift
|
102
93
|
# +secondary_route+:: +Route+ who hitchhike
|
103
|
-
# +deviation+:: +Integer+ maximum deviation accepeted to principal route
|
104
|
-
# +mode_of_travel+:: +String+ specify mode of travel
|
105
94
|
# Returns Hash with point and smallest deviation
|
106
|
-
def principal_accepted_deviation(principal_route, secondary_route
|
95
|
+
def principal_accepted_deviation(principal_route, secondary_route)
|
107
96
|
distance_with_both = get_distance_with_waypoints(principal_route.locations.first,
|
108
97
|
principal_route.locations.last, [secondary_route.locations.first, secondary_route.locations.last],
|
109
|
-
mode_of_travel)
|
98
|
+
principal_route.mode_of_travel)
|
110
99
|
|
111
|
-
if((not distance_with_both.nil?) and
|
100
|
+
if((not distance_with_both.nil?) and
|
101
|
+
distance_with_both <= (principal_route.distance + principal_route.accepted_deviation))
|
112
102
|
return {accept: :both, distance: distance_with_both}
|
113
103
|
else
|
114
104
|
distance_with_origin = get_distance_with_waypoints(principal_route.locations.first,
|
115
|
-
principal_route.locations.last, [secondary_route.locations.first], mode_of_travel)
|
105
|
+
principal_route.locations.last, [secondary_route.locations.first], principal_route.mode_of_travel)
|
116
106
|
|
117
107
|
distance_with_destiny = get_distance_with_waypoints(principal_route.locations.first,
|
118
|
-
principal_route.locations.last, [secondary_route.locations.last], mode_of_travel)
|
108
|
+
principal_route.locations.last, [secondary_route.locations.last], principal_route.mode_of_travel)
|
119
109
|
|
120
|
-
if((not distance_with_origin.nil?) and
|
121
|
-
|
110
|
+
if((not distance_with_origin.nil?) and
|
111
|
+
distance_with_origin <= (principal_route.distance + principal_route.accepted_deviation) and
|
112
|
+
distance_with_destiny <= (principal_route.distance + principal_route.accepted_deviation))
|
122
113
|
return {accept: :any, distance_origin: distance_with_origin, distance_destiny: distance_with_destiny}
|
123
|
-
elsif((not distance_with_origin.nil?) and
|
114
|
+
elsif((not distance_with_origin.nil?) and
|
115
|
+
distance_with_origin <= (principal_route.distance + principal_route.accepted_deviation))
|
124
116
|
return {accept: :origin, distance_origin: distance_with_origin}
|
125
|
-
elsif((not distance_with_destiny.nil?) and
|
117
|
+
elsif((not distance_with_destiny.nil?) and
|
118
|
+
distance_with_destiny <= (principal_route.distance + principal_route.accepted_deviation))
|
126
119
|
return {accept: :destiny, distance_destiny: distance_with_destiny}
|
127
120
|
else
|
128
121
|
return {accept: :none}
|
@@ -134,21 +127,22 @@ module SocialFramework
|
|
134
127
|
# ====== Params:
|
135
128
|
# +principal_route+:: +Route+ who gives a lift
|
136
129
|
# +secondary_route+:: +Route+ who hitchhike
|
137
|
-
# +deviation+:: +Integer+ maximum deviation accepeted to secondary route
|
138
|
-
# +mode_of_travel+:: +String+ specify mode of travel
|
139
130
|
# Returns Hash with point and smallest deviation
|
140
|
-
def secondary_accepted_deviation(principal_route, secondary_route
|
141
|
-
points = near_points(principal_route, secondary_route
|
142
|
-
origin_deviation = smallest_distance(points[:origins], secondary_route.locations.first,
|
143
|
-
|
131
|
+
def secondary_accepted_deviation(principal_route, secondary_route)
|
132
|
+
points = near_points(principal_route, secondary_route)
|
133
|
+
origin_deviation = smallest_distance(points[:origins], secondary_route.locations.first,
|
134
|
+
secondary_route.mode_of_travel)
|
135
|
+
destiny_deviation = smallest_distance(points[:destinations], secondary_route.locations.last,
|
136
|
+
secondary_route.mode_of_travel)
|
144
137
|
|
145
|
-
if(
|
138
|
+
if(secondary_route.accepted_deviation >= origin_deviation[:deviation] + destiny_deviation[:deviation])
|
146
139
|
return {accept: :both, distance_origin: origin_deviation[:deviation], distance_destiny: destiny_deviation[:deviation]}
|
147
|
-
elsif(
|
140
|
+
elsif(secondary_route.accepted_deviation >= origin_deviation[:deviation] and
|
141
|
+
secondary_route.accepted_deviation >= destiny_deviation[:deviation])
|
148
142
|
return {accept: :any, distance_origin: origin_deviation[:deviation], distance_destiny: destiny_deviation[:deviation]}
|
149
|
-
elsif(
|
143
|
+
elsif(secondary_route.accepted_deviation >= origin_deviation[:deviation])
|
150
144
|
return {accept: :origin, distance_origin: origin_deviation[:deviation]}
|
151
|
-
elsif(
|
145
|
+
elsif(secondary_route.accepted_deviation >= destiny_deviation[:deviation])
|
152
146
|
return {accept: :destiny, distance_destiny: destiny_deviation[:deviation]}
|
153
147
|
end
|
154
148
|
return {accept: :none}
|
@@ -161,12 +155,12 @@ module SocialFramework
|
|
161
155
|
# +mode_of_travel+:: +String+ specify mode of travel
|
162
156
|
# Returns Hash with point and smallest deviation
|
163
157
|
def smallest_distance(origin_points, destiny, mode_of_travel)
|
164
|
-
smallest_distance =
|
158
|
+
smallest_distance = RATIO_OF_EARTH
|
165
159
|
origin_point = nil
|
166
160
|
|
167
161
|
origin_points.each do |origin|
|
168
162
|
distance = get_distance(origin, destiny, mode_of_travel)
|
169
|
-
if
|
163
|
+
if distance < smallest_distance
|
170
164
|
origin_point = origin
|
171
165
|
smallest_distance = distance
|
172
166
|
end
|
@@ -278,9 +272,8 @@ module SocialFramework
|
|
278
272
|
# ====== Params:
|
279
273
|
# +principal_route+:: +Route+ who gives a lift
|
280
274
|
# +secondary_route+:: +Route+ who hitchhike
|
281
|
-
# +secondary_maximum_deviation+:: +Integer+ maximum deviation to who hitchhike
|
282
275
|
# Returns Hash with origins and destinations points
|
283
|
-
def near_points(principal_route, secondary_route
|
276
|
+
def near_points(principal_route, secondary_route)
|
284
277
|
origin = secondary_route.locations.first
|
285
278
|
destiny = secondary_route.locations.last
|
286
279
|
|
@@ -291,12 +284,31 @@ module SocialFramework
|
|
291
284
|
distance_origin = haversine_distance(location, origin)
|
292
285
|
distance_destiny = haversine_distance(location, destiny)
|
293
286
|
|
294
|
-
origins << location if (not distance_origin.nil?) and
|
295
|
-
|
287
|
+
origins << location if (not distance_origin.nil?) and
|
288
|
+
distance_origin <= secondary_route.accepted_deviation
|
289
|
+
destinations << location if (not distance_destiny.nil?) and
|
290
|
+
distance_destiny <= secondary_route.accepted_deviation
|
296
291
|
end
|
297
292
|
|
298
293
|
return {origins: origins, destinations: destinations}
|
299
294
|
end
|
295
|
+
|
296
|
+
# Build results do compare routes
|
297
|
+
# ====== Params:
|
298
|
+
# +result+:: +Hash+ with all results to compare routes
|
299
|
+
# +compatible+:: +Boolean+ result compare routes
|
300
|
+
# +first_deviation+:: +Symbol+ specify where the first route is devious
|
301
|
+
# +first_distance+:: +Integer+ distance total to first route
|
302
|
+
# +second_deviation+:: +Symbol+ specify where the last route is devious
|
303
|
+
# +second_distance+:: +Integer+ distance total to last route
|
304
|
+
# Returns Hash with origins and destinations points
|
305
|
+
def build_result(result, compatible, first_deviation, first_distance, second_deviation, second_distance)
|
306
|
+
result[:compatible] = compatible
|
307
|
+
result[:principal_route][:deviation] = first_deviation
|
308
|
+
result[:principal_route][:distance] = first_distance
|
309
|
+
result[:secondary_route][:deviation] = second_deviation
|
310
|
+
result[:secondary_route][:distance] = second_distance
|
311
|
+
end
|
300
312
|
end
|
301
313
|
|
302
314
|
# Used to define the RouteStrategy class
|
@@ -311,14 +323,9 @@ module SocialFramework
|
|
311
323
|
# ====== Params:
|
312
324
|
# +principal_route+:: +Route+ who gives a lift
|
313
325
|
# +secondary_route+:: +Route+ who hitchhike
|
314
|
-
# +principal_deviation+:: +Hash+ with maximum deviation and mode of travel to principal route
|
315
|
-
# +secondary_deviation+:: +Hash+ with maximum deviation and mode of travel to secondary route
|
316
326
|
# Returns Hash with information of compatibility and necessary distances
|
317
|
-
def compare_routes(principal_route, secondary_route
|
318
|
-
|
319
|
-
secondary_deviation = SocialFramework.secondary_deviation)
|
320
|
-
|
321
|
-
@route.compare_routes(principal_route, secondary_route, principal_deviation, secondary_deviation)
|
327
|
+
def compare_routes(principal_route, secondary_route)
|
328
|
+
@route.compare_routes(principal_route, secondary_route)
|
322
329
|
end
|
323
330
|
end
|
324
331
|
end
|