From 294ea86abc2dac9027b9c6f1bf6dfd8273f77f29 Mon Sep 17 00:00:00 2001 From: kaneda Date: Sat, 18 Jul 2015 10:46:52 -0400 Subject: [PATCH 1/5] [dev-rspecs-2] Updates - Fixed docs - Added missing methods to buffer client - Add deauth to user API - Added new specs - Updated readme --- lib/api/helpers/api_helpers.rb | 12 +- lib/api/user_api.rb | 8 +- lib/buffer_client.rb | 21 +++- readme.md | 80 ++++++------- spec/lib/api/helpers/api_helpers_spec.rb | 137 +++++++++++++++++++++++ spec/lib/api/update_api_spec.rb | 25 +++++ 6 files changed, 236 insertions(+), 47 deletions(-) create mode 100644 spec/lib/api/helpers/api_helpers_spec.rb create mode 100644 spec/lib/api/update_api_spec.rb diff --git a/lib/api/helpers/api_helpers.rb b/lib/api/helpers/api_helpers.rb index e99a511..c28471c 100644 --- a/lib/api/helpers/api_helpers.rb +++ b/lib/api/helpers/api_helpers.rb @@ -38,12 +38,12 @@ def has_data?(response) response.present? && response.body.present? end - def set_err(json_response) + def set_err(response_code, json_response) if json_response.present? if json_response["error"].present? @error = json_response["error"] elsif json_response["code"].present? - @error = get_error_message(response.code, json_response["code"]) + @error = get_error_message(response_code, json_response["code"]) else @error = DEFAULT_ERR end @@ -58,13 +58,15 @@ def parse_data(response) begin json_response = JSON.parse(response.body) rescue => e - Rails.logger.error "Failed to parse JSON return from Buffer: #{e}" + msg = "Failed to parse JSON return from Buffer: #{e}" + log_or_print msg + @error = msg end - if response.code == GOOD_RESPONSE + if response.code == GOOD_RESPONSE || json_response.nil? return json_response else - set_err(json_response) + set_err(response.code, json_response) end end diff --git a/lib/api/user_api.rb b/lib/api/user_api.rb index b9cabe7..2f420ca 100644 --- a/lib/api/user_api.rb +++ b/lib/api/user_api.rb @@ -3,7 +3,8 @@ class UserApi < BaseApi # PATHS - USER_PATH = "user.json" + USER_PATH = "user.json" + DEAUTH_PATH = "user/deauthorize.json" def get_user_id get_user_json["id"] rescue nil @@ -13,4 +14,9 @@ def get_user_json return nil unless verify_token get_get_response( build_url(USER_PATH) ) end + + def deauthorize + return nil unless verify_token + get_get_response( build_url(DEAUTH_PATH) ) + end end diff --git a/lib/buffer_client.rb b/lib/buffer_client.rb index 6fa50c0..ec5d2b8 100644 --- a/lib/buffer_client.rb +++ b/lib/buffer_client.rb @@ -82,6 +82,13 @@ def get_user_json user_json end + def deauthorize + success_json = @user_api.deauthorize + record_err(@user_api) + + is_success?(success_json) + end + ############### # PROFILE API # ############### @@ -108,6 +115,7 @@ def get_schedule(id) end def update_schedule(id, sched_array) + sched_array.deep_symbolize_keys! success_json = @profile_api.update_schedule(id, sched_array) record_err(@profile_api) @@ -153,7 +161,14 @@ def reorder_updates(id, updates_array, options = {}) new_order_json = @update_api.reorder_updates(id, updates_array, options) record_err(@update_api) - new_order_json + extract_key(new_order_json, "updates") + end + + def shuffle_updates(id, options = {}) + shuffle_json = @update_api.shuffle_updates(id, options) + record_err(@update_api) + + extract_key(shuffle_json, "updates") end def create_update(profile_ids, options = {}) @@ -229,4 +244,8 @@ def is_success?(success_json) def record_err(api) @error = api.get_error if api.has_error? end + + def extract_key(json, key) + return json[key] if json.present? + end end diff --git a/readme.md b/readme.md index 38abf9f..23adfcc 100644 --- a/readme.md +++ b/readme.md @@ -1,24 +1,23 @@ # Buffer Gem -Modelled after Octokit <3. This is a lightweight client for the Buffer API (https://buffer.com/developers/api), which is intended to return JSON as opposed to objects. +Modelled after [Octokit](https://github.com/octokit/octokit.rb) <3, this is a lightweight client for the [Buffer API](https://buffer.com/developers/api), which is intended to return (mostly) JSON as opposed to objects. [![Code Climate](https://codeclimate.com/github/kaneda/buffer-ruby/badges/gpa.svg)](https://codeclimate.com/github/kaneda/buffer-ruby) [![Test Coverage](https://codeclimate.com/github/kaneda/buffer-ruby/badges/coverage.svg)](https://codeclimate.com/github/kaneda/buffer-ruby/coverage) +[![Build Status](https://travis-ci.org/kaneda/buffer-ruby.svg?branch=master)](https://travis-ci.org/kaneda/buffer-ruby) ## Client API ### What's Imlemented? -* https://buffer.com/developers/api/oauth -* https://buffer.com/developers/api/user -* https://buffer.com/developers/api/profiles -* https://buffer.com/developers/api/updates -* https://buffer.com/developers/api/links -* https://buffer.com/developers/api/info -* https://buffer.com/developers/api/errors - - -### What's Missing? -* Rspecs +* [OAuth](https://buffer.com/developers/api/oauth) +* [Users](https://buffer.com/developers/api/user) +* [Profiles](https://buffer.com/developers/api/profiles) +* [Updates](https://buffer.com/developers/api/updates) +* [Links](https://buffer.com/developers/api/links) +* [Info](https://buffer.com/developers/api/info) +* [Errors](https://buffer.com/developers/api/errors) +* Automated builds through Travis-CI +* Automated tests using RSpec ### Installing @@ -45,39 +44,39 @@ You can define the client up front or configure it later. The options array the buffer_client = BufferClient.new({ :user_code => "yourcode" }) -``` -```ruby -buffer_client = BufferClient.new +auth_tok = buffer_client.get_auth_token buffer_client.configure({ - :user_code => "yourcode" + :auth_token => auth_tok }) ``` ### Available Calls -| Client call | Input | Description | Notes -| :---------: | :------ | :---- | :--------- -| get_auth_token | | Returns the user's long-lasting auth token | user_code must be defined in the buffer_client, as well as the ENV variables "BUFFER_KEY" and "BUFFER_SECRET" | -| get_user_id | | Returns the user's Buffer ID | | -| get_user_json | | Returns the entirety of the user JSON | | -| get_user_profiles | | Returns the entirety of the profile JSON | | -| get_user_profile | Profile ID | Get a single profile as JSON by ID | | -| get_schedule | Profile ID | Get the schedule of a profile as JSON by ID | | -| update_schedule | Profile ID, Schedule Hash | Set the schedule of a profile as JSON by ID | auth_token must be defined in the buffer client. See below for schedule representation | -| get_update | Social Media Post ID | Gets an update by post ID | | -| get_pending_updates | Profile ID, Options Hash (optional) | Gets pending updates as JSON by profile ID | Takes in hash of options, see Buffer API docs for optional parameters | -| get_sent_updates | Profile ID, Options Hash (optional) | Gets sent updates as JSON by profile ID | Takes in hash of options, see Buffer API docs for optional parameters | -| get_interactions | Social Media Post ID, Event, Options Hash (optional) | Gets interactions based on event type (see https://bufferapp.com/developers/api/info#configuration) | Takes in a hash of options, see Buffer API docs for optional parameters | -| reorder_updates | Profile ID, Updates Array, Options Hash (optional) | Updates order of updates in a profile based on updates array | | -| shuffle_updates | Profile ID, Options Hash (optional) | Randomize the order of updates to be sent | | -| create_update | Profile ID Array, Options Hash (optional) | Create a new post | Note that for the "media" option, please specify each media option in the hash separately, e.g. ```{ "media[link]" => "http%3A%2F%2Fgoogle.com", "media[description]" => "The%20google%20homepage" }``` | -| update_status | Social Media Post ID, Text, Options Hash (optional) | Update an existing status | For the "media" option see the note on create_update | -| share_update | Social Media Post ID | Share a post immediately | | -| destroy_update | Social Media Post ID| Permanently destroy an update | | -| move_to_top | Social Media Post ID| Move post to top of queue | | -| get_shares | URL (unencoded) | Gets the number of shares for a given URL through Buffer | You can pass a normal URL here, the client will encode it. This is one of the only calls to not require an auth_token | -| get_configuration | | Gets the current Buffer config | | + +| Client call | Input | Output | Notes +| :---------: | :------ | :------ | :--------- +| [get_auth_token](https://buffer.com/developers/api/oauth) | | Auth Token:String | ENV variables "BUFFER_KEY" and "BUFFER_SECRET" must be defined here | +| get_user_id | | ID:String | Convenience wrapper, calls get_user_json under the hood | +| [get_user_json](https://buffer.com/developers/api/user#user) | | User:Hash | | +| [deauthorize](https://buffer.com/developers/api/user#deauthorize) | | Success:Boolean | | +| [get_user_profiles](https://buffer.com/developers/api/profiles#profiles) | | Profiles:Array | | +| [get_user_profile](https://buffer.com/developers/api/profiles#profilesid) | Profile ID:String | Profile:Hash | | +| [get_schedule](https://buffer.com/developers/api/profiles#schedules) | Profile ID:String | Schedule:Hash | | +| [update_schedule](https://buffer.com/developers/api/profiles#schedulesupdate) | Profile ID:String, Schedule:Hash | Success:Boolean | See below for schedule representation | +| [get_update](https://buffer.com/developers/api/updates#updatesid) | Social Media Post ID:String | Update:Hash | | +| [get_pending_updates](https://buffer.com/developers/api/updates#updatespending) | Profile ID:String, Options:Hash (optional) | UpdateResult:Hash | See Buffer API doc (link to the left) for optional parameters. "updates" key contains the Updates:Array | +| [get_sent_updates](https://buffer.com/developers/api/updates#updatessent) | Profile ID:String, Options:Hash (optional) | UpdateResult:Hash | See Buffer API doc (link to the left) for optional parameters. "updates" key contains the Updates:Array | +| [get_interactions](https://buffer.com/developers/api/updates#updatesinteractions) | Social Media Post ID:String, Event:ENUM, Options:Hash (optional) | Interactions:Hash | See [event types](https://bufferapp.com/developers/api/info#configuration) for possible event values and Buffer API doc (link to the left) for optional parameters. "interactions" key contains the Interactions:Array | +| [reorder_updates](https://buffer.com/developers/api/updates#updatesreorder) | Profile ID:String, Updates:Array, Options:Hash (optional) | Updates:Array | | +| [shuffle_updates](https://buffer.com/developers/api/updates#updatesshuffle) | Profile ID:String, Options:Hash (optional) | Updates:Array | | +| [create_update](https://buffer.com/developers/api/updates#updatescreate) | Profile IDs:Array, Options:Hash (optional) | Update:Hash | Note that for the "media" option, please specify each media option in the hash separately, e.g. ```{ "media[link]" => "http%3A%2F%2Fgoogle.com", "media[description]" => "The%20google%20homepage" }```. See all available options in the Buffer docs (link to the left) | +| [update_status](https://buffer.com/developers/api/updates#updatesupdate) | Social Media Post ID:String, Text:String, Options:Hash (optional) | Update:Hash | For the "media" option see the note on create_update above | +| [share_update](https://buffer.com/developers/api/updates#updatesshare) | Social Media Post ID:String | Success:Boolean | | +| [destroy_update](https://buffer.com/developers/api/updates#updatesdestroy) | Social Media Post ID:String | Success:Boolean | | +| [move_to_top](https://buffer.com/developers/api/updates#updatesmovetotop) | Social Media Post ID:String | Success:Boolean | | +| [get_shares](https://buffer.com/developers/api/links#shares) | Unencodded URL:String | Shares:Integer | You can pass a normal URL here, the client will encode it. This is one of the only calls to not require an auth_token | +| [get_configuration](https://buffer.com/developers/api/info#configuration) | | Configuration:Hash | "services" key has internal keys for each service | ### Helper methods | Method | Description | @@ -104,6 +103,7 @@ To update a schedule the BufferClient is expecting a schedule of the form: ] ``` +Note that you can make the keys symbols or strings, the client will accept either one. ## Contributing To contribute simply: @@ -111,13 +111,13 @@ To contribute simply: 1. Fork this project 2. Make your changes in a new branch 3. Create a PR -4. Once approved squash your commits (http://davidwalsh.name/squash-commits-git) +4. Once approved [squash your commits](http://davidwalsh.name/squash-commits-git) 5. Party ## Contact Email: kanedasan@gmail.com -Twitter: @kanedasan +Twitter: [@kanedasan](https://twitter.com/kanedasan) IRC: kaneda^ on FreeNode (##hackers) diff --git a/spec/lib/api/helpers/api_helpers_spec.rb b/spec/lib/api/helpers/api_helpers_spec.rb new file mode 100644 index 0000000..d444d63 --- /dev/null +++ b/spec/lib/api/helpers/api_helpers_spec.rb @@ -0,0 +1,137 @@ +require 'action_dispatch' +require 'uri' +require_relative '../../../../lib/api/helpers/api_helpers.rb' + +describe ApiHelpers do + let(:base_api) { build(:base_api) } + let(:options) do + { + :key => "value", + :key_2 => "value_2" + } + end + + let(:err) { "An error" } + + describe "#build_options_string" do + let(:path_regex) do + /\A(\w+=[\w\d]+(&\w+=[\w\d]+)+)*\Z/ + end + + it "returns a string representing a valid path" do + expect(base_api.build_options_string(options)).to match(path_regex) + end + end + + describe "#build_url" do + let(:path) { "some_path" } + let(:auth_tok) { "123456789" } + + it "returns a valid URL when given only a path" do + expect(base_api.build_url(path, {}, false)).to match(URI::regexp) + end + + it "returns a valid URL when given a path and options" do + expect(base_api.build_url(path, options, false)).to match(URI::regexp) + end + + it "returns a valid URL when given a path and an auth token" do + base_api.instance_variable_set(:@auth_token, auth_tok) + + expect(base_api.build_url(path)).to match(URI::regexp) + end + + it "returns a valid URL when given a path, options, and an auth token" do + base_api.instance_variable_set(:@auth_token, auth_tok) + + expect(base_api.build_url(path, options)).to match(URI::regexp) + end + end + + describe "#set_err" do + let(:response_code) { "403" } + let(:err) { "An error" } + let(:err_2) { "A second err" } + let(:json_with_error) { { "error" => err } } + let(:json_with_code) { { "code" => "1001" } } + let(:json_with_other) { { "other" => "stuff" } } + + it "returns the default error when response is blank" do + expect(base_api.set_err(response_code, {})).to eq(ApiHelpers::DEFAULT_ERR) + end + + it "returns an error when the error key is present in the response" do + expect(base_api.set_err(response_code, json_with_error)).to eq(err) + end + + it "returns another error when only the code key is present" do + allow_any_instance_of(BaseApi).to receive(:get_error_message).and_return(err_2) + expect(base_api.set_err(response_code, json_with_code)).to eq(err_2) + end + + it "returns the default error when response is present but neither error nor code is present" do + expect(base_api.set_err(response_code, json_with_other)).to eq(ApiHelpers::DEFAULT_ERR) + end + end + + describe "#has_data?" do + let(:empty_response) do + ActionDispatch::Response.new(500, {}, "") + end + + let(:valid_response) do + ActionDispatch::Response.new(200, {}, "A body") + end + + it "returns false when passed a blank response" do + expect(base_api.has_data?(empty_response)).to eq(false) + end + + it "returns true when passed a response with a body" do + expect(base_api.has_data?(valid_response)).to eq(true) + end + end + + describe "#parse_data" do + let(:bad_json) { '{"foo": "bar"' } + let(:bad_json_err) { "Failed to parse JSON return from Buffer" } + let(:bad_response) do + ActionDispatch::Response.new(403, {}, bad_json) + end + + let(:good_json) { "#{bad_json} }" } + let(:good_response) do + ActionDispatch::Response.new(200, {}, good_json) + end + + it "returns nil and sets @error if the response is invalid JSON" do + json = base_api.parse_data(bad_response) + expect(json).to be_nil + expect(base_api.error).to include(bad_json_err) + end + + it "returns a hash when passed good JSON" do + json = base_api.parse_data(good_response) + expect(json).to be_present + expect(json.is_a?(Hash)).to eq(true) + end + end + + describe "#get_error_message" do + let(:event_err) { "Event type not supported." } + let(:http_code) { "400" } + let(:err_code) { "1029" } + + it "returns the event err when the given codes are passed" do + expect(base_api.get_error_message(http_code, err_code)).to eq(event_err) + end + + it "returns the default error when the http code is unknown" do + expect(base_api.get_error_message("10101", err_code)).to eq(ApiHelpers::DEFAULT_ERR) + end + + it "returns the default error when the error code is unknown" do + expect(base_api.get_error_message(http_code, "10101")).to eq(ApiHelpers::DEFAULT_ERR) + end + end +end diff --git a/spec/lib/api/update_api_spec.rb b/spec/lib/api/update_api_spec.rb new file mode 100644 index 0000000..b9f8d9b --- /dev/null +++ b/spec/lib/api/update_api_spec.rb @@ -0,0 +1,25 @@ +require_relative '../../../lib/api/update_api.rb' + +describe UpdateApi do + let(:update_api) { build(:update_api) } + let(:id) { "123456789" } + let(:path) { "some_path" } + + before(:each) do + allow_any_instance_of(UpdateApi).to receive(:verify_token).and_return(true) + end + + describe "#build_profile_url" do + it "returns a proper profile URL" do + expected_val = "#{UpdateApi::PROFILE_PATH}/#{id}/#{path}" + expect(update_api.send(:build_profile_url, id, path)).to include(expected_val) + end + end + + describe "#build_update_url" do + it "returns a proper update URL" do + expected_val = "#{UpdateApi::UPDATE_PATH}/#{id}/#{path}" + expect(update_api.send(:build_update_url, id, path)).to include(expected_val) + end + end +end From a3df6775777c58248f9bfe0436c30f93a8ba3694 Mon Sep 17 00:00:00 2001 From: kaneda Date: Sat, 18 Jul 2015 18:08:38 -0400 Subject: [PATCH 2/5] [dev-rspecs-3] Added more coverage - Full coverage of api helpers - 1.9.3 fix - Added placeholder specs to ensure that logic doesn't get broken, however simple --- lib/api/helpers/api_helpers.rb | 18 ++++- readme.md | 6 ++ spec/lib/api/helpers/api_helpers_spec.rb | 69 ++++++++++++++++++- spec/lib/api/info_api_spec.rb | 19 ++++++ spec/lib/api/link_api_spec.rb | 19 ++++++ spec/lib/api/profile_api_spec.rb | 33 ++++++++- spec/lib/api/update_api_spec.rb | 85 ++++++++++++++++++++++++ spec/lib/api/user_api_spec.rb | 25 +++++++ 8 files changed, 267 insertions(+), 7 deletions(-) create mode 100644 spec/lib/api/info_api_spec.rb create mode 100644 spec/lib/api/link_api_spec.rb create mode 100644 spec/lib/api/user_api_spec.rb diff --git a/lib/api/helpers/api_helpers.rb b/lib/api/helpers/api_helpers.rb index c28471c..b8e72e0 100644 --- a/lib/api/helpers/api_helpers.rb +++ b/lib/api/helpers/api_helpers.rb @@ -71,7 +71,9 @@ def parse_data(response) end def get_http_obj(url) - uri = URI.parse(url) + uri = URI.parse(url) + raise URI::InvalidURIError if uri.host.nil? + http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE @@ -82,7 +84,12 @@ def get_http_obj(url) def get_get_response(url) uri, http = get_http_obj(url) - req = Net::HTTP::Get.new(uri) + req = begin + Net::HTTP::Get.new(uri) + rescue + # Fall back in case of Ruby 1.9.3 + Net::HTTP::Get.new(uri.request_uri) + end response = http.request(req) parse_data(response) @@ -91,7 +98,12 @@ def get_get_response(url) def get_post_response(url, post_data = "") uri, http = get_http_obj(url) - req = Net::HTTP::Post.new(uri) + req = begin + Net::HTTP::Post.new(uri) + rescue + # Fall back in case of Ruby 1.9.3 + Net::HTTP::Post.new(uri.request_uri) + end req.content_type = "application/x-www-form-urlencoded" req.body = post_data response = http.request(req) diff --git a/readme.md b/readme.md index 23adfcc..eaf8bb9 100644 --- a/readme.md +++ b/readme.md @@ -6,6 +6,12 @@ Modelled after [Octokit](https://github.com/octokit/octokit.rb) <3, this is a li [![Test Coverage](https://codeclimate.com/github/kaneda/buffer-ruby/badges/coverage.svg)](https://codeclimate.com/github/kaneda/buffer-ruby/coverage) [![Build Status](https://travis-ci.org/kaneda/buffer-ruby.svg?branch=master)](https://travis-ci.org/kaneda/buffer-ruby) +## Ruby Versions + +This gem is built to work with Ruby 1.9.3+ + +See the Travis-CI build information above for build status on 1.9.3 and 2.1.2 + ## Client API ### What's Imlemented? diff --git a/spec/lib/api/helpers/api_helpers_spec.rb b/spec/lib/api/helpers/api_helpers_spec.rb index d444d63..3a999cf 100644 --- a/spec/lib/api/helpers/api_helpers_spec.rb +++ b/spec/lib/api/helpers/api_helpers_spec.rb @@ -11,7 +11,12 @@ } end + let(:good_hash) { options } + let(:err) { "An error" } + let(:good_url) { "http://jbegleiter.com" } + let(:bad_json) { '{"foo": "bar"' } + let(:good_json) { "#{bad_json} }" } describe "#build_options_string" do let(:path_regex) do @@ -50,7 +55,6 @@ describe "#set_err" do let(:response_code) { "403" } - let(:err) { "An error" } let(:err_2) { "A second err" } let(:json_with_error) { { "error" => err } } let(:json_with_code) { { "code" => "1001" } } @@ -93,13 +97,11 @@ end describe "#parse_data" do - let(:bad_json) { '{"foo": "bar"' } let(:bad_json_err) { "Failed to parse JSON return from Buffer" } let(:bad_response) do ActionDispatch::Response.new(403, {}, bad_json) end - let(:good_json) { "#{bad_json} }" } let(:good_response) do ActionDispatch::Response.new(200, {}, good_json) end @@ -134,4 +136,65 @@ expect(base_api.get_error_message(http_code, "10101")).to eq(ApiHelpers::DEFAULT_ERR) end end + + describe "#get_http_obj" do + let(:bad_url) { "foo" } + + it "raises an error when passed an invalid URL" do + expect { base_api.get_http_obj(bad_url) }.to raise_error + end + + it "returns an array of URI, HTTP object when passed a valid URL" do + uri, http = base_api.get_http_obj(good_url) + expect(uri).to be_present + expect(http).to be_present + end + end + + # Test that these don't blow up keeping in mind that + # get_http_obj and parse_data are tested above + describe "#get_functions" do + before(:each) do + # Stubbed, as this is tested above + allow_any_instance_of(BaseApi).to receive(:parse_data).and_return(good_hash) + + # Since we're stubbing parse it doesn't matter what this returns + allow_any_instance_of(Net::HTTP).to receive(:request).and_return({}) + end + + describe "#get_post_response" do + let(:post_data) { "foo=bar&boz=bot" } + + it "doesn't explode when post data is set" do + expect(base_api.get_post_response(good_url, post_data)).to eq(good_hash) + end + + it "doesn't explode when post data isn't set" do + expect(base_api.get_post_response(good_url)).to eq(good_hash) + end + end + + describe "#get_get_response" do + it "doesn't explode" do + expect(base_api.get_get_response(good_url)).to eq(good_hash) + end + end + end + + describe "#log_or_print" do + let(:msg) { "A message" } + let(:log_level) { "info" } + + it "prints to the logger when logger is defined" do + logger = Logger.new(STDOUT) + base_api.instance_variable_set(:@logger, logger) + + expect(logger).to receive(:send).with(log_level, msg) + base_api.log_or_print(msg, log_level) + end + + it "prints to STDOUT when logger is not defined" do + expect { base_api.log_or_print(msg, log_level) }.to output(/#{msg}(\n)?/).to_stdout + end + end end diff --git a/spec/lib/api/info_api_spec.rb b/spec/lib/api/info_api_spec.rb new file mode 100644 index 0000000..cf185ca --- /dev/null +++ b/spec/lib/api/info_api_spec.rb @@ -0,0 +1,19 @@ +require_relative '../../../lib/api/info_api.rb' + +describe InfoApi do + let(:info_api) { build(:info_api) } + let(:success_json) { {"info" => "info_and_stuff"} } + + before(:each) do + allow_any_instance_of(InfoApi).to receive(:verify_token).and_return(true) + allow_any_instance_of(InfoApi).to receive(:get_get_response).and_return(success_json) + end + + # Make sure this doesn't blow up, keeping in mind that + # that get_get_response is tested in the ApiHelpers spec + describe "#get_configuration" do + it "returns configuration JSON" do + expect(info_api.get_configuration).to eq(success_json) + end + end +end diff --git a/spec/lib/api/link_api_spec.rb b/spec/lib/api/link_api_spec.rb new file mode 100644 index 0000000..6f27642 --- /dev/null +++ b/spec/lib/api/link_api_spec.rb @@ -0,0 +1,19 @@ +require_relative '../../../lib/api/link_api.rb' + +describe LinkApi do + let(:link_api) { build(:link_api) } + let(:success_json) { {"shares" => 123456} } + let(:url) { "http%3A%2F%2Fjbegleiter.com" } + + before(:each) do + allow_any_instance_of(LinkApi).to receive(:get_get_response).and_return(success_json) + end + + # Make sure this doesn't blow up, keeping in mind that + # that get_get_response is tested in the ApiHelpers spec + describe "#get_shares" do + it "returns share JSON" do + expect(link_api.get_shares(url)).to eq(success_json) + end + end +end diff --git a/spec/lib/api/profile_api_spec.rb b/spec/lib/api/profile_api_spec.rb index 2f3b4e6..c3f2968 100644 --- a/spec/lib/api/profile_api_spec.rb +++ b/spec/lib/api/profile_api_spec.rb @@ -20,10 +20,13 @@ before(:each) do allow_any_instance_of(ProfileApi).to receive(:verify_token).and_return(true) - allow_any_instance_of(ProfileApi).to receive(:get_post_response).and_return(success_json) end describe "#update_schedule" do + before(:each) do + allow_any_instance_of(ProfileApi).to receive(:get_post_response).and_return(success_json) + end + it "raises an exception when the schedule is improperly defined" do expect { profile_api.update_schedule(id, bad_sched) }.to raise_error end @@ -32,4 +35,32 @@ expect(profile_api.update_schedule(id, good_sched)).to eq(success_json) end end + + # Make sure these don't blow up, keeping in mind that + # that get_get_response is tested in the ApiHelpers spec + describe "#gets" do + let(:id) { "123456789" } + + before(:each) do + allow_any_instance_of(ProfileApi).to receive(:get_get_response).and_return(success_json) + end + + describe "#get_profiles" do + it "doesn't blow up on invocation" do + expect(profile_api.get_profiles).to eq(success_json) + end + end + + describe "#get_profile" do + it "doesn't blow up on invocation" do + expect(profile_api.get_profile(id)).to eq(success_json) + end + end + + describe "#get_schedule" do + it "doesn't blow up on invocation" do + expect(profile_api.get_schedule(id)).to eq(success_json) + end + end + end end diff --git a/spec/lib/api/update_api_spec.rb b/spec/lib/api/update_api_spec.rb index b9f8d9b..0858a81 100644 --- a/spec/lib/api/update_api_spec.rb +++ b/spec/lib/api/update_api_spec.rb @@ -4,6 +4,7 @@ let(:update_api) { build(:update_api) } let(:id) { "123456789" } let(:path) { "some_path" } + let(:return_json) { { :key => "value" } } before(:each) do allow_any_instance_of(UpdateApi).to receive(:verify_token).and_return(true) @@ -22,4 +23,88 @@ expect(update_api.send(:build_update_url, id, path)).to include(expected_val) end end + + # Make sure these don't blow up, keeping in mind that + # that get_get_response is tested in the ApiHelpers spec + describe "#gets" do + before(:each) do + allow_any_instance_of(UpdateApi).to receive(:get_get_response).and_return(return_json) + end + + describe "#get_update" do + it "doesn't blow up when invoked" do + expect(update_api.get_update(id)).to eq(return_json) + end + end + + describe "#get_pending_updates" do + it "doesn't blow up when invoked" do + expect(update_api.get_pending_updates(id)).to eq(return_json) + end + end + + describe "#get_sent_updates" do + it "doesn't blow up when invoked" do + expect(update_api.get_sent_updates(id)).to eq(return_json) + end + end + + describe "#get_interactions" do + it "doesn't blow up when invoked" do + expect(update_api.get_interactions(id)).to eq(return_json) + end + end + end + + describe "#posts" do + before(:each) do + allow_any_instance_of(UpdateApi).to receive(:get_post_response).and_return(return_json) + end + + describe "#reorder_updates" do + let(:updates_array) { [ id, id, id ] } + + it "doesn't blow up when invoked" do + expect(update_api.reorder_updates(id, updates_array)).to eq(return_json) + end + end + + describe "#shuffle_updates" do + it "doesn't blow up when invoked" do + expect(update_api.shuffle_updates(id)).to eq(return_json) + end + end + + describe "#create_update" do + let(:profile_ids) { [ id, id, id ] } + + it "doesn't blow up when invoked" do + expect(update_api.create_update(profile_ids)).to eq(return_json) + end + end + + describe "#update_status" do + it "doesn't blow up when invoked" do + expect(update_api.update_status(id)).to eq(return_json) + end + end + + describe "#share_update" do + it "doesn't blow up when invoked" do + expect(update_api.share_update(id)).to eq(return_json) + end + end + + describe "#destroy_update" do + it "doesn't blow up when invoked" do + expect(update_api.destroy_update(id)).to eq(return_json) + end + end + + describe "#move_to_top" do + it "doesn't blow up when invoked" do + expect(update_api.move_to_top(id)).to eq(return_json) + end + end + end end diff --git a/spec/lib/api/user_api_spec.rb b/spec/lib/api/user_api_spec.rb new file mode 100644 index 0000000..fc3e7fa --- /dev/null +++ b/spec/lib/api/user_api_spec.rb @@ -0,0 +1,25 @@ +require_relative '../../../lib/api/user_api.rb' + +describe UserApi do + let(:user_api) { build(:user_api) } + let(:success_json) { {"key" => "value"} } + + before(:each) do + allow_any_instance_of(UserApi).to receive(:verify_token).and_return(true) + allow_any_instance_of(UserApi).to receive(:get_get_response).and_return(success_json) + end + + # Make sure these don't blow up, keeping in mind that + # that get_get_response is tested in the ApiHelpers spec + describe "#get_user_json" do + it "returns user JSON" do + expect(user_api.get_user_json).to eq(success_json) + end + end + + describe "#deauthorize" do + it "returns success JSON" do + expect(user_api.deauthorize).to eq(success_json) + end + end +end From 0f56f7a86e0e42717a4ca63f5cdb5a4a8b6e6bcc Mon Sep 17 00:00:00 2001 From: kaneda Date: Sat, 18 Jul 2015 22:25:33 -0400 Subject: [PATCH 3/5] Roll version --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index eaf8bb9..e09b890 100644 --- a/readme.md +++ b/readme.md @@ -30,7 +30,7 @@ See the Travis-CI build information above for build status on 1.9.3 and 2.1.2 Put this sucker in your Gemfile and bundle install ```ruby -gem "buffer-app", :git => "git://github.com/kaneda/buffer-ruby", :tag => "v1.1" +gem "buffer-app", :git => "git://github.com/kaneda/buffer-ruby", :tag => "v1.2" ``` Then drop this into your config/application.rb (or wherever you want to use it) From 50cbfce495e08b06b98b2726605aa78fd75fa021 Mon Sep 17 00:00:00 2001 From: kaneda Date: Sun, 19 Jul 2015 22:36:01 -0400 Subject: [PATCH 4/5] Updated readme --- readme.md | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index e09b890..1def464 100644 --- a/readme.md +++ b/readme.md @@ -6,6 +6,19 @@ Modelled after [Octokit](https://github.com/octokit/octokit.rb) <3, this is a li [![Test Coverage](https://codeclimate.com/github/kaneda/buffer-ruby/badges/coverage.svg)](https://codeclimate.com/github/kaneda/buffer-ruby/coverage) [![Build Status](https://travis-ci.org/kaneda/buffer-ruby.svg?branch=master)](https://travis-ci.org/kaneda/buffer-ruby) +## Table of Contents + +* [Ruby Versions](https://github.com/kaneda/buffer-ruby#ruby-versions) +* [Client API](https://github.com/kaneda/buffer-ruby#client-api) +** [What's Implemented](https://github.com/kaneda/buffer-ruby#whats-imlemented) +** [Installing](https://github.com/kaneda/buffer-ruby#installing) +** [Basic Usage](https://github.com/kaneda/buffer-ruby#basic-usage) +** [Available Calls](https://github.com/kaneda/buffer-ruby#available-calls) +** [Helper Methods](https://github.com/kaneda/buffer-ruby#helper-methods) +** [Defining a Schedule](https://github.com/kaneda/buffer-ruby#defining-a-schedule) +* [Contributing](https://github.com/kaneda/buffer-ruby#contributing) +* [Contact](https://github.com/kaneda/buffer-ruby#contact) + ## Ruby Versions This gem is built to work with Ruby 1.9.3+ @@ -56,6 +69,8 @@ auth_tok = buffer_client.get_auth_token buffer_client.configure({ :auth_token => auth_tok }) + +# Subsequent calls here (see below) ``` ### Available Calls @@ -84,7 +99,7 @@ buffer_client.configure({ | [get_shares](https://buffer.com/developers/api/links#shares) | Unencodded URL:String | Shares:Integer | You can pass a normal URL here, the client will encode it. This is one of the only calls to not require an auth_token | | [get_configuration](https://buffer.com/developers/api/info#configuration) | | Configuration:Hash | "services" key has internal keys for each service | -### Helper methods +### Helper Methods | Method | Description | | :---------: | :----- | | configure | Takes in a hash (as above) and reconfigures all API objects | @@ -93,7 +108,7 @@ buffer_client.configure({ | error | Returns the current error, leaving it in tact | -### Defining a schedule +### Defining a Schedule To update a schedule the BufferClient is expecting a schedule of the form: @@ -110,6 +125,7 @@ To update a schedule the BufferClient is expecting a schedule of the form: ``` Note that you can make the keys symbols or strings, the client will accept either one. + ## Contributing To contribute simply: @@ -120,7 +136,7 @@ To contribute simply: 4. Once approved [squash your commits](http://davidwalsh.name/squash-commits-git) 5. Party -## Contact +## Contact Me Email: kanedasan@gmail.com From 5b0513fd35a6d02a41cf9a1fdee98cc935b41198 Mon Sep 17 00:00:00 2001 From: kaneda Date: Sun, 19 Jul 2015 22:36:41 -0400 Subject: [PATCH 5/5] Updated readme --- readme.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 1def464..f0ecde3 100644 --- a/readme.md +++ b/readme.md @@ -10,12 +10,12 @@ Modelled after [Octokit](https://github.com/octokit/octokit.rb) <3, this is a li * [Ruby Versions](https://github.com/kaneda/buffer-ruby#ruby-versions) * [Client API](https://github.com/kaneda/buffer-ruby#client-api) -** [What's Implemented](https://github.com/kaneda/buffer-ruby#whats-imlemented) -** [Installing](https://github.com/kaneda/buffer-ruby#installing) -** [Basic Usage](https://github.com/kaneda/buffer-ruby#basic-usage) -** [Available Calls](https://github.com/kaneda/buffer-ruby#available-calls) -** [Helper Methods](https://github.com/kaneda/buffer-ruby#helper-methods) -** [Defining a Schedule](https://github.com/kaneda/buffer-ruby#defining-a-schedule) + * [What's Implemented](https://github.com/kaneda/buffer-ruby#whats-imlemented) + * [Installing](https://github.com/kaneda/buffer-ruby#installing) + * [Basic Usage](https://github.com/kaneda/buffer-ruby#basic-usage) + * [Available Calls](https://github.com/kaneda/buffer-ruby#available-calls) + * [Helper Methods](https://github.com/kaneda/buffer-ruby#helper-methods) + * [Defining a Schedule](https://github.com/kaneda/buffer-ruby#defining-a-schedule) * [Contributing](https://github.com/kaneda/buffer-ruby#contributing) * [Contact](https://github.com/kaneda/buffer-ruby#contact)