diff --git a/lib/ruby-stackoverflow/client.rb b/lib/ruby-stackoverflow/client.rb index fc9538d..3cd6b6b 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' @@ -13,10 +14,13 @@ 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/tag_helper' require 'ruby-stackoverflow/client/parse_options' module RubyStackoverflow @@ -26,6 +30,8 @@ class Client include RubyStackoverflow::Client::QuestionHelper include RubyStackoverflow::Client::BadgesHelper include RubyStackoverflow::Client::CommentsHelper + include RubyStackoverflow::Client::SearchHelper + include RubyStackoverflow::Client::TagHelper attr_accessor :configuration @@ -53,8 +59,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) 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/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/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/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/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' 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"