Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit bc55d85

Browse files
committed
Refactored some repetition out of lib/spec
1 parent 93ba61b commit bc55d85

6 files changed

Lines changed: 47 additions & 33 deletions

File tree

lib/buff/core.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def get(path, options={})
1515
interpret_response(response)
1616
end
1717

18+
#TODO post data isn't limited to body requests in post
19+
#split out the options data separately
1820
def post(path, post_data)
1921
@conn.post do |req|
2022
req.url path.gsub(%r{^\/}, '')

lib/buff/profile.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def profile_schedules_by_id(id, options={})
2020
# currently deletes schedule due to malformed request
2121
def set_schedules(id, options={})
2222
# schedules = options.fetch(:schedules) { raise ArgumentError }
23+
raise ArgumentError unless ENV['BUFFER_DEBUG']
2324
response = post("/profiles/#{id}/schedules/update.json", options )
2425
Buff::Response.new(JSON.parse(response.body))
2526
end

lib/buff/update.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def interactions_by_update_id(id, options={})
3030

3131
def reorder_updates(profile_id, options={})
3232
# order, optional: offset, utc
33-
order = options.fetch(:order) { raise ArgumentError }
33+
options.fetch(:order) { raise ArgumentError }
3434
response = post("/profiles/#{profile_id}/updates/reorder.json", options)
3535
end
3636

@@ -41,13 +41,6 @@ def shuffle_updates(profile_id, options={})
4141

4242
#TODO
4343
def create_update(options={})
44-
45-
# POST Data
46-
# text=This%20is%20an%20example%20update&
47-
# profile_ids[]=4eb854340acb04e870000010&
48-
# profile_ids[]=4eb9276e0acb04bb81000067&
49-
# media[link]=http%3A%2F%2Fgoogle.com&
50-
# media[description]=The%20google%20homepage
5144
# options = {
5245
# text: "bodytext",
5346
# profile_ids: ["230958239058", "23059u2350923"],

spec/lib/buff/link_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
let(:url) { %q{http://bufferapp.com} }
77

88
before do
9-
stub_request(:get, "https://api.bufferapp.com/1/links/shares.json?access_token=some_token&url=http://bufferapp.com").
10-
to_return(fixture('link.txt'))
9+
stub_request(:get, "#{ base_path }/links/shares.json?#{ access_token_param }&url=http://bufferapp.com").
10+
to_return(fixture('link.txt'))
1111
end
1212

1313
it "connects to the correct endpoint" do

spec/lib/buff_spec.rb

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
let(:rash) { subject.user_info }
1919

2020
before(:each) do
21-
stub_request(:get, 'https://api.bufferapp.com/1/user.json').
22-
with(query: { 'access_token' => 'some_token'}).
23-
to_return(fixture('user_authenticated.txt'))
21+
url = "#{ base_path }/user.json"
22+
stub_with_to_return(:get, url, "user_authenticated.txt")
2423
end
2524

2625
it "returns a Rash object" do
@@ -36,9 +35,8 @@
3635
let(:rash) { Buff::Client.new("some_token").profiles }
3736

3837
before(:each) do
39-
stub_request(:get, 'https://api.bufferapp.com/1/profiles.json').
40-
with(query: { 'access_token' => 'some_token'}).
41-
to_return(fixture('profile_authenticated.txt'))
38+
url = "#{ base_path }/profiles.json"
39+
stub_with_to_return(:get, url, 'profile_authenticated.txt')
4240
end
4341

4442
it "makes the correct url request" do
@@ -57,9 +55,9 @@
5755
describe "#profile_by_id" do
5856
let(:id) { "5160746d54f04a5e3a00000f" }
5957
before(:each) do
60-
stub_request(:get, "https://api.bufferapp.com/1/profiles/#{id}.json").
61-
with(query: { 'access_token' => 'some_token'}).
62-
to_return(fixture('profiles_by_id.txt'))
58+
url = "#{base_path}/profiles/#{id}.json"
59+
fixture_name = "profiles_by_id.txt"
60+
stub_with_to_return(:get, url, fixture_name)
6361
end
6462

6563
let(:rash) { Buff::Client.new("some_token").profile_by_id(id) }
@@ -75,9 +73,9 @@
7573

7674
describe "#profile_schedules_by_id" do
7775
before(:each) do
78-
stub_request(:get, "https://api.bufferapp.com/1/profiles/#{id}/schedules.json").
79-
with(query: { 'access_token' => 'some_token'}).
80-
to_return(fixture('profile_schedules_by_id.txt'))
76+
url = "#{base_path}/profiles/#{id}/schedules.json"
77+
fixture_name = 'profile_schedules_by_id.txt'
78+
stub_with_to_return(:get, url, fixture_name)
8179
end
8280

8381
let(:rash) { Buff::Client.new("some_token").profile_schedules_by_id(id) }
@@ -107,7 +105,7 @@
107105

108106
describe "#info" do
109107
before do
110-
stub_request(:get, "https://api.bufferapp.com/1/info/configuration.json?access_token=some_token").
108+
stub_request(:get, "#{base_path}/info/configuration.json?access_token=some_token").
111109
to_return(fixture("info.txt"))
112110
end
113111

@@ -182,8 +180,7 @@
182180

183181
it "dumping a double schedule yields correct json" do
184182
schedules = Buff::Schedules.new
185-
schedules << @schedule
186-
schedules << @schedule
183+
schedules << @schedule << @schedule
187184
@sample_schedules = @sample_schedules.to_json
188185

189186
schedules.dump.should eq(@sample_schedules)
@@ -195,15 +192,15 @@
195192
let(:client) { Buff::Client.new("some_token") }
196193
describe "#get" do
197194
it "delegates to #handle_response_code when code != 200" do
198-
stub_request(:get, "https://api.bufferapp.com/1/info/configuration.json?access_token=some_token").
195+
stub_request(:get, "#{base_path}/info/configuration.json?access_token=some_token").
199196
to_return(:status => 403, :body => "", :headers => {})
200197
client.should_receive(:handle_response_code).once
201198
client.info
202199
end
203200

204201

205202
it "does not delegate to #handle_response_code when code = 200" do
206-
stub_request(:get, "https://api.bufferapp.com/1/info/configuration.json?access_token=some_token").
203+
stub_request(:get, "#{base_path}/info/configuration.json?access_token=some_token").
207204
to_return(fixture("link.txt"))
208205
client.should_not_receive(:handle_response_code)
209206
client.info
@@ -214,7 +211,7 @@
214211

215212
it "connects to the correct endpoint" do
216213

217-
#TODO improve test
214+
#TODO improve test
218215
response = %Q[{"success": true, "message": "Schedule saved successfully"}]
219216
id = "4eb854340acb04e870000010"
220217
stub_request(:post, %r{https://api\.bufferapp\.com/1/profiles/4eb854340acb04e870000010/schedules/update\.json\?access_token=.*}).
@@ -226,8 +223,9 @@
226223
end
227224

228225
xit "does not delegate to #handle_response_code when code = 200" do
229-
stub_request(:get, "https://api.bufferapp.com/1/info/configuration.json?access_token=some_token").
230-
to_return(fixture("link.txt"))
226+
url = "#{base_path}/info/configuration.json"
227+
fixture_name = "link.txt"
228+
stub_with_to_return(:get, url, fixture_name)
231229
client.should_not_receive(:handle_response_code)
232230
client.info
233231
end
@@ -243,9 +241,8 @@
243241
context "fails gracefully with undocumented responses" do
244242
it "responds to 401 unauthorized response" do
245243
id = "4eb8565e0acb04bb82000004"
246-
stub_request(:get, "https://api.bufferapp.com/1/updates/#{id}.json?access_token=some_token").
247-
to_return(fixture("update_by_id_non_auth.txt"))
248-
244+
url = "#{base_path}/updates/#{id}.json?access_token=some_token"
245+
stub_with_to_return(:get, url, "update_by_id_non_auth.txt")
249246
lambda { client.update_by_id(id) }.
250247
should raise_error(Buff::APIError)
251248
end

spec/spec_helper.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,28 @@ def sample_schedules2
2626
{days: %w[mon tue wed],
2727
times: %w[12:00 17:00 18:00]},
2828
]
29+
30+
end
31+
32+
def base_path
33+
"https://api.bufferapp.com/1"
34+
end
35+
36+
def access_token_param
37+
"access_token=some_token"
2938
end
39+
40+
def stub_with_to_return(request_type, url, fixture_name, query_hash={})
41+
query = access_hash.merge(query_hash)
42+
stub_request(request_type, url).
43+
with(query: query).
44+
to_return(fixture(fixture_name))
45+
end
46+
47+
def access_hash
48+
{ 'access_token' => 'some_token'}
49+
end
50+
3051
def sample_schedules
3152
[
3253
[{ days: %w[mon tue wed],

0 commit comments

Comments
 (0)