buffer-alpaca 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/buffer/api/info.rb +28 -0
- data/lib/buffer/api/link.rb +30 -0
- data/lib/buffer/api/profile.rb +76 -0
- data/lib/buffer/api/schedule.rb +43 -0
- data/lib/buffer/api/update.rb +87 -0
- data/lib/buffer/api/user.rb +54 -0
- data/lib/buffer/client.rb +60 -0
- data/lib/buffer/error/client_error.rb +18 -0
- data/lib/buffer/error.rb +9 -0
- data/lib/buffer/http_client/auth_handler.rb +91 -0
- data/lib/buffer/http_client/error_handler.rb +51 -0
- data/lib/buffer/http_client/request_handler.rb +30 -0
- data/lib/buffer/http_client/response.rb +20 -0
- data/lib/buffer/http_client/response_handler.rb +24 -0
- data/lib/buffer/http_client.rb +129 -0
- data/lib/buffer-alpaca.rb +8 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 67a71d02204a85ab67796f39465df7628d02e7d6
|
4
|
+
data.tar.gz: c4e047efefc8c69f41de9c502e3a4de81521cd66
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d342b22c399737ea55a58d93e41d276f9a9a1b68939e21a10341ce0cf80651687ae2540d71a3ce4fbf272222a9958aa2e604bb5dfcdd08c97a24905a4ccd803
|
7
|
+
data.tar.gz: d3e5ac5808c6557e3f4cec31e215d7ee13fa2400fef87bdf75ac2548ee64dfaa902fd999c2ff30164c0a95be94577f13eb18e26e3771087b04ab3d177cf1808c
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module Api
|
4
|
+
|
5
|
+
# Returns api instance to get auxilary information about Buffer useful when creating your app.
|
6
|
+
#
|
7
|
+
class Info
|
8
|
+
|
9
|
+
def initialize(client)
|
10
|
+
@client = client
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns an object with the current configuration that Buffer is using, including supported services, their icons and the varying limits of character and schedules.
|
14
|
+
# '/info/configuration' GET
|
15
|
+
#
|
16
|
+
def show(options = {})
|
17
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
18
|
+
|
19
|
+
response = @client.get "/info/configuration", body, options
|
20
|
+
|
21
|
+
return response
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module Api
|
4
|
+
|
5
|
+
# Returns api instance to get information about links shared through Buffer.
|
6
|
+
#
|
7
|
+
class Link
|
8
|
+
|
9
|
+
def initialize(client)
|
10
|
+
@client = client
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns an object with a the numbers of shares a link has had using Buffer.
|
14
|
+
# '/link/shares' GET
|
15
|
+
#
|
16
|
+
# url - URL of the page for which the number of shares is requested.
|
17
|
+
def shares(url, options = {})
|
18
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
19
|
+
body[:url] = url
|
20
|
+
|
21
|
+
response = @client.get "/link/shares", body, options
|
22
|
+
|
23
|
+
return response
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module Api
|
4
|
+
|
5
|
+
# Returns a social media profile api instance.
|
6
|
+
#
|
7
|
+
# id - Identifier of a social media profile
|
8
|
+
class Profile
|
9
|
+
|
10
|
+
def initialize(id, client)
|
11
|
+
@id = id
|
12
|
+
@client = client
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns details of the single specified social media profile.
|
16
|
+
# '/profiles/:id' GET
|
17
|
+
#
|
18
|
+
def show(options = {})
|
19
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
20
|
+
|
21
|
+
response = @client.get "/profiles/#{@id}", body, options
|
22
|
+
|
23
|
+
return response
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns an array of updates that are currently in the buffer for an individual social media profile.
|
27
|
+
# '/profiles/:id/updates/pending' GET
|
28
|
+
#
|
29
|
+
def pending(options = {})
|
30
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
31
|
+
|
32
|
+
response = @client.get "/profiles/#{@id}/updates/pending", body, options
|
33
|
+
|
34
|
+
return response
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns an array of updates that have been sent from the buffer for an individual social media profile.
|
38
|
+
# '/profiles/:id/updates/sent' GET
|
39
|
+
#
|
40
|
+
def sent(options = {})
|
41
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
42
|
+
|
43
|
+
response = @client.get "/profiles/#{@id}/updates/sent", body, options
|
44
|
+
|
45
|
+
return response
|
46
|
+
end
|
47
|
+
|
48
|
+
# Edit the order at which statuses for the specified social media profile will be sent out of the buffer.
|
49
|
+
# '/profiles/:id/updates/reorder' POST
|
50
|
+
#
|
51
|
+
# order - An ordered array of status update id's. This can be a partial array in combination with the offset parameter or a full array of every update in the profiles Buffer.
|
52
|
+
def reorder(order, options = {})
|
53
|
+
body = options.has_key?(:body) ? options[:body] : {}
|
54
|
+
body[:order] = order
|
55
|
+
|
56
|
+
response = @client.post "/profiles/#{@id}/updates/reorder", body, options
|
57
|
+
|
58
|
+
return response
|
59
|
+
end
|
60
|
+
|
61
|
+
# Randomize the order at which statuses for the specified social media profile will be sent out of the buffer.
|
62
|
+
# '/profiles/:id/updates/shuffle' POST
|
63
|
+
#
|
64
|
+
def shuffle(options = {})
|
65
|
+
body = options.has_key?(:body) ? options[:body] : {}
|
66
|
+
|
67
|
+
response = @client.post "/profiles/#{@id}/updates/shuffle", body, options
|
68
|
+
|
69
|
+
return response
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module Api
|
4
|
+
|
5
|
+
# Returns scheduling api instance for social media profile.
|
6
|
+
#
|
7
|
+
# id - Identifier of a social media profile
|
8
|
+
class Schedule
|
9
|
+
|
10
|
+
def initialize(id, client)
|
11
|
+
@id = id
|
12
|
+
@client = client
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns details of the posting schedules associated with a social media profile.
|
16
|
+
# '/profiles/:id/schedules' GET
|
17
|
+
#
|
18
|
+
def list(options = {})
|
19
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
20
|
+
|
21
|
+
response = @client.get "/profiles/#{@id}/schedules", body, options
|
22
|
+
|
23
|
+
return response
|
24
|
+
end
|
25
|
+
|
26
|
+
# Set the posting schedules for the specified social media profile.
|
27
|
+
# '/profiles/:id/schedules/update' POST
|
28
|
+
#
|
29
|
+
# schedules - Each item in the array is an individual posting schedule which consists of days and times to match the format return by the above method.
|
30
|
+
def update(schedules, options = {})
|
31
|
+
body = options.has_key?(:body) ? options[:body] : {}
|
32
|
+
body[:schedules] = schedules
|
33
|
+
|
34
|
+
response = @client.post "/profiles/#{@id}/schedules/update", body, options
|
35
|
+
|
36
|
+
return response
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module Api
|
4
|
+
|
5
|
+
# Returns a social media update api instance.
|
6
|
+
#
|
7
|
+
# id - Identifier of a social media update
|
8
|
+
class Update
|
9
|
+
|
10
|
+
def initialize(id, client)
|
11
|
+
@id = id
|
12
|
+
@client = client
|
13
|
+
end
|
14
|
+
|
15
|
+
# Returns a single social media update.
|
16
|
+
# '/updates/:id' GET
|
17
|
+
#
|
18
|
+
def show(options = {})
|
19
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
20
|
+
|
21
|
+
response = @client.get "/updates/#{@id}", body, options
|
22
|
+
|
23
|
+
return response
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the detailed information on individual interactions with the social media update such as favorites, retweets and likes.
|
27
|
+
# '/updates/:id/interactions' GET
|
28
|
+
#
|
29
|
+
def interactions(options = {})
|
30
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
31
|
+
|
32
|
+
response = @client.get "/updates/#{@id}/interactions", body, options
|
33
|
+
|
34
|
+
return response
|
35
|
+
end
|
36
|
+
|
37
|
+
# Edit an existing, individual status update.
|
38
|
+
# '/updates/:id/update' POST
|
39
|
+
#
|
40
|
+
# text - The status update text.
|
41
|
+
def update(text, options = {})
|
42
|
+
body = options.has_key?(:body) ? options[:body] : {}
|
43
|
+
body[:text] = text
|
44
|
+
|
45
|
+
response = @client.post "/updates/#{@id}/update", body, options
|
46
|
+
|
47
|
+
return response
|
48
|
+
end
|
49
|
+
|
50
|
+
# Immediately shares a single pending update and recalculates times for updates remaining in the queue.
|
51
|
+
# '/updates/:id/share' POST
|
52
|
+
#
|
53
|
+
def share(options = {})
|
54
|
+
body = options.has_key?(:body) ? options[:body] : {}
|
55
|
+
|
56
|
+
response = @client.post "/updates/#{@id}/share", body, options
|
57
|
+
|
58
|
+
return response
|
59
|
+
end
|
60
|
+
|
61
|
+
# Permanently delete an existing status update.
|
62
|
+
# '/updates/:id/destroy' POST
|
63
|
+
#
|
64
|
+
def destroy(options = {})
|
65
|
+
body = options.has_key?(:body) ? options[:body] : {}
|
66
|
+
|
67
|
+
response = @client.post "/updates/#{@id}/destroy", body, options
|
68
|
+
|
69
|
+
return response
|
70
|
+
end
|
71
|
+
|
72
|
+
# Move an existing status update to the top of the queue and recalculate times for all updates in the queue. Returns the update with its new posting time.
|
73
|
+
# '/updates/:id/move_to_top' POST
|
74
|
+
#
|
75
|
+
def top(options = {})
|
76
|
+
body = options.has_key?(:body) ? options[:body] : {}
|
77
|
+
|
78
|
+
response = @client.post "/updates/#{@id}/move_to_top", body, options
|
79
|
+
|
80
|
+
return response
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module Api
|
4
|
+
|
5
|
+
# Returns authenticated user api instance.
|
6
|
+
#
|
7
|
+
class User
|
8
|
+
|
9
|
+
def initialize(client)
|
10
|
+
@client = client
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns information about the authenticated user.
|
14
|
+
# '/user' GET
|
15
|
+
#
|
16
|
+
def show(options = {})
|
17
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
18
|
+
|
19
|
+
response = @client.get "/user", body, options
|
20
|
+
|
21
|
+
return response
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns an array of social media profiles connected to the authenticated users account.
|
25
|
+
# '/profiles' GET
|
26
|
+
#
|
27
|
+
def profiles(options = {})
|
28
|
+
body = options.has_key?(:query) ? options[:query] : {}
|
29
|
+
|
30
|
+
response = @client.get "/profiles", body, options
|
31
|
+
|
32
|
+
return response
|
33
|
+
end
|
34
|
+
|
35
|
+
# Create one or more new status updates.
|
36
|
+
# '/updates/create' POST
|
37
|
+
#
|
38
|
+
# text - The status update text.
|
39
|
+
# profile_ids - An array of profile id's that the status update should be sent to. Invalid profile_id's will be silently ignored.
|
40
|
+
def create_update(text, profile_ids, options = {})
|
41
|
+
body = options.has_key?(:body) ? options[:body] : {}
|
42
|
+
body[:text] = text
|
43
|
+
body[:profile_ids] = profile_ids
|
44
|
+
|
45
|
+
response = @client.post "/updates/create", body, options
|
46
|
+
|
47
|
+
return response
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require "faraday"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
require "buffer/api/info"
|
5
|
+
require "buffer/api/user"
|
6
|
+
require "buffer/api/link"
|
7
|
+
require "buffer/api/profile"
|
8
|
+
require "buffer/api/schedule"
|
9
|
+
require "buffer/api/update"
|
10
|
+
|
11
|
+
module Buffer
|
12
|
+
|
13
|
+
class Client
|
14
|
+
|
15
|
+
def initialize(auth = {}, options = {})
|
16
|
+
@http_client = Buffer::HttpClient::HttpClient.new auth, options
|
17
|
+
end
|
18
|
+
|
19
|
+
# Returns api instance to get auxilary information about Buffer useful when creating your app.
|
20
|
+
#
|
21
|
+
def info()
|
22
|
+
Buffer::Api::Info.new @http_client
|
23
|
+
end
|
24
|
+
|
25
|
+
# Returns authenticated user api instance.
|
26
|
+
#
|
27
|
+
def user()
|
28
|
+
Buffer::Api::User.new @http_client
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns api instance to get information about links shared through Buffer.
|
32
|
+
#
|
33
|
+
def link()
|
34
|
+
Buffer::Api::Link.new @http_client
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns a social media profile api instance.
|
38
|
+
#
|
39
|
+
# id - Identifier of a social media profile
|
40
|
+
def profile(id)
|
41
|
+
Buffer::Api::Profile.new id, @http_client
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns scheduling api instance for social media profile.
|
45
|
+
#
|
46
|
+
# id - Identifier of a social media profile
|
47
|
+
def schedule(id)
|
48
|
+
Buffer::Api::Schedule.new id, @http_client
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns a social media update api instance.
|
52
|
+
#
|
53
|
+
# id - Identifier of a social media update
|
54
|
+
def update(id)
|
55
|
+
Buffer::Api::Update.new id, @http_client
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
data/lib/buffer/error.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require "base64"
|
2
|
+
|
3
|
+
module Buffer
|
4
|
+
|
5
|
+
module HttpClient
|
6
|
+
|
7
|
+
# AuthHandler takes care of devising the auth type and using it
|
8
|
+
class AuthHandler < Faraday::Middleware
|
9
|
+
|
10
|
+
URL_SECRET = 2
|
11
|
+
URL_TOKEN = 3
|
12
|
+
|
13
|
+
def initialize(app, auth = {}, options = {})
|
14
|
+
@auth = auth
|
15
|
+
super(app)
|
16
|
+
end
|
17
|
+
|
18
|
+
def call(env)
|
19
|
+
if !@auth.empty?
|
20
|
+
auth = get_auth_type
|
21
|
+
flag = false
|
22
|
+
|
23
|
+
if auth == URL_SECRET
|
24
|
+
env = url_secret env
|
25
|
+
flag = true
|
26
|
+
end
|
27
|
+
|
28
|
+
if auth == URL_TOKEN
|
29
|
+
env = url_token env
|
30
|
+
flag = true
|
31
|
+
end
|
32
|
+
|
33
|
+
if !flag
|
34
|
+
raise StandardError.new "Unable to calculate authorization method. Please check"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
@app.call(env)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Calculating the Authentication Type
|
42
|
+
def get_auth_type()
|
43
|
+
|
44
|
+
if @auth.has_key?(:client_id) and @auth.has_key?(:client_secret)
|
45
|
+
return URL_SECRET
|
46
|
+
end
|
47
|
+
|
48
|
+
if @auth.has_key?(:access_token)
|
49
|
+
return URL_TOKEN
|
50
|
+
end
|
51
|
+
|
52
|
+
return -1
|
53
|
+
end
|
54
|
+
|
55
|
+
# OAUTH2 Authorization with client secret
|
56
|
+
def url_secret(env)
|
57
|
+
query = {
|
58
|
+
:client_id => @auth[:client_id],
|
59
|
+
:client_secret => @auth[:client_secret]
|
60
|
+
}
|
61
|
+
|
62
|
+
merge_query env, query
|
63
|
+
end
|
64
|
+
|
65
|
+
# OAUTH2 Authorization with access token
|
66
|
+
def url_token(env)
|
67
|
+
query = { :access_token => @auth[:access_token] }
|
68
|
+
|
69
|
+
merge_query env, query
|
70
|
+
end
|
71
|
+
|
72
|
+
def query_params(url)
|
73
|
+
if url.query.nil? or url.query.empty?
|
74
|
+
{}
|
75
|
+
else
|
76
|
+
Faraday::Utils.parse_query url.query
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def merge_query(env, query)
|
81
|
+
query = query.update query_params(env[:url])
|
82
|
+
|
83
|
+
env[:url].query = Faraday::Utils.build_query query
|
84
|
+
|
85
|
+
return env
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module HttpClient
|
4
|
+
|
5
|
+
# ErrorHanlder takes care of selecting the error message from response body
|
6
|
+
class ErrorHandler < Faraday::Middleware
|
7
|
+
|
8
|
+
def initialize(app)
|
9
|
+
super(app)
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
@app.call(env).on_complete do |env|
|
14
|
+
code = env[:response].status
|
15
|
+
type = env[:response].headers["content-type"]
|
16
|
+
|
17
|
+
case code
|
18
|
+
when 500...599
|
19
|
+
raise Buffer::Error::ClientError.new "Error #{code}", code
|
20
|
+
when 400...499
|
21
|
+
body = Buffer::HttpClient::ResponseHandler.get_body env[:response]
|
22
|
+
message = ""
|
23
|
+
|
24
|
+
# If HTML, whole body is taken
|
25
|
+
if body.is_a? String
|
26
|
+
message = body
|
27
|
+
end
|
28
|
+
|
29
|
+
# If JSON, a particular field is taken and used
|
30
|
+
if type.include?("json") and body.is_a?(Hash)
|
31
|
+
if body.has_key? "error"
|
32
|
+
message = body["error"]
|
33
|
+
else
|
34
|
+
message = "Unable to select error message from json returned by request responsible for error"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
if message == ""
|
39
|
+
message = "Unable to understand the content type of response returned by request responsible for error"
|
40
|
+
end
|
41
|
+
|
42
|
+
raise Buffer::Error::ClientError.new message, code
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module HttpClient
|
4
|
+
|
5
|
+
# RequestHandler takes care of encoding the request body into format given by options
|
6
|
+
class RequestHandler
|
7
|
+
|
8
|
+
def self.set_body(options)
|
9
|
+
type = options.has_key?(:request_type) ? options[:request_type] : "form"
|
10
|
+
|
11
|
+
# Encoding body into form-urlencoded format
|
12
|
+
if type == "form"
|
13
|
+
options[:body] = Faraday::Utils::ParamsHash[options[:body]].to_query
|
14
|
+
options[:headers]["content-type"] = "application/x-www-form-urlencoded"
|
15
|
+
end
|
16
|
+
|
17
|
+
# Raw body
|
18
|
+
if type == "raw"
|
19
|
+
options[:body] = options[:body].is_a?(Hash) ? "" : options[:body]
|
20
|
+
options[:headers].delete "content-type"
|
21
|
+
end
|
22
|
+
|
23
|
+
return options
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module HttpClient
|
4
|
+
|
5
|
+
# Response object contains the response returned by the client
|
6
|
+
class Response
|
7
|
+
|
8
|
+
attr_accessor :body, :code, :headers
|
9
|
+
|
10
|
+
def initialize(body, code, headers)
|
11
|
+
@body = body
|
12
|
+
@code = code
|
13
|
+
@headers = headers
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Buffer
|
2
|
+
|
3
|
+
module HttpClient
|
4
|
+
|
5
|
+
# ResponseHandler takes care of decoding the response body into suitable type
|
6
|
+
class ResponseHandler
|
7
|
+
|
8
|
+
def self.get_body(response)
|
9
|
+
type = response.headers["content-type"]
|
10
|
+
body = response.body
|
11
|
+
|
12
|
+
# Response body is in JSON
|
13
|
+
if type.include? "json"
|
14
|
+
body = JSON.parse body
|
15
|
+
end
|
16
|
+
|
17
|
+
body
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require "buffer/http_client/auth_handler"
|
2
|
+
require "buffer/http_client/error_handler"
|
3
|
+
require "buffer/http_client/request_handler"
|
4
|
+
require "buffer/http_client/response"
|
5
|
+
require "buffer/http_client/response_handler"
|
6
|
+
|
7
|
+
module Buffer
|
8
|
+
|
9
|
+
module HttpClient
|
10
|
+
|
11
|
+
# Main HttpClient which is used by Api classes
|
12
|
+
class HttpClient
|
13
|
+
|
14
|
+
attr_accessor :options, :headers
|
15
|
+
|
16
|
+
@headers = {}
|
17
|
+
|
18
|
+
def initialize(auth = {}, options = {})
|
19
|
+
|
20
|
+
if auth.is_a? String
|
21
|
+
auth = { :access_token => auth }
|
22
|
+
end
|
23
|
+
|
24
|
+
@options = {
|
25
|
+
:base => "https://api.bufferapp.com",
|
26
|
+
:api_version => "1",
|
27
|
+
:user_agent => "alpaca/0.1.0 (https://github.com/pksunkara/alpaca)"
|
28
|
+
}
|
29
|
+
|
30
|
+
@options.update options
|
31
|
+
|
32
|
+
@headers = {
|
33
|
+
"user-agent" => @options[:user_agent]
|
34
|
+
}
|
35
|
+
|
36
|
+
if @options.has_key? :headers
|
37
|
+
@headers.update Hash[@options[:headers].map { |k, v| [k.downcase, v] }]
|
38
|
+
@options.delete :headers
|
39
|
+
end
|
40
|
+
|
41
|
+
@client = Faraday.new @options[:base] do |conn|
|
42
|
+
conn.use Buffer::HttpClient::AuthHandler, auth
|
43
|
+
conn.use Buffer::HttpClient::ErrorHandler
|
44
|
+
|
45
|
+
conn.adapter Faraday.default_adapter
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def get(path, params = {}, options = {})
|
50
|
+
request path, nil, "get", options.merge({ :query => params })
|
51
|
+
end
|
52
|
+
|
53
|
+
def post(path, body = {}, options = {})
|
54
|
+
request path, body, "post", options
|
55
|
+
end
|
56
|
+
|
57
|
+
def patch(path, body = {}, options = {})
|
58
|
+
request path, body, "patch", options
|
59
|
+
end
|
60
|
+
|
61
|
+
def delete(path, body = {}, options = {})
|
62
|
+
request path, body, "delete", options
|
63
|
+
end
|
64
|
+
|
65
|
+
def put(path, body = {}, options = {})
|
66
|
+
request path, body, "put", options
|
67
|
+
end
|
68
|
+
|
69
|
+
# Intermediate function which does three main things
|
70
|
+
#
|
71
|
+
# - Transforms the body of request into correct format
|
72
|
+
# - Creates the requests with give parameters
|
73
|
+
# - Returns response body after parsing it into correct format
|
74
|
+
def request(path, body, method, options)
|
75
|
+
options = @options.merge options
|
76
|
+
|
77
|
+
options[:headers] = options[:headers] || {}
|
78
|
+
options[:headers] = @headers.merge Hash[options[:headers].map { |k, v| [k.downcase, v] }]
|
79
|
+
|
80
|
+
options[:body] = body
|
81
|
+
|
82
|
+
if method != "get"
|
83
|
+
options[:body] = options[:body] || {}
|
84
|
+
options = set_body options
|
85
|
+
end
|
86
|
+
|
87
|
+
response = create_request method, path, options
|
88
|
+
|
89
|
+
body = get_body response
|
90
|
+
|
91
|
+
Buffer::HttpClient::Response.new body, response.status, response.headers
|
92
|
+
end
|
93
|
+
|
94
|
+
# Creating a request with the given arguments
|
95
|
+
#
|
96
|
+
# If api_version is set, appends it immediately after host
|
97
|
+
def create_request(method, path, options)
|
98
|
+
version = options.has_key?(:api_version) ? "/#{options[:api_version]}" : ""
|
99
|
+
|
100
|
+
# Adds a suffix (ex: ".html", ".json") to url
|
101
|
+
suffix = options.has_key?(:response_type) ? options[:response_type] : "json"
|
102
|
+
path = "#{path}.#{suffix}"
|
103
|
+
|
104
|
+
path = "#{version}#{path}"
|
105
|
+
|
106
|
+
instance_eval <<-RUBY, __FILE__, __LINE__ + 1
|
107
|
+
@client.#{method} path do |req|
|
108
|
+
req.body = options[:body]
|
109
|
+
req.headers.update(options[:headers])
|
110
|
+
req.params.update(options[:query]) if options[:query]
|
111
|
+
end
|
112
|
+
RUBY
|
113
|
+
end
|
114
|
+
|
115
|
+
# Get response body in correct format
|
116
|
+
def get_body(response)
|
117
|
+
Buffer::HttpClient::ResponseHandler.get_body response
|
118
|
+
end
|
119
|
+
|
120
|
+
# Set request body in correct format
|
121
|
+
def set_body(options)
|
122
|
+
Buffer::HttpClient::RequestHandler.set_body options
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: buffer-alpaca
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pavan Kumar Sunkara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.8.8
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.8.8
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.7.7
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.7.7
|
41
|
+
description: Buffer API library client for ruby
|
42
|
+
email: pavan.sss1991@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- lib/buffer-alpaca.rb
|
48
|
+
- lib/buffer/api/info.rb
|
49
|
+
- lib/buffer/api/link.rb
|
50
|
+
- lib/buffer/api/profile.rb
|
51
|
+
- lib/buffer/api/schedule.rb
|
52
|
+
- lib/buffer/api/update.rb
|
53
|
+
- lib/buffer/api/user.rb
|
54
|
+
- lib/buffer/client.rb
|
55
|
+
- lib/buffer/error.rb
|
56
|
+
- lib/buffer/error/client_error.rb
|
57
|
+
- lib/buffer/http_client.rb
|
58
|
+
- lib/buffer/http_client/auth_handler.rb
|
59
|
+
- lib/buffer/http_client/error_handler.rb
|
60
|
+
- lib/buffer/http_client/request_handler.rb
|
61
|
+
- lib/buffer/http_client/response.rb
|
62
|
+
- lib/buffer/http_client/response_handler.rb
|
63
|
+
homepage: https://bufferapp.com
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 2.2.0.rc.1
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: Buffer API library client for ruby
|
87
|
+
test_files: []
|