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

Commit ee27258

Browse files
committed
Added Link Route & Error Codes
1 parent e368936 commit ee27258

6 files changed

Lines changed: 115 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ Or install it yourself as:
3333
* User
3434
* Profiles (:get)
3535
* Updates (:get)
36+
* Links
37+
* Info
38+
* Error Codes
3639

3740
#### Not Implemented
3841

3942
* Authentication (Try OAuth-buffer)
4043
* Profiles (:post)
4144
* Updates (:post & optional params)
42-
* Links
43-
* Info
44-
* Error Codes
4545
* Optional params for get requests
4646

4747
## Contributing

lib/buff.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
require "httparty"
22
require "json"
33
require "rash"
4+
require "CSV"
45

56
require "buff/version"
67
require "buff/core"
78
require "buff/user"
89
require "buff/profile"
910
require "buff/update"
11+
require "buff/link"
12+
require "buff/error"
1013

1114
module Buff
1215

@@ -17,6 +20,8 @@ class Update < Hashie::Mash; end
1720
class Updates < Hashie::Mash; end
1821
class Interaction < Hashie::Mash; end
1922
class Interactions < Hashie::Mash; end
23+
class Link < Hashie::Mash; end
24+
class Info < Hashie::Mash; end
2025

2126
class Schedule < Hashie::Mash; end
2227
Schedules = Class.new(Array) do
@@ -28,13 +33,17 @@ def dump
2833
InvalidIdLength = Class.new(ArgumentError)
2934
InvalidIdContent = Class.new(ArgumentError)
3035
MissingStatus = Class.new(ArgumentError)
36+
APIError = Class.new(StandardError)
37+
UnauthorizeRequest = Class.new(StandardError)
3138

3239
class Client
3340
include HTTParty
3441
include Core
3542
include User
3643
include Profile
3744
include Update
45+
include Link
46+
include Error
3847

3948
attr_accessor :access_token, :auth_query
4049

@@ -48,6 +57,11 @@ def initialize(access_token)
4857
}
4958
}
5059
end
60+
61+
def info
62+
response = get("/info/configuration.json")
63+
Buff::Info.new(response)
64+
end
5165
end
5266
end
5367

lib/buff/core.rb

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,70 @@ class Client
33
module Core
44
API_VERSION = "1"
55

6+
attr_reader :error_table
7+
68
def get(path, options=auth_query)
7-
self.class.get(path, options)
9+
response = self.class.get(path, options)
10+
case response.code
11+
when 200
12+
response
13+
# when 401
14+
# raise Buff::UnauthorizeRequest
15+
else
16+
handle_response_code(response)
17+
end
18+
end
19+
20+
def handle_response_code(response)
21+
error = Hashie::Mash.new( response.body )
22+
if ERROR_TABLE[error.code]
23+
error_explanation = "Buffer API Error Code: #{error.code}\n"
24+
error_explanation += "Description: #{error.error}"
25+
else
26+
error_explanation = "Buffer API Unknown Error in Response"
27+
end
28+
29+
raise Buff::APIError, error_explanation
830
end
31+
32+
def self.error_table
33+
@error_table ||= ERROR_TABLE
34+
end
35+
36+
ERROR_TABLE = {
37+
"403"=>"Permission denied.",
38+
"404"=>"Endpoint not found.",
39+
"405"=>"Method not allowed.",
40+
"1000"=>"An unknown error occurred.",
41+
"1001"=>"Access token required.",
42+
"1002"=>"Not within application scope.",
43+
"1003"=>"Parameter not recognized.",
44+
"1004"=>"Required parameter missing.",
45+
"1005"=>"Unsupported response format.",
46+
"1006"=>"Parameter value not within bounds.",
47+
"1010"=>"Profile could not be found.",
48+
"1011"=>"No authorization to access profile.",
49+
"1012"=>"Profile did not save successfully.",
50+
"1013"=>"Profile schedule limit reached.",
51+
"1014"=>"Profile limit for user has been reached.",
52+
"1015"=>"Profile could not be destroyed.",
53+
"1020"=>"Update could not be found.",
54+
"1021"=>"No authorization to access update.",
55+
"1022"=>"Update did not save successfully.",
56+
"1023"=>"Update limit for profile has been reached.",
57+
"1024"=>"Update limit for team profile has been reached.",
58+
"1025"=>"Update was recently posted, can't post duplicate content.",
59+
"1026"=>"Update must be in error status to requeue.",
60+
"1028"=>"Update soft limit for profile reached.",
61+
"1029"=>"Event type not supported.",
62+
"1030"=>"Media filetype not supported.",
63+
"1031"=>"Media filesize out of acceptable range.",
64+
"1032"=>"Unable to post image to LinkedIn group(s).",
65+
"1042"=>"User did not save successfully.",
66+
"1050"=>"Client could not be found.",
67+
"1051"=>"No authorization to access client."
68+
}
69+
970
end
1071
end
1172
end

lib/buff/error.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Buff
2+
module Error
3+
end
4+
end

lib/buff/link.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module Buff
2+
class Client
3+
module Link
4+
def link(url)
5+
response = get("/links/shares.json?url=#{url}")
6+
Buff::Link.new(response)
7+
end
8+
end
9+
end
10+
end

spec/lib/buff/link_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require 'spec_helper'
2+
3+
describe Buff::Client do
4+
describe "#link" do
5+
let(:client) { Buff::Client.new("some_token") }
6+
let(:url) { %q{http://bufferapp.com} }
7+
8+
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'))
11+
end
12+
13+
it "connects to the correct endpoint" do
14+
client.link(url)
15+
end
16+
17+
it "parses the shares of a link" do
18+
client.link(url).shares.should eq(47348)
19+
end
20+
21+
end
22+
end

0 commit comments

Comments
 (0)