code cleanup and additional spec

stvnrlly 10 anni fa
parent
commit
ae286c3abe
2 ha cambiato i file con 18 aggiunte e 6 eliminazioni
  1. 2 6
      app/models/agents/user_location_agent.rb
  2. 16 0
      spec/models/agents/user_location_agent_spec.rb

+ 2 - 6
app/models/agents/user_location_agent.rb

@@ -73,14 +73,10 @@ module Agents
73 73
     def handle_payload(payload)
74 74
       location = Location.new(payload)
75 75
 
76
-      if interpolated[:accuracy_field].present?
77
-        accuracy_field = interpolated[:accuracy_field]
78
-      else
79
-        accuracy_field = 'accuracy'
80
-      end
76
+      accuracy_field = interpolated[:accuracy_field].presence || 'accuracy'
81 77
 
82 78
       if location.present? && (!interpolated[:max_accuracy].present? || !payload[accuracy_field] || payload[accuracy_field].to_i < interpolated[:max_accuracy].to_i)
83
-        if !payload[accuracy_field]
79
+        if interpolated[:max_accuracy].present? && !payload[accuracy_field].present?
84 80
           log "Accuracy field missing; all locations will be kept"
85 81
         end
86 82
         create_event payload: payload, location: location

+ 16 - 0
spec/models/agents/user_location_agent_spec.rb

@@ -74,4 +74,20 @@ describe Agents::UserLocationAgent do
74 74
     expect(@agent.events.last.lat).to eq(45)
75 75
     expect(@agent.events.last.lng).to eq(123)
76 76
   end
77
+
78
+  it 'allows a custom accuracy field' do
79
+    event = Event.new
80
+    event.agent = agents(:bob_weather_agent)
81
+    event.created_at = Time.now
82
+    @agent.options['accuracy_field'] = 'estimated_to'
83
+    event.payload = { 'longitude' => 123, 'latitude' => 45, 'estimated_to' => '20', 'something' => 'else' }
84
+
85
+    expect {
86
+      @agent.receive([event])
87
+    }.to change { @agent.events.count }.by(1)
88
+
89
+    expect(@agent.events.last.payload).to eq({ 'longitude' => 123, 'latitude' => 45, 'estimated_to' => '20', 'something' => 'else' })
90
+    expect(@agent.events.last.lat).to eq(45)
91
+    expect(@agent.events.last.lng).to eq(123)
92
+  end
77 93
 end