Skip to content

Commit d3a9d4f

Browse files
committed
Added API Coverage and added missing test coverage
1 parent 99a57c0 commit d3a9d4f

8 files changed

Lines changed: 227 additions & 26 deletions

File tree

API_COVERAGE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Fully Implemented API Requests
2+
GET https://api.bufferapp.com/1/user.json
3+
GET https://api.bufferapp.com/1/profiles.json
4+
GET https://api.bufferapp.com/1/profiles/4eb854340acb04e870000010.json
5+
GET https://api.bufferapp.com/1/profiles/4eb854340acb04e870000010/schedules.json
6+
GET https://api.bufferapp.com/1/updates/4eb8565e0acb04bb82000004.json
7+
POST https://api.bufferapp.com/1/updates/4ecda256512f7ee521000001/share.json
8+
POST https://api.bufferapp.com/1/updates/4ecda256512f7ee521000004/destroy.json
9+
POST https://api.bufferapp.com/1/profiles/4eb854340acb04e870000010/schedules/update.json
10+
GET https://api.bufferapp.com/1/profiles/4eb854340acb04e870000010/updates/pending.json
11+
GET https://api.bufferapp.com/1/profiles/4eb854340acb04e870000010/updates/sent.json
12+
GET https://api.bufferapp.com/1/updates/4ecda476542f7ee521000006/interactions.json
13+
POST https://api.bufferapp.com/1/profiles/4eb854340acb04e870000010/updates/shuffle.json
14+
POST https://api.bufferapp.com/1/updates/create.json
15+
POST https://api.bufferapp.com/1/updates/4ecda256512f7ee521000004/update.json
16+
POST https://api.bufferapp.com/1/profiles/4eb854340acb04e870000010/updates/reorder.json
17+
18+
## Untested Optional Params
19+

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ Since the gem is currently in ALPHA development, the interface is prone to chang
99

1010
[![Coverage Status](https://coveralls.io/repos/zph/buff/badge.png?branch=master)](https://coveralls.io/r/zph/buff?branch=master) [![Build Status](https://travis-ci.org/zph/buff.png?branch=master)](https://travis-ci.org/zph/buff) [![Code Climate](https://codeclimate.com/github/zph/buff.png)](https://codeclimate.com/github/zph/buff)
1111

12-
Once Buff is released as a gem, the following instructions will work. For now please `git clone` the repo.
12+
For now please `git clone [email protected]:zph/buff.git` the repo
13+
14+
Or
1315

1416
Add this line to your application's Gemfile to include HEAD code:
1517

16-
`gem 'buff', :github => 'zph/buff'`
18+
`gem 'buff', :github => 'zph/buff'`
1719

1820
And then execute:
1921

20-
`$ bundle`
22+
`$ bundle`
2123

2224
Or install RubyGems version, which will receive more attention to stability:
2325

24-
`$ gem install buff`
26+
`$ gem install buff`
2527

2628
## Usage
2729

@@ -53,10 +55,11 @@ Or install RubyGems version, which will receive more attention to stability:
5355
* Info
5456
* Error Codes
5557

58+
Further Details [API Coverage](API_COVERAGE.md)
59+
5660
#### Not Implemented
5761

5862
* Caching
59-
* Various optional params
6063

6164
## Supported Ruby Implementations
6265
- MRI 2.0.0

lib/buff/core.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ def get(path, options = {})
2727

2828
def post(path, options = {})
2929
params = merge_auth_token_and_query(options)
30-
@connection.post do |req|
30+
response = @connection.post do |req|
3131
req.url path.remove_leading_slash
3232
req.headers['Content-Type'] = "application/x-www-form-urlencoded"
3333
req.body = options[:body]
3434
req.params = params
3535
end
36+
37+
Hashie::Mash.new(JSON.parse response.body)
3638
end
3739

3840
def merge_auth_token_and_query(options)

lib/buff/profile.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@ def profile_by_id(id)
1111
Buff::Profile.new(response)
1212
end
1313

14-
def profile_schedules_by_id(id)
14+
def schedules_by_profile_id(id)
1515
response = get("/profiles/#{id}/schedules.json")
1616
response.map { |a_response| Buff::Schedule.new(a_response) }
1717
end
1818

1919
def set_schedules(id, options)
2020
schedules = Buff::Encode.encode(
2121
options.fetch(:schedules) { raise ArgumentError })
22-
response = post("/profiles/#{id}/schedules/update.json",
22+
post("/profiles/#{id}/schedules/update.json",
2323
body: { schedules: schedules })
24-
Buff::Response.new(JSON.parse(response.body))
2524
end
2625
end
2726
end

lib/buff/update.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,28 @@ def shuffle_updates(profile_id, options = {})
4242
end
4343

4444
def create_update(options = {})
45-
response = post("/updates/create.json", options)
46-
Hashie::Mash.new(JSON.parse response.body)
45+
options[:body].fetch(:text) do
46+
raise ArgumentError, "Must include text for update"
47+
end
48+
options[:body].fetch(:profile_ids) do
49+
raise ArgumentError, "Must include array of profile_ids"
50+
end
51+
post("/updates/create.json", options)
4752
end
4853

4954
def modify_update_text(update_id, options = {})
50-
# text, (now, media, utc)
51-
options.fetch(:text) { raise ArgumentError }
52-
response = post("/updates/#{update_id}/update.json", options)
53-
Hashie::Mash.new(JSON.parse response.body)
55+
options[:body].fetch(:text) do
56+
raise ArgumentError, "Must include updated text"
57+
end
58+
post("/updates/#{update_id}/update.json", options)
5459
end
5560

56-
def share_update(update_id, options = {})
57-
post("/updates/#{update_id}/share.json", options)
61+
def share_update(update_id)
62+
post("/updates/#{update_id}/share.json")
5863
end
5964

60-
def destroy_update(update_id, options = {})
61-
post("/updates/#{update_id}/destroy.json", options)
65+
def destroy_update(update_id)
66+
post("/updates/#{update_id}/destroy.json")
6267
end
6368

6469
def check_id(id)

spec/lib/buff/profile_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@
4747
end
4848
end
4949

50-
describe "#profile_schedules_by_id" do
50+
describe "#schedules_by_profile_id" do
5151
before(:each) do
5252
url = "#{base_path}/profiles/#{id}/schedules.json"
5353
fixture_name = 'profile_schedules_by_id.txt'
5454
stub_with_to_return(:get, url, fixture_name)
5555
end
5656

57-
let(:rash) { Buff::Client.new("some_token").profile_schedules_by_id(id) }
57+
let(:rash) { Buff::Client.new("some_token").schedules_by_profile_id(id) }
5858

5959
it "returns a rash collection" do
6060
rash[0].class.should eq(Buff::Schedule)

spec/lib/buff/update_spec.rb

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,45 @@
7474
end
7575

7676
it "allows optional params" do
77-
client.interactions_by_update_id(id, page: 2)
77+
response =<<EOF
78+
{
79+
"total":2,
80+
"interactions":[
81+
{
82+
"_id":"50f98310c5ac415d7f2e74fd",
83+
"created_at":1358509258,
84+
"event":"favorite",
85+
"id":"50f98310c5ac415d7f2e74fd",
86+
"interaction_id":"292235127847788544",
87+
"user":{
88+
"username":"Crispy Potatoes",
89+
"followers":160,
90+
"avatar":"http:\/\/si0.twimg.com\/profile_images\/...",
91+
"avatar_https":"https:\/\/si0.twimg.com\/profile_images\/...",
92+
"twitter_id":"70712344376"
93+
}
94+
},
95+
{
96+
"_id":"50f8623ac5ac415d7f1d4f77",
97+
"created_at":1358454592,
98+
"event":"retweet",
99+
"id":"50f8623ac5ac415d7f1d4f77",
100+
"interaction_id":"292005842654461953",
101+
"user":{
102+
"username":"Lucky Number 8",
103+
"followers":36079,
104+
"avatar":"http:\/\/si0.twimg.com\/profile_images\/2901468678\/...",
105+
"avatar_https":"https:\/\/si0.twimg.com\/profile_images\/2901468678\/...",
106+
"twitter_id":"1423444249"
107+
}
108+
}
109+
]
110+
}
111+
EOF
112+
stub_request(:get, "https://api.bufferapp.com/1/updates/4ecda476542f7ee521000006/interactions.json?access_token=some_token&count=3&event=favorite&page=2").
113+
with(:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'User-Agent'=>'Faraday v0.8.7'}).
114+
to_return(:status => 200, :body => response, :headers => {})
115+
client.interactions_by_update_id(id, page: 2, count: 3, event: "favorite")
78116
end
79117
end
80118

@@ -103,29 +141,82 @@
103141
stub_request(:post, %r{https://api\.bufferapp\.com/1/profiles/4ecda256512f7ee521000001/updates/reorder\.json\?access_token=.*}).
104142
with(:body => {"order"=>["4ecda256512f7ee521000001", "4ecda256512f7ee521000001", "4ecda256512f7ee521000001"]},
105143
:headers => {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>'Faraday v0.8.7'}).
106-
to_return(:status => 200, :body => "", :headers => {})
144+
to_return(:status => 200, :body => reorder_updates_body_response, :headers => {})
107145
client.reorder_updates(id_no, order_hash)
108-
109146
end
110147
end
111148

112149
describe "#shuffle_updates" do
113150
it "connects to appropriate endpoint" do
114151
id_no = "4ecda256512f7ee521000001"
115152
stub_request(:post, %r{https://api\.bufferapp\.com/1/profiles/4ecda256512f7ee521000001/updates/shuffle\.json\?access_token=.*}).
116-
with(:body => {"count"=>"10"})
153+
with(:body => {"count"=>"10"}).
154+
to_return(:status => 200, :body => '{"success": true,
155+
"updates": [],
156+
"time_to_shuffle":0.0041220188140869}')
117157
client.shuffle_updates(id_no, count: 10)
118158
end
119159
end
160+
120161
describe "#share_update" do
121162
it "should connect to correct endpoint" do
122163
stub_request(:post, %r{https://api\.bufferapp\.com/1/updates/4ecda256512f7ee521000001/share\.json\?access_token=.*}).
123-
to_return(:status => 200, :body => "{'success': true}", :headers => {})
164+
to_return(:status => 200, :body => '{"success": true}', :headers => {})
124165
update_id = "4ecda256512f7ee521000001"
125166
client.share_update(update_id)
126167
end
127168
end
128169

170+
describe "#create_update" do
171+
172+
let(:body_content) do {text: "Text for an update",
173+
profile_ids: [
174+
"4eb854340acb04e870000010",
175+
"4eb9276e0acb04bb81000067"
176+
]}
177+
end
178+
179+
let(:url) { %r{https://api\.bufferapp\.com/1/updates/create\.json\?access_token=.*} }
180+
181+
context "should create an update" do
182+
it "when only required params are present" do
183+
stub_request(:post, url).
184+
with(:body => body_content).
185+
to_return(:status => 200, :body => create_update_return_body, :headers => {})
186+
client.create_update(body: body_content)
187+
end
188+
it "when optional params are included" do
189+
body_content[:media] = {}
190+
body_content[:media][:link] = "http://google.com"
191+
body_content[:media][:description] = "Google Homepage"
192+
stub_request(:post, url).
193+
with(:body => body_content).
194+
to_return(:status => 200, :body => create_update_return_body, :headers => {})
195+
client.create_update(body: body_content)
196+
197+
end
198+
end
199+
end
200+
201+
describe "#modify_update_text" do
202+
203+
let(:body_content) { {text: "Text for an updated text for update"} }
204+
205+
id = "4ecda256512f7ee521000004"
206+
let(:url) { %r{https://api\.bufferapp\.com/1/updates/#{ id }/update\.json\?access_token=.*} }
207+
208+
context "should modify an update" do
209+
it "when params are present" do
210+
stub_request(:post, url).
211+
with(:body => body_content).
212+
to_return(:status => 200, :body => modify_update_response, :headers => {})
213+
Pry.rescue do
214+
client.modify_update_text(id, body: body_content)
215+
end
216+
end
217+
end
218+
end
219+
129220
describe "#destroy_update" do
130221
it "connects to correct endpoint" do
131222
stub_request(:post, %r{https://api\.bufferapp\.com/1/updates/4ecda256512f7ee521000001/destroy\.json\?access_token=.*}).

spec/spec_helper.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,88 @@ def post_data
2020
EOF
2121
end
2222

23+
def modify_update_response
24+
response =<<EOF
25+
{
26+
"success" : true,
27+
"buffer_count" : 10,
28+
"buffer_percentage" : 20,
29+
"update" : {
30+
"id" : "4ecda256512f7ee521000004",
31+
"client_id" : "4f850cc93733aa9301000002",
32+
"created_at" : 1320703582,
33+
"day" : "Saturday 26th November",
34+
"due_at" : 1320742680,
35+
"due_time" : "11:05 am",
36+
"media" : {
37+
"link" : "http://google.com",
38+
"title" : "Google",
39+
"description" : "The google homepage"
40+
},
41+
"profile_id" : "4eb854340acb04e870000010",
42+
"profile_service" : "twitter",
43+
"status" : "buffer",
44+
"text" : "This is an edited update",
45+
"text_formatted" : "This is an edited update",
46+
"user_id" : "4eb9276e0acb04bb81000067",
47+
"via" : "api"
48+
}
49+
}
50+
EOF
51+
end
52+
53+
def create_update_return_body
54+
create_update_return_body =<<EOF
55+
{
56+
"success" : true,
57+
"buffer_count" : 10,
58+
"buffer_percentage" : 20,
59+
"updates" : [{
60+
"id" : "4ecda256512f7ee521000004",
61+
"created_at" : 1320703582,
62+
"day" : "Saturday 26th November",
63+
"due_at" : 1320742680,
64+
"due_time" : "11:05 am",
65+
"media" : {
66+
"link" : "http://google.com",
67+
"title" : "Google",
68+
"description" : "The google homepage"
69+
},
70+
"profile_id" : "4eb854340acb04e870000010",
71+
"profile_service" : "twitter",
72+
"status" : "buffer",
73+
"text" : "This is an example update",
74+
"text_formatted" : "This is an example update",
75+
"user_id" : "4eb9276e0acb04bb81000067",
76+
"via" : "api"
77+
}
78+
]
79+
}
80+
EOF
81+
end
82+
def reorder_updates_body_response
83+
return_body =<<EOF
84+
{
85+
"success" : true,
86+
"updates" : [{
87+
"id" : "4eb854340acb04e870000010",
88+
"created_at" : 1320703582,
89+
"day" : "Saturday 5th November",
90+
"due_at" : 1320742680,
91+
"due_time" : "08:01 am",
92+
"profile_id" : "4eb854340acb04e870000010",
93+
"profile_service" : "twitter",
94+
"status" : "buffer",
95+
"text" : "3 Incredible Stories Made Possible Through Twitter j.mp/u...",
96+
"text_formatted" : "3 Incredible Stories Made Possible Through Twit...",
97+
"user_id" : "4eb9276e0acb04bb81000067",
98+
"via" : "safari"
99+
}
100+
]
101+
}
102+
EOF
103+
end
104+
23105
def sample_schedules2
24106
[{ days: %w[mon tue wed],
25107
times: %w[12:00 17:00 18:00]},

0 commit comments

Comments
 (0)