Skip to content

Commit

Permalink
Added user type and general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahzs committed Jan 15, 2018
1 parent 59e619e commit f4d7fa5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 23 deletions.
62 changes: 42 additions & 20 deletions lib/se/api.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "se/api/version"
require "se/api/types/post"
require "se/api/types/answer"
require "se/api/types/question"
require "se/api/types/comment"
require "se/api/types/user"
require "se/api/types/post"

require "net/http"
require "json"
Expand All @@ -28,35 +29,56 @@ def initialize(key = "", **params)
@logger_json = Logger.new 'api_json.log'
end

def posts(ids = "", **params)
return if ids == ""
json("posts/#{Array(ids).join(';')}", **params).map do |i|
Post.new(i)
end
def posts(*ids, **params)
objectify Post, ids, **params
end

def questions(ids = "", **params)
return if ids == ""
json("questions/#{Array(ids).join(';')}", **params).map do |i|
Question.new(i)
end
def post(id, **params)
posts(id, **params).first
end

def answers(ids = "", **params)
return if ids == ""
json("answers/#{Array(ids).join(';')}", **params).map do |i|
Answer.new(i)
end
def questions(*ids, **params)
objectify Question, ids, **params
end

def comments(ids = "", **params)
json("comments/#{Array(ids).join(';')}", **params).map do |i|
Comment.new(i)
end
def question(id, **params)
questions(id, **params).first
end

def answers(*ids, **params)
objectify Answer, ids, **params
end

def answer(id, **params)
answers(id, **params).first
end

def comments(*ids, **params)
objectify Comment, ids, **params
end

def comment(id, **params)
comments(id, **params).first
end

def users(*ids, **params)
objectify User, ids, **params
end

def user(id, **params)
users(id, **params).first
end

private

def objectify(type, ids = "", uri: nil, **params)
return if ids == ""
uri = "#{type.to_s.split('::').last.downcase}s" if uri.nil?
json("#{uri}/#{Array(ids).join(';')}", **params).map do |i|
type.new(i)
end
end

def json(uri, **params)
params = @params.merge(params)
throw "No site specified" if params[:site].nil?
Expand Down
2 changes: 2 additions & 0 deletions lib/se/api/types/answer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "se/api/types/post"

module SE
module API
class Answer < Post
Expand Down
6 changes: 5 additions & 1 deletion lib/se/api/types/comment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "se/api/types/user"

module SE
module API
class Comment
Expand All @@ -13,8 +15,10 @@ def initialize(item_json)
@type = item_json["post_type"]
@id = item_json["comment_id"].to_i
@created_at = item_json["creation_date"]
#@author = User.new(item_json["owner"])
@author = User.new(item_json["owner"])
end

alias_method :user, :author
end
end
end
8 changes: 6 additions & 2 deletions lib/se/api/types/post.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "se/api/types/user"

module SE
module API
class Post
Expand All @@ -14,9 +16,11 @@ def initialize(item_json)
@id = (@json["post_id"] || @json["answer_id"] || @json["question_id"]).to_i
@updated_at = @json["last_activity_date"]
@created_at = @json["creation_date"]
@last_activity_date = @updated_at
# @author = User.new(item_json["author"])
@author = User.new(@json["owner"])
end

alias_method :last_activity_date, :updated_at
alias_method :user, :author
end
end
end
2 changes: 2 additions & 0 deletions lib/se/api/types/question.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "se/api/types/post"

module SE
module API
class Question < Post
Expand Down
19 changes: 19 additions & 0 deletions lib/se/api/types/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module SE
module API
class User
attr_reader :json, :accept_rate, :badge_counts, :name, :link, :image, :reputation, :id, :type

def initialize(item_json)
@json = item_json
@accept_rate = @json["accept_rate"]
@badge_counts = @json["badge_counts"]
@name = @json["display_name"]
@link = @json["link"]
@image = @json["profile_image"]
@reputation = @json["reputation"]
@id = @json["user_id"]
@type = @json["user_type"]
end
end
end
end

0 comments on commit f4d7fa5

Please sign in to comment.