From a3df6775777c58248f9bfe0436c30f93a8ba3694 Mon Sep 17 00:00:00 2001 From: kaneda Date: Sat, 18 Jul 2015 18:08:38 -0400 Subject: [PATCH 1/4] [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 2/4] 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 3/4] 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 4/4] 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)