Basecamp: ensure url encoding of http parameters

The since paramter is now passed to HTTParty which takes care of encoding it properly

Dominik Sander преди 11 години
родител
ревизия
9c2773e8d4
променени са 2 файла, в които са добавени 16 реда и са изтрити 13 реда
  1. 7 9
      app/models/agents/basecamp_agent.rb
  2. 9 4
      spec/models/agents/basecamp_agent_spec.rb

+ 7 - 9
app/models/agents/basecamp_agent.rb

@@ -64,8 +64,7 @@ module Agents
64 64
     end
65 65
 
66 66
     def check
67
-      log "Requesting events from #{request_url}"
68
-      reponse = HTTParty.get request_url, auth_options.merge(:headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"})
67
+      reponse = HTTParty.get request_url, request_options.merge(query_parameters)
69 68
       memory[:last_run] = Time.now.utc.iso8601
70 69
       if last_check_at != nil
71 70
         JSON.parse(reponse.body).each do |event|
@@ -76,17 +75,16 @@ module Agents
76 75
     end
77 76
 
78 77
   private
79
-    def first_run?
80
-      memory[:first_run].nil?
78
+    def request_url
79
+      "https://basecamp.com/#{URI.encode(options[:user_id].to_s)}/api/v1/projects/#{URI.encode(options[:project_id].to_s)}/events.json"
81 80
     end
82 81
 
83
-    def request_url
84
-      since = memory[:last_run] ? "?since=#{memory[:last_run]}" : ''
85
-      "https://basecamp.com/#{URI.encode(options[:user_id].to_s)}/api/v1/projects/#{URI.encode(options[:project_id].to_s)}/events.json#{since}"
82
+    def request_options
83
+      {:basic_auth => {:username =>options[:username], :password=>options[:password]}, :headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"}}
86 84
     end
87 85
 
88
-    def auth_options
89
-      {:basic_auth => {:username =>options[:username], :password=>options[:password]}}
86
+    def query_parameters
87
+      memory[:last_run].present? ? { :query => {:since => memory[:last_run]} } : {}
90 88
     end
91 89
   end
92 90
 end

+ 9 - 4
spec/models/agents/basecamp_agent_spec.rb

@@ -44,19 +44,24 @@ describe Agents::BasecampAgent do
44 44
   end
45 45
 
46 46
   describe "helpers" do
47
-    it "should generate a correct auth hash" do
48
-      @checker.send(:auth_options).should == {:basic_auth=>{:username=>"user", :password=>"pass"}}
47
+    it "should generate a correct request options hash" do
48
+      @checker.send(:request_options).should == {:basic_auth=>{:username=>"user", :password=>"pass"}, :headers => {"User-Agent" => "Huginn (https://github.com/cantino/huginn)"}}
49 49
     end
50 50
 
51
-    it "should not provide the since attribute on first run" do
51
+    it "should generate the currect request url" do
52 52
       @checker.send(:request_url).should == "https://basecamp.com/12345/api/v1/projects/6789/events.json"
53 53
     end
54 54
 
55
+
56
+    it "should not provide the since attribute on first run" do
57
+      @checker.send(:query_parameters).should == {}
58
+    end
59
+
55 60
     it "should provide the since attribute after the first run" do
56 61
       time = (Time.now-1.minute).iso8601
57 62
       @checker.memory[:last_run] = time
58 63
       @checker.save
59
-      @checker.reload.send(:request_url).should == "https://basecamp.com/12345/api/v1/projects/6789/events.json?since=#{time}"
64
+      @checker.reload.send(:query_parameters).should == {:query => {:since => time}}
60 65
     end
61 66
   end
62 67
   describe "#check" do