Skip to content

Refactoring for code improvement #21

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 2 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
57 changes: 25 additions & 32 deletions lib/ruby-stackoverflow/client.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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('&')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end

def key_params
{key: configuration.client_key, site: 'stackoverflow', access_token: configuration.access_token}
{
key: configuration.client_key,
site: 'stackoverflow',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

access_token: configuration.access_token,
}
end

def configuration
@configuration||= Configuration.new
@configuration ||= Configuration.new
end
end
end
28 changes: 13 additions & 15 deletions lib/ruby-stackoverflow/client/parse_options.rb
Original file line number Diff line number Diff line change
@@ -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|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space missing after comma.

case k
when :fromdate , :todate, :min, :max
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Space found before comma.

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(';')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

end
end
end
13 changes: 13 additions & 0 deletions lib/ruby-stackoverflow/client/resources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require_relative 'resources/resource'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/user'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/question'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/answer'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/notification'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/badge'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/reputation'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/suggested_edit'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/comment'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/tag'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/post'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/permission'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

require_relative 'resources/stackoverflow_error'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

10 changes: 5 additions & 5 deletions lib/ruby-stackoverflow/client/user_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = '')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

url = 'users/' + url
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary spacing detected.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

getr(url, 'user', options)
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/ruby-stackoverflow/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper'

module RubyStackoverflow
describe Client::UserHelper do
before(:each) do
Expand Down