From 62fca30aa49f549213db027cce78219c54d3e1e2 Mon Sep 17 00:00:00 2001 From: Jikku Jose Date: Sun, 21 Jun 2015 13:26:03 +0530 Subject: [PATCH 1/2] Reorganize resources --- lib/ruby-stackoverflow/client/resources.rb | 13 +++++++++++++ .../client/{resource => resources}/answer.rb | 0 .../client/{resource => resources}/badge.rb | 0 .../client/{resource => resources}/comment.rb | 0 .../client/{resource => resources}/notification.rb | 0 .../client/{resource => resources}/permission.rb | 0 .../client/{resource => resources}/post.rb | 0 .../client/{resource => resources}/question.rb | 0 .../client/{resource => resources}/reputation.rb | 0 .../client/{resource => resources}/resource.rb | 0 .../{resource => resources}/stackoverflow_error.rb | 0 .../{resource => resources}/suggested_edit.rb | 0 .../client/{resource => resources}/tag.rb | 0 .../client/{resource => resources}/user.rb | 0 14 files changed, 13 insertions(+) create mode 100644 lib/ruby-stackoverflow/client/resources.rb rename lib/ruby-stackoverflow/client/{resource => resources}/answer.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/badge.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/comment.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/notification.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/permission.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/post.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/question.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/reputation.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/resource.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/stackoverflow_error.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/suggested_edit.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/tag.rb (100%) rename lib/ruby-stackoverflow/client/{resource => resources}/user.rb (100%) diff --git a/lib/ruby-stackoverflow/client/resources.rb b/lib/ruby-stackoverflow/client/resources.rb new file mode 100644 index 0000000..500506f --- /dev/null +++ b/lib/ruby-stackoverflow/client/resources.rb @@ -0,0 +1,13 @@ +require_relative 'resources/resource' +require_relative 'resources/user' +require_relative 'resources/question' +require_relative 'resources/answer' +require_relative 'resources/notification' +require_relative 'resources/badge' +require_relative 'resources/reputation' +require_relative 'resources/suggested_edit' +require_relative 'resources/comment' +require_relative 'resources/tag' +require_relative 'resources/post' +require_relative 'resources/permission' +require_relative 'resources/stackoverflow_error' diff --git a/lib/ruby-stackoverflow/client/resource/answer.rb b/lib/ruby-stackoverflow/client/resources/answer.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/answer.rb rename to lib/ruby-stackoverflow/client/resources/answer.rb diff --git a/lib/ruby-stackoverflow/client/resource/badge.rb b/lib/ruby-stackoverflow/client/resources/badge.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/badge.rb rename to lib/ruby-stackoverflow/client/resources/badge.rb diff --git a/lib/ruby-stackoverflow/client/resource/comment.rb b/lib/ruby-stackoverflow/client/resources/comment.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/comment.rb rename to lib/ruby-stackoverflow/client/resources/comment.rb diff --git a/lib/ruby-stackoverflow/client/resource/notification.rb b/lib/ruby-stackoverflow/client/resources/notification.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/notification.rb rename to lib/ruby-stackoverflow/client/resources/notification.rb diff --git a/lib/ruby-stackoverflow/client/resource/permission.rb b/lib/ruby-stackoverflow/client/resources/permission.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/permission.rb rename to lib/ruby-stackoverflow/client/resources/permission.rb diff --git a/lib/ruby-stackoverflow/client/resource/post.rb b/lib/ruby-stackoverflow/client/resources/post.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/post.rb rename to lib/ruby-stackoverflow/client/resources/post.rb diff --git a/lib/ruby-stackoverflow/client/resource/question.rb b/lib/ruby-stackoverflow/client/resources/question.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/question.rb rename to lib/ruby-stackoverflow/client/resources/question.rb diff --git a/lib/ruby-stackoverflow/client/resource/reputation.rb b/lib/ruby-stackoverflow/client/resources/reputation.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/reputation.rb rename to lib/ruby-stackoverflow/client/resources/reputation.rb diff --git a/lib/ruby-stackoverflow/client/resource/resource.rb b/lib/ruby-stackoverflow/client/resources/resource.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/resource.rb rename to lib/ruby-stackoverflow/client/resources/resource.rb diff --git a/lib/ruby-stackoverflow/client/resource/stackoverflow_error.rb b/lib/ruby-stackoverflow/client/resources/stackoverflow_error.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/stackoverflow_error.rb rename to lib/ruby-stackoverflow/client/resources/stackoverflow_error.rb diff --git a/lib/ruby-stackoverflow/client/resource/suggested_edit.rb b/lib/ruby-stackoverflow/client/resources/suggested_edit.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/suggested_edit.rb rename to lib/ruby-stackoverflow/client/resources/suggested_edit.rb diff --git a/lib/ruby-stackoverflow/client/resource/tag.rb b/lib/ruby-stackoverflow/client/resources/tag.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/tag.rb rename to lib/ruby-stackoverflow/client/resources/tag.rb diff --git a/lib/ruby-stackoverflow/client/resource/user.rb b/lib/ruby-stackoverflow/client/resources/user.rb similarity index 100% rename from lib/ruby-stackoverflow/client/resource/user.rb rename to lib/ruby-stackoverflow/client/resources/user.rb From d9fe52bf60349a247abbf0148f3c07ec91d3c6da Mon Sep 17 00:00:00 2001 From: Jikku Jose Date: Sun, 21 Jun 2015 13:26:52 +0530 Subject: [PATCH 2/2] Refactor based on rubocop guides --- lib/ruby-stackoverflow/client.rb | 57 ++++++++----------- .../client/parse_options.rb | 28 +++++---- lib/ruby-stackoverflow/client/user_helper.rb | 10 ++-- spec/ruby-stackoverflow/user_spec.rb | 1 + 4 files changed, 44 insertions(+), 52 deletions(-) diff --git a/lib/ruby-stackoverflow/client.rb b/lib/ruby-stackoverflow/client.rb index fc9538d..8f52376 100644 --- a/lib/ruby-stackoverflow/client.rb +++ b/lib/ruby-stackoverflow/client.rb @@ -1,36 +1,25 @@ require 'json' -require 'ruby-stackoverflow/client/response_data' -require 'ruby-stackoverflow/client/resource/resource' -require 'ruby-stackoverflow/client/resource/user' -require 'ruby-stackoverflow/client/resource/question' -require 'ruby-stackoverflow/client/resource/answer' -require 'ruby-stackoverflow/client/resource/notification' -require 'ruby-stackoverflow/client/resource/badge' -require 'ruby-stackoverflow/client/resource/reputation' -require 'ruby-stackoverflow/client/resource/suggested_edit' -require 'ruby-stackoverflow/client/resource/comment' -require 'ruby-stackoverflow/client/resource/tag' -require 'ruby-stackoverflow/client/resource/post' -require 'ruby-stackoverflow/client/resource/permission' -require 'ruby-stackoverflow/client/resource/stackoverflow_error' -require 'ruby-stackoverflow/client/user_helper' -require 'ruby-stackoverflow/client/question_helper' -require 'ruby-stackoverflow/client/badges_helper' -require 'ruby-stackoverflow/client/comments_helper' -require 'ruby-stackoverflow/client/parse_options' + +require_relative 'client/response_data' +require_relative 'client/resources' +require_relative 'client/user_helper' +require_relative 'client/question_helper' +require_relative 'client/badges_helper' +require_relative 'client/comments_helper' +require_relative 'client/parse_options' module RubyStackoverflow class Client - include RubyStackoverflow::Client::ParseOptions - include RubyStackoverflow::Client::UserHelper - include RubyStackoverflow::Client::QuestionHelper - include RubyStackoverflow::Client::BadgesHelper - include RubyStackoverflow::Client::CommentsHelper + include ParseOptions + include UserHelper + include QuestionHelper + include BadgesHelper + include CommentsHelper attr_accessor :configuration - def getr(url,klass, options={}) - request :get, url,klass ,options + def getr(url, klass, options = {}) + request :get, url, klass, options end def configure @@ -39,9 +28,9 @@ def configure private - def request(method, url, klass, options={}) + def request(method, url, klass, options = {}) url = append_params_to_url(url, parse_options(options)) - response = HTTParty.send(method,url) + response = HTTParty.send(method, url) parse_response(response, klass) end @@ -53,16 +42,20 @@ def parse_response(data, klass) def append_params_to_url(url, options) url = Configuration.api_url + url options.merge!(key_params) - options = options.to_a.map{|k,v|"#{k}=#{v}"} - url+'?'+options.join('&') + options = options.to_a.map { |k, v| "#{k}=#{v}" } + url + '?' + options.join('&') end def key_params - {key: configuration.client_key, site: 'stackoverflow', access_token: configuration.access_token} + { + key: configuration.client_key, + site: 'stackoverflow', + access_token: configuration.access_token, + } end def configuration - @configuration||= Configuration.new + @configuration ||= Configuration.new end end end diff --git a/lib/ruby-stackoverflow/client/parse_options.rb b/lib/ruby-stackoverflow/client/parse_options.rb index 017e1ea..6892082 100644 --- a/lib/ruby-stackoverflow/client/parse_options.rb +++ b/lib/ruby-stackoverflow/client/parse_options.rb @@ -1,24 +1,22 @@ module RubyStackoverflow - class Client - module ParseOptions - def parse_options(options = {}) - options.each do|k,v| - case k - when :fromdate , :todate, :min, :max - begin - options[k] = Time.parse(v).to_i - rescue - options[k] = v - end - else + module ParseOptions + def parse_options(options = {}) + options.each do|k,v| + case k + when :fromdate , :todate, :min, :max + begin + options[k] = Time.parse(v).to_i + rescue options[k] = v end + else + options[k] = v end end + end - def join_ids(ids) - ids.join(';') - end + def join_ids(ids) + ids.join(';') end end end diff --git a/lib/ruby-stackoverflow/client/user_helper.rb b/lib/ruby-stackoverflow/client/user_helper.rb index 4ac8c4e..9a669ce 100644 --- a/lib/ruby-stackoverflow/client/user_helper.rb +++ b/lib/ruby-stackoverflow/client/user_helper.rb @@ -114,17 +114,17 @@ def users_top_questions_with_given_tags(id, tags, options = {}) user_response(options, url) end - def user_top_tags(id, options ={}) + def user_top_tags(id, options = {}) url = id + '/top-tags' user_response(options, url) end - def user_top_tags_by_answers(id, options ={}) + def user_top_tags_by_answers(id, options = {}) url = id + '/top-answer-tags' user_response(options, url) end - def user_top_tags_by_questions(id, options ={}) + def user_top_tags_by_questions(id, options = {}) url = id + '/top-question-tags' user_response(options, url) end @@ -147,8 +147,8 @@ def user_full_reputation_history(id, options = {}) private - def user_response(options={}, url='') - url = 'users/'+ url + def user_response(options = {}, url = '') + url = 'users/' + url getr(url, 'user', options) end end diff --git a/spec/ruby-stackoverflow/user_spec.rb b/spec/ruby-stackoverflow/user_spec.rb index 81d1f0c..e6a6c3f 100644 --- a/spec/ruby-stackoverflow/user_spec.rb +++ b/spec/ruby-stackoverflow/user_spec.rb @@ -1,4 +1,5 @@ require 'spec_helper' + module RubyStackoverflow describe Client::UserHelper do before(:each) do