From 64cbd7b82f5de9c28551788e60d7663d36161a3a Mon Sep 17 00:00:00 2001 From: Chris Fritz Date: Mon, 17 Nov 2014 12:14:03 -0500 Subject: [PATCH 1/3] added search resource with single helper to find similar questions --- lib/ruby-stackoverflow/client.rb | 3 +++ .../client/resource/question.rb | 10 +++++----- .../client/resource/search.rb | 6 ++++++ lib/ruby-stackoverflow/client/search_helper.rb | 17 +++++++++++++++++ lib/ruby-stackoverflow/client/user_helper.rb | 2 +- 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 lib/ruby-stackoverflow/client/resource/search.rb create mode 100644 lib/ruby-stackoverflow/client/search_helper.rb diff --git a/lib/ruby-stackoverflow/client.rb b/lib/ruby-stackoverflow/client.rb index fc9538d..c6ba2f3 100644 --- a/lib/ruby-stackoverflow/client.rb +++ b/lib/ruby-stackoverflow/client.rb @@ -13,10 +13,12 @@ require 'ruby-stackoverflow/client/resource/post' require 'ruby-stackoverflow/client/resource/permission' require 'ruby-stackoverflow/client/resource/stackoverflow_error' +require 'ruby-stackoverflow/client/resource/search' 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/search_helper' require 'ruby-stackoverflow/client/parse_options' module RubyStackoverflow @@ -26,6 +28,7 @@ class Client include RubyStackoverflow::Client::QuestionHelper include RubyStackoverflow::Client::BadgesHelper include RubyStackoverflow::Client::CommentsHelper + include RubyStackoverflow::Client::SearchHelper attr_accessor :configuration diff --git a/lib/ruby-stackoverflow/client/resource/question.rb b/lib/ruby-stackoverflow/client/resource/question.rb index 0b6b6c5..e9fd195 100644 --- a/lib/ruby-stackoverflow/client/resource/question.rb +++ b/lib/ruby-stackoverflow/client/resource/question.rb @@ -31,15 +31,15 @@ def parse_data(data) def data_has_answer?(data) data.first.include?(:answer_id) - end + end def data_has_comment?(data) data.first.include?(:comment_id) && !data.first.include?(:timeline_type) - end + end def data_has_timeline?(data) data.first.include?(:timeline_type) - end + end def find_or_create_question(questions, qid) question_array = questions.select{|q|q.question_id == qid} @@ -47,9 +47,9 @@ def find_or_create_question(questions, qid) end - def create_question(attr_hash, questions, hash_key) + def create_question(attr_hash, questions, hash_key) qid = attr_hash.delete(hash_key) - question = find_or_create_question(questions, qid) + question = find_or_create_question(questions, qid) questions << question unless question_exists?(questions,qid) question end diff --git a/lib/ruby-stackoverflow/client/resource/search.rb b/lib/ruby-stackoverflow/client/resource/search.rb new file mode 100644 index 0000000..a65c685 --- /dev/null +++ b/lib/ruby-stackoverflow/client/resource/search.rb @@ -0,0 +1,6 @@ +module RubyStackoverflow + class Client + class Search < Resource + end + end +end \ No newline at end of file diff --git a/lib/ruby-stackoverflow/client/search_helper.rb b/lib/ruby-stackoverflow/client/search_helper.rb new file mode 100644 index 0000000..3127cea --- /dev/null +++ b/lib/ruby-stackoverflow/client/search_helper.rb @@ -0,0 +1,17 @@ +module RubyStackoverflow + class Client + module SearchHelper + + def similar(title, options={}) + similar_response(title, options) + end + + private + + def similar_response(title, options={}) + getr 'similar', 'question', options.merge(title: title) + end + + end + end +end \ No newline at end of file diff --git a/lib/ruby-stackoverflow/client/user_helper.rb b/lib/ruby-stackoverflow/client/user_helper.rb index e7ad5f6..8801ea3 100644 --- a/lib/ruby-stackoverflow/client/user_helper.rb +++ b/lib/ruby-stackoverflow/client/user_helper.rb @@ -43,7 +43,7 @@ def users_unread_notifications(id, options = {}) url = id + '/notifications/unread' user_response(options, url) end - + def users_with_favorites_questions(ids, options = {}) ids = join_ids(ids) url = ids + '/favorites' From e38d57490c63ff2c7f48a8bb22ac4efccee3e885 Mon Sep 17 00:00:00 2001 From: Chris Fritz Date: Mon, 17 Nov 2014 12:55:05 -0500 Subject: [PATCH 2/3] improved url component parsing so that strings with spaces did not produce an invalid url error --- lib/ruby-stackoverflow/client.rb | 5 +++-- lib/ruby-stackoverflow/client/parse_options.rb | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/ruby-stackoverflow/client.rb b/lib/ruby-stackoverflow/client.rb index c6ba2f3..d4b3637 100644 --- a/lib/ruby-stackoverflow/client.rb +++ b/lib/ruby-stackoverflow/client.rb @@ -1,4 +1,5 @@ require 'json' +require 'uri' require 'ruby-stackoverflow/client/response_data' require 'ruby-stackoverflow/client/resource/resource' require 'ruby-stackoverflow/client/resource/user' @@ -56,8 +57,8 @@ 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 = URI.encode_www_form(options) + url+'?'+options end def key_params diff --git a/lib/ruby-stackoverflow/client/parse_options.rb b/lib/ruby-stackoverflow/client/parse_options.rb index 6ee61cf..0b6ae99 100644 --- a/lib/ruby-stackoverflow/client/parse_options.rb +++ b/lib/ruby-stackoverflow/client/parse_options.rb @@ -14,7 +14,7 @@ def parse_options(options = {}) else options[k] = v end - end + end end def join_ids(ids) From 4ce9bf7776b71dea344bb2c79f213ce466266422 Mon Sep 17 00:00:00 2001 From: Chris Fritz Date: Mon, 17 Nov 2014 15:14:19 -0500 Subject: [PATCH 3/3] added /tags query --- lib/ruby-stackoverflow/client.rb | 2 ++ lib/ruby-stackoverflow/client/resource/tag.rb | 11 ----------- lib/ruby-stackoverflow/client/tag_helper.rb | 17 +++++++++++++++++ ruby-stackoverflow.gemspec | 2 +- 4 files changed, 20 insertions(+), 12 deletions(-) create mode 100644 lib/ruby-stackoverflow/client/tag_helper.rb diff --git a/lib/ruby-stackoverflow/client.rb b/lib/ruby-stackoverflow/client.rb index d4b3637..3cd6b6b 100644 --- a/lib/ruby-stackoverflow/client.rb +++ b/lib/ruby-stackoverflow/client.rb @@ -20,6 +20,7 @@ require 'ruby-stackoverflow/client/badges_helper' require 'ruby-stackoverflow/client/comments_helper' require 'ruby-stackoverflow/client/search_helper' +require 'ruby-stackoverflow/client/tag_helper' require 'ruby-stackoverflow/client/parse_options' module RubyStackoverflow @@ -30,6 +31,7 @@ class Client include RubyStackoverflow::Client::BadgesHelper include RubyStackoverflow::Client::CommentsHelper include RubyStackoverflow::Client::SearchHelper + include RubyStackoverflow::Client::TagHelper attr_accessor :configuration diff --git a/lib/ruby-stackoverflow/client/resource/tag.rb b/lib/ruby-stackoverflow/client/resource/tag.rb index 383bd5d..33a2d20 100644 --- a/lib/ruby-stackoverflow/client/resource/tag.rb +++ b/lib/ruby-stackoverflow/client/resource/tag.rb @@ -1,17 +1,6 @@ module RubyStackoverflow class Client class Tag < Resource - #def initialize(attributes_hash) - #Tag.define_atribute_methods(attributes_hash) - #end - - #class << self - #def define_atribute_methods(attributes_hash) - #attributes_hash.each do|k,v| - #define_method(k) do v; end - #end - #end - #end end end end diff --git a/lib/ruby-stackoverflow/client/tag_helper.rb b/lib/ruby-stackoverflow/client/tag_helper.rb new file mode 100644 index 0000000..204b62a --- /dev/null +++ b/lib/ruby-stackoverflow/client/tag_helper.rb @@ -0,0 +1,17 @@ +module RubyStackoverflow + class Client + module TagHelper + + def tags(options={}) + tags_response(options) + end + + private + + def tags_response(options={}) + getr 'tags/', 'tag', options + end + + end + end +end \ No newline at end of file diff --git a/ruby-stackoverflow.gemspec b/ruby-stackoverflow.gemspec index d52bcc1..a5cad20 100644 --- a/ruby-stackoverflow.gemspec +++ b/ruby-stackoverflow.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.add_runtime_dependency "httparty", "~> 0.11.0" + spec.add_runtime_dependency "httparty", "~> 0.13.3" spec.add_runtime_dependency "json" spec.add_development_dependency "rspec", "~> 2.1"