buffer 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.bufferapprc.template +23 -0
  3. data/.gitignore +6 -0
  4. data/.rubocop.yml +18 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +5 -0
  7. data/API_COVERAGE.md +19 -0
  8. data/Gemfile +1 -1
  9. data/Guardfile +10 -0
  10. data/{LICENSE → LICENSE.txt} +1 -1
  11. data/README.md +62 -80
  12. data/Rakefile +25 -1
  13. data/TODO.md +3 -0
  14. data/bin/buffer +35 -0
  15. data/buffer.gemspec +26 -16
  16. data/lib/buffer/client.rb +26 -0
  17. data/lib/buffer/core.rb +64 -0
  18. data/lib/buffer/datastructure.rb +18 -0
  19. data/lib/buffer/encode.rb +33 -0
  20. data/lib/buffer/error.rb +10 -0
  21. data/lib/buffer/info.rb +10 -0
  22. data/lib/buffer/link.rb +10 -0
  23. data/lib/buffer/profile.rb +27 -0
  24. data/lib/buffer/update.rb +75 -0
  25. data/lib/buffer/user.rb +9 -0
  26. data/lib/buffer/version.rb +1 -1
  27. data/lib/buffer.rb +16 -102
  28. data/spec/fixtures/destroy.txt +10 -0
  29. data/spec/fixtures/info.txt +10 -0
  30. data/spec/fixtures/interactions_by_update_id.txt +10 -0
  31. data/spec/fixtures/link.txt +12 -0
  32. data/spec/fixtures/profile_authenticated.txt +11 -0
  33. data/spec/fixtures/profile_schedules_by_id.txt +10 -0
  34. data/spec/fixtures/profiles_by_id.txt +10 -0
  35. data/spec/fixtures/update_by_id.txt +10 -0
  36. data/spec/fixtures/update_by_id_non_auth.txt +9 -0
  37. data/spec/fixtures/updates_by_profile_id.txt +10 -0
  38. data/spec/fixtures/updates_by_profile_id_pending.txt +10 -0
  39. data/spec/fixtures/user_authenticated.txt +19 -0
  40. data/spec/lib/buffer/encode_spec.rb +42 -0
  41. data/spec/lib/buffer/link_spec.rb +22 -0
  42. data/spec/lib/buffer/profile_spec.rb +87 -0
  43. data/spec/lib/buffer/schedule_spec.rb +80 -0
  44. data/spec/lib/buffer/update_spec.rb +227 -0
  45. data/spec/lib/buffer/user_spec.rb +26 -0
  46. data/spec/lib/buffer_spec.rb +30 -0
  47. data/spec/lib/core_spec.rb +60 -0
  48. data/spec/spec_helper.rb +171 -0
  49. metadata +215 -67
  50. data/spec/buffer_spec.rb +0 -354
  51. data/spec/fixtures/create_body.txt +0 -1
  52. data/spec/fixtures/success.json +0 -1
  53. data/spec/fixtures/user.json +0 -1
  54. data/spec/helper.rb +0 -43
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d9793acd4e784b551ab7fc634b25c6b6405e34f0
4
+ data.tar.gz: 52b18271fe05f996e8de43e55113f3e6acef5900
5
+ SHA512:
6
+ metadata.gz: a1144545ae627a84a30dfe1b2864fa25a14cf4851c3979a6fb5a359043425de4ee3acf6d3eb9ca23f3f32300a46311015c8ce3b62da6c67a0dd518d7ce7d12db
7
+ data.tar.gz: e26756d5d8a66ff72ab437039c239914faa37d2c0df7203f38ba38ce727bbb841fc8618b63f47d928f27bbd4d47f5a646eb0b240270a794583fdcd86428e3a74
@@ -0,0 +1,23 @@
1
+ ---
2
+ access_token: ASDFASDFASDF
3
+ profile_index: 0
4
+ version: 0.0.1
5
+
6
+ # How to Get Started:
7
+ # Create a Developer API Token here: http://bufferapp.com/developers/apps/create.
8
+ # Fill in Stuff. Your answers don't matter much for the purpose of this rudimentary setup.
9
+ # Submit that form and wait a short period (~2 min )
10
+ # Visit: http://bufferapp.com/developers/apps
11
+ # Gather Access Token and place it after the word "access_token"
12
+ # Copy this file to the root of your user's home folder:
13
+ # Set "profile_index" to 0 if you only have one account to post to. Otherwise it's more complicated ;). Find me on Twitter and I can explain (@_ZPH).
14
+ # - ~/.bufferapprc
15
+
16
+ # Structure:
17
+ # access_token: Access Token
18
+ # profile_index: Buffer Account number, ie posting to first account in list, use 0 (ie zero)
19
+ #
20
+ #TODO: improve instructions
21
+ # remove need for user to create their own App on bufferapp.com
22
+ # Future versions will integrate with Buffer-OAuth system.
23
+
data/.gitignore CHANGED
@@ -15,3 +15,9 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ notes.txt
19
+ .DS_STORE
20
+ spec/fixtures/updates_by_profile_id_sent.txt
21
+ utility.rb
22
+ tags
23
+ vendor
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ Encoding:
2
+ Enabled: true
3
+
4
+ LineLength:
5
+ Enabled: true
6
+ Max: 79
7
+
8
+ StringLiterals:
9
+ Enabled: false
10
+
11
+ AllCops:
12
+ # Includes:
13
+ Excludes:
14
+ - spec/**
15
+ - bin/**
16
+ - utility.rb
17
+ - Rakefile
18
+ - tmp/*
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.5
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ env:
5
+ - TRAVIS_CI=true
data/API_COVERAGE.md ADDED
@@ -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
+
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- # Specify your gem's dependencies in buffer.gemspec
3
+ gem 'coveralls', require: false
4
4
  gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ end
10
+
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Tom Ashworth
1
+ Copyright (c) 2013 ZPH
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,117 +1,99 @@
1
- # Buffer
1
+ # buffer
2
2
 
3
- Official API wrapper for [Buffer](http://bufferapp.com) covering user & profile management and adding, removing & editing updates, and more planned endpoints. See the [full API Documentation](http://bufferapp.com/developers/api/) for more.
3
+ buffer is a Buffer API Wrapper written in Ruby. It provides more thorough API coverage than the existing gem.
4
+
5
+ Since the gem is currently in ALPHA development, the interface is prone to change. Please wait until v0.1.0 is released to become reliant on interface. As it stands, all of the basic API calls in BufferApp's spec are available. Some of the optional params are yet to be implemented.
4
6
 
5
- For a Buffer OmniAuth strategy for authenticating your users with Buffer, see [omniauth-buffer2](/bufferapp/omniauth-buffer2).
6
7
 
7
8
  ## Installation
8
9
 
9
- Add this line to your application's Gemfile:
10
+ [![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)
10
11
 
11
- gem 'buffer'
12
+ For now please `git clone git@github.com:bufferapp/buffer-ruby.git` the repo
12
13
 
13
- And then execute:
14
+ Or
14
15
 
15
- $ bundle
16
+ Add this line to your application's Gemfile to include HEAD code:
16
17
 
17
- Or install it yourself as:
18
+ `gem 'buffer', :github => 'bufferapp/buffer-ruby'`
18
19
 
19
- $ gem install buffer
20
+ And then execute:
20
21
 
21
- ## Usage
22
+ `$ bundle`
22
23
 
23
- ### Client
24
+ Or install RubyGems version, which will receive more attention to stability:
24
25
 
25
- The most basic usage of the wrapper involves creating a `Buffer::Client` and then making requests through that object. Since all requests to the Buffer API require an access token you must have first authorized a Buffer user, or otherwise have an access token. The [Buffer OmniAuth Strategy](http://github.com/bufferapp/omniauth-buffer2) can help with this.
26
+ `$ gem install buffer`
26
27
 
27
- Use the Client if you just want to have full control over your `get` and `post` requests.
28
+ ## Usage
28
29
 
29
- Creating a new client is simple:
30
+ * All methods are tested with Rspec and WebMock. Most methods do not have integration tests that reach out to the live Buffer API servers. Proceed with caution until buffer reaches v0.1.0 and submit issues on Github Issues tab.
31
+ * Authentication is not included in this gem (Try OAuth-buffer2) or use the single API key given when registering your own Buffer Dev credentials.
32
+ * Commandline bin is provided to enable posting of updates:
33
+ `buffer Super witty stuff that fits in 140 chars`
34
+ Will post to your first account when setup following instructions below.
35
+ _A more convenient setup is planned in future releases._
36
+ * For convenience load credentials into environment as ENV variables:
30
37
 
31
- ```ruby
32
- buffer = Buffer::Client.new access_token
33
38
  ```
34
-
35
- ### User
36
-
37
- The User object makes working with users easier by providing some useful shortcuts to user information, like `id`, and data, like `profiles`. It provides the all the methods specified in the Client as it inherits from it.
38
-
39
- The User introduces some caching of requests. These are invalidated when a `post` request is made to an endpoint that might affect the data. You can force cache invalidation of one or all endpoints using the `invalidate` method.
40
-
41
- **Note: Currently only `invalidate` is implemented. If you make a POST request that changes a user object you must manually invalidate the cache**.
42
-
43
- Creating a new user:
44
-
45
- ```ruby
46
- user = Buffer::User.new access_token
39
+ export BUFFER_ACCESS_TOKEN="1/jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" # (BufferApp OAuth Access Token)
40
+ export BUFFER_PROFILE_ID="0" # (default of 0)
47
41
  ```
48
42
 
49
- ## API
43
+ ## Access Token Instructions
50
44
 
51
- You can use a client, or any subclass, to make GET & POST requests to the Buffer API. The exposed API methods are `get`, `post` and, the lowest level, `api`.
45
+ #### How to Get Started:
52
46
 
53
- #### api
47
+ * Create a Developer API Token here: http://bufferapp.com/developers/apps/create.
48
+ * Fill in Stuff. Your answers don't matter much for the purpose of this rudimentary setup.
49
+ * Submit that form and wait a short period (~2 min )
50
+ * Visit: http://bufferapp.com/developers/apps
51
+ * Gather Access Token and place it after the word "access_token"
52
+ * Set BUFFER_PROFILE_ID="0" if you only have one account to post to. Otherwise it's more complicated ;). Find me on Twitter and I can explain [@_ZPH](https://twitter.com/_ZPH).
54
53
 
55
- `api` is the method at the root of the API, handling all requests.
54
+ ## TODO:
56
55
 
57
- ```ruby
58
- api (symbol) method, (string) url, (hash, optional) params, (hash or array, optional) data
59
- # for example:
60
- buffer.api :get, 'user'
61
- ```
56
+ * Improve instructions
62
57
 
63
- `method` must be either `:get` or `:post`
58
+ #### Future versions will integrate with Buffer-OAuth system.
59
+ * Integrate Launchy for the purpose of launching browser window.
60
+ * Possible to model behavior on [ t.gem ](https://github.com/sferik/t/blob/master/lib/t/cli.rb#L56-L113)
64
61
 
65
- #### get
62
+ #### Raise error if message is beyond the character limit.
63
+ * Accomplish this via [ Twitter Text library ](https://github.com/twitter/twitter-text-rb)
64
+ * Refactor to simplify use of default params
66
65
 
67
- ```ruby
68
- user_data = buffer.get 'user'
69
- user_profiles = buffer.get 'profiles'
70
- ```
71
-
72
- `get` is just a thin layer over `api`, so the above is equivalent to:
66
+ ## API Coverage
73
67
 
74
- ```ruby
75
- user_data = buffer.api :get, 'user'
76
- user_profiles = buffer.api :get, 'profiles'
77
- ```
68
+ #### Implemented
78
69
 
79
- #### `post`
70
+ * User
71
+ * Profiles (:get, :post)
72
+ * Updates (:get, :post)
73
+ * Links
74
+ * Info
75
+ * Error Codes
80
76
 
81
- ```ruby
82
- user_data = buffer.post 'updates/create', :text => "Hello, world!", :profile_ids => ['123abc456', '789def123']
83
- ```
77
+ Further Details [API Coverage](API_COVERAGE.md)
84
78
 
85
- `post` is also a wrapper for `api`, so the above becomes:
79
+ #### Not Implemented
86
80
 
87
- ```ruby
88
- user_data = buffer.api :post, 'updates/create', :text => "Hello, world!", :profile_ids => ['123abc456', '789def123']
89
- ```
81
+ * Caching
90
82
 
91
- ## User API
83
+ ## Supported Ruby Implementations
84
+ - MRI 2.0.0
85
+ - Others likely work but are not included in CI Server
92
86
 
93
- #### `id`, `created_at`...
87
+ ## Contributing
94
88
 
95
- The User object allows access to the data from the user endpoint in manner of a normal ruby accessor. This makes accessing user info very easy, like the following:
89
+ 1. Fork it
90
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
91
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
92
+ 4. Push to the branch (`git push origin my-new-feature`)
93
+ 5. Create new Pull Request
96
94
 
97
- ```ruby
98
- user.id
99
- user.created_at
100
- ```
101
-
102
- #### `profiles` **not implemented**
103
-
104
- `profiles` is a helper method that gives you access to the profiles associated with a particular user. It's shorthand for `get 'profiles'` with caching. The
105
-
106
- `profiles` is invalidated by any `post` request made to a child of the `profiles` endpoing, like `/profiles/:id/schedules/update`.
107
-
108
- ```ruby
109
- user.profiles # all a user's profiles
110
- user.profiles '123abc456def' # return a profile with a specific id
111
- ```
95
+ Issues, refactoring, and feedback are all welcome.
112
96
 
113
- ## Todo
97
+ Also, this project is newcomer friendly!! We'd love to be your first Open Source Software contribution and would be happy to assist in that process.
114
98
 
115
- * `user.profiles`
116
- * Automatic cache invalidation after post request
117
- * Move cache handling to a mixin
99
+ Crafted with care by Zander. Reach out and say hi at [@_ZPH](http://twitter.com/_ZPH) or [civet.ws](http://www.civet.ws)
data/Rakefile CHANGED
@@ -1,2 +1,26 @@
1
- #!/usr/bin/env rake
2
1
  require "bundler/gem_tasks"
2
+ require 'rake'
3
+ require 'rspec/core/rake_task'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc "Run specs"
9
+ RSpec::Core::RakeTask.new do |t|
10
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
11
+ # Put spec opts in a file named .rspec in root
12
+ end
13
+
14
+ desc "Generate code coverage"
15
+ RSpec::Core::RakeTask.new(:coverage) do |t|
16
+ t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
17
+ t.rcov = true
18
+ t.rcov_opts = %w[--exclude spec]
19
+ end
20
+
21
+ task :default => :spec
22
+
23
+ task :curl_dump, [ :url ] do |t, args|
24
+ access_token = `cat ~/.bufferapprc | head -3 | tail -1`.chomp
25
+ sh "curl -is #{args[:url]}?access_token=#{access_token}"
26
+ end
data/TODO.md ADDED
@@ -0,0 +1,3 @@
1
+ Improve Interface for #create_update to not need :body in args
2
+ Add error if message is obviously above 140 char
3
+ - Extra feature to shorten web links to length used by t.co
data/bin/buffer ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'buffer'
4
+ require 'logger'
5
+
6
+ log_level = ENV['BUFFER_DEBUG'] ? Logger::INFO : Logger::ERROR
7
+
8
+ LOG = Logger.new(STDOUT) { |l| l.log_level = log_level }
9
+
10
+ def gather_message(argv)
11
+ args = argv.dup
12
+ case args.count
13
+ when 0 then abort("Please supply a message for Buffer Post.")
14
+ when 1 then args.first
15
+ else
16
+ args.join(" ")
17
+ end
18
+ end
19
+
20
+ def main
21
+ token = Env.BUFFER_ACCESS_TOKEN
22
+ profile = Integer(ENV.fetch('BUFFER_PROFILE_ID', "0"))
23
+
24
+ message = gather_message(ARGV)
25
+ client = Buffer::Client.new(token)
26
+ profile_id = client.profiles[profile].id
27
+ post_args = {text: message, profile_ids: [profile_id]}
28
+ LOG.info('#main.post_args') { post_args }
29
+ unless ENV['BUFFER_DEBUG']
30
+ client.create_update(body: post_args)
31
+ puts "Posted message."
32
+ end
33
+ end
34
+
35
+ main
data/buffer.gemspec CHANGED
@@ -1,26 +1,36 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/buffer/version', __FILE__)
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'buffer/version'
3
5
 
4
6
  Gem::Specification.new do |gem|
5
- gem.authors = ["Tom Ashworth"]
6
- gem.email = ["tom@phuu.net"]
7
- gem.description = "Buffer API wrapper for Ruby"
8
- gem.summary = "Buffer API wrapper for Ruby"
9
- gem.homepage = ""
7
+ gem.name = "buffer"
8
+ gem.version = Buffer::VERSION
9
+ gem.authors = ["ZPH"]
10
+ gem.email = ["Zander@civet.ws"]
11
+ gem.description = %q{Buffer is an API Wrapper Gem for Bufferapp.com's API}
12
+ gem.summary = %q{Buffer is an API Wrapper Gem for Bufferapp.com's API}
13
+ gem.homepage = "http://github.com/bufferapp/buffer-ruby"
10
14
 
11
- gem.files = `git ls-files`.split($\)
15
+ gem.files = `git ls-files`.split($/)
12
16
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
- gem.name = "buffer"
15
18
  gem.require_paths = ["lib"]
16
- gem.version = Buffer::VERSION
17
19
 
18
- gem.add_dependency "faraday"
19
- gem.add_dependency "multi_json"
20
- gem.add_dependency "i18n"
21
- gem.add_dependency "active_support"
20
+ gem.add_development_dependency 'rspec'
21
+ gem.add_development_dependency 'webmock'
22
+ gem.add_development_dependency 'guard-rspec'
23
+ gem.add_development_dependency 'guard-bundler'
24
+ gem.add_development_dependency 'rb-fsevent'
25
+ gem.add_development_dependency 'growl'
26
+ gem.add_development_dependency 'pry-uber'
22
27
 
23
- gem.add_development_dependency "rspec", "~> 2.7"
24
- gem.add_development_dependency "simplecov"
25
- gem.add_development_dependency "webmock"
28
+ gem.add_runtime_dependency 'multi_json'
29
+ gem.add_runtime_dependency 'yajl-ruby'
30
+ gem.add_runtime_dependency 'faraday_middleware'
31
+ gem.add_runtime_dependency 'faraday'
32
+ gem.add_runtime_dependency 'hashie'
33
+ gem.add_runtime_dependency 'rake'
34
+ gem.add_runtime_dependency 'addressable'
35
+ gem.add_runtime_dependency 'environs'
26
36
  end
@@ -0,0 +1,26 @@
1
+ module Buffer
2
+ class Client
3
+ include Core
4
+ include User
5
+ include Profile
6
+ include Update
7
+ include Link
8
+ include Info
9
+
10
+ attr_accessor :access_token
11
+
12
+ def initialize(access_token)
13
+ @access_token = access_token
14
+ url = "https://api.bufferapp.com/1/"
15
+ @connection = Faraday.new(url: url) do |faraday|
16
+ faraday.request :url_encoded
17
+ faraday.adapter Faraday.default_adapter
18
+ end
19
+ end
20
+
21
+ def auth_query
22
+ { access_token: @access_token }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,64 @@
1
+ require 'pathname'
2
+
3
+ module Buffer
4
+ class Client
5
+ module Core
6
+ API_VERSION = "1"
7
+
8
+ private
9
+
10
+ def get(path, options = {})
11
+ options.merge!(auth_query)
12
+ response = @connection.get do |req|
13
+ req.url path.remove_leading_slash
14
+ req.params = options
15
+ end
16
+
17
+ interpret_response(response)
18
+ end
19
+
20
+ def post(path, options = {})
21
+ params = merge_auth_token_and_query(options)
22
+ params.merge!(options)
23
+ response = @connection.post do |req|
24
+ req.url path.remove_leading_slash
25
+ req.headers['Content-Type'] = "application/x-www-form-urlencoded"
26
+ req.body = options[:body]
27
+ req.params = params
28
+ end
29
+
30
+ Hashie::Mash.new(JSON.parse response.body)
31
+ end
32
+
33
+ def merge_auth_token_and_query(options)
34
+ if options[:query]
35
+ auth_query.merge options[:query]
36
+ else
37
+ auth_query
38
+ end
39
+ end
40
+
41
+ def interpret_response(response)
42
+ if response.status == 200
43
+ JSON.parse response.body
44
+ else
45
+ handle_response_code(response)
46
+ end
47
+ end
48
+
49
+ def handle_response_code(response)
50
+ error = Hashie::Mash.new(response.body)
51
+ raise Buffer::Error::APIError unless error.code
52
+ "Buffer API Error Code: #{error.code}\n" +
53
+ "HTTP Code: #{response.code}." +
54
+ "Description: #{error.error}"
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ class String
61
+ def remove_leading_slash
62
+ gsub(/^\//, '')
63
+ end
64
+ end
@@ -0,0 +1,18 @@
1
+ module Buffer
2
+ class UserInfo < Hashie::Mash; end
3
+ class Profile < Hashie::Mash; end
4
+ class Response < Hashie::Mash; end
5
+ class Update < Hashie::Mash; end
6
+ class Updates < Hashie::Mash; end
7
+ class Interaction < Hashie::Mash; end
8
+ class Interactions < Hashie::Mash; end
9
+ class Link < Hashie::Mash; end
10
+ class Info < Hashie::Mash; end
11
+
12
+ class Schedule < Hashie::Mash; end
13
+ Schedules = Class.new(Array) do
14
+ def dump
15
+ { schedules: self }.to_json
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,33 @@
1
+ module Buffer
2
+ class Encode
3
+ class << self
4
+ def encode(arg)
5
+ raise_error_for_incorrect_input(arg)
6
+ arg = arg[:schedules] if arg.respond_to?(:keys)
7
+ arg.map.with_index do |item, index|
8
+ process_schedule(item, index)
9
+ end.join("&")
10
+ end
11
+
12
+ private
13
+
14
+ def raise_error_for_incorrect_input(arg)
15
+ unless arg.kind_of?(Hash) || arg.kind_of?(Array)
16
+ raise ArgumentError, "Input must be/inherit from Hash or Array"
17
+ end
18
+ end
19
+
20
+ def process_schedule(item, index)
21
+ pairs_for(item).map do |key, value|
22
+ "schedules[#{index}][#{key}][]=#{value}"
23
+ end.join("&")
24
+ end
25
+
26
+ def pairs_for(item)
27
+ uri = Addressable::URI.new
28
+ uri.query_values = item
29
+ uri.query.split("&").map {|p| p.split("=")}
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,10 @@
1
+ module Buffer
2
+ module Error
3
+ ConfigFileMissing = Class.new(StandardError)
4
+ InvalidIdLength = Class.new(ArgumentError)
5
+ InvalidIdContent = Class.new(ArgumentError)
6
+ MissingStatus = Class.new(ArgumentError)
7
+ APIError = Class.new(StandardError)
8
+ UnauthorizedRequest = Class.new(StandardError)
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Buffer
2
+ class Client
3
+ module Info
4
+ def info
5
+ response = get("/info/configuration.json")
6
+ Buffer::Info.new(response)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module Buffer
2
+ class Client
3
+ module Link
4
+ def link(options)
5
+ response = get("/links/shares.json", options)
6
+ Buffer::Link.new(response)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ module Buffer
2
+ class Client
3
+ module Profile
4
+ def profiles
5
+ response = get("/profiles.json")
6
+ response.map { |profile| Buffer::Profile.new(profile) }
7
+ end
8
+
9
+ def profile_by_id(id)
10
+ response = get("/profiles/#{id}.json")
11
+ Buffer::Profile.new(response)
12
+ end
13
+
14
+ def schedules_by_profile_id(id)
15
+ response = get("/profiles/#{id}/schedules.json")
16
+ response.map { |a_response| Buffer::Schedule.new(a_response) }
17
+ end
18
+
19
+ def set_schedules(id, options)
20
+ schedules = Buffer::Encode.encode(
21
+ options.fetch(:schedules) { raise ArgumentError })
22
+ post("/profiles/#{id}/schedules/update.json",
23
+ body: { schedules: schedules })
24
+ end
25
+ end
26
+ end
27
+ end