Skip to content

Added support for the queries of similar questions (categorized as a "search" method to mirror the organization of the API docs) #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/ruby-stackoverflow/client.rb
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-stackoverflow/client/parse_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def parse_options(options = {})
else
options[k] = v
end
end
end
end

def join_ids(ids)
Expand Down
10 changes: 5 additions & 5 deletions lib/ruby-stackoverflow/client/resource/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,25 @@ 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}
!question_array.empty? ? question_array.first : new({question_id: 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
Expand Down
6 changes: 6 additions & 0 deletions lib/ruby-stackoverflow/client/resource/search.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module RubyStackoverflow
class Client
class Search < Resource
end
end
end
11 changes: 0 additions & 11 deletions lib/ruby-stackoverflow/client/resource/tag.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 17 additions & 0 deletions lib/ruby-stackoverflow/client/search_helper.rb
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions lib/ruby-stackoverflow/client/tag_helper.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/ruby-stackoverflow/client/user_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion ruby-stackoverflow.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down