Skip to content

Commit

Permalink
Added question and answer types
Browse files Browse the repository at this point in the history
  • Loading branch information
isaiahzs committed Dec 14, 2017
1 parent 288c643 commit d1f4227
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/se/api.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require "se/api/version"
require "se/api/types/post"
require "se/api/types/answer"
require "se/api/types/question"

require "net/http"
require "json"
Expand Down Expand Up @@ -30,6 +32,20 @@ def posts(ids = "", **params)
end
end

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

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

private

def json(uri, **params)
Expand Down
6 changes: 6 additions & 0 deletions lib/se/api/types/answer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module SE
module API
class Answer < Post
end
end
end
6 changes: 4 additions & 2 deletions lib/se/api/types/post.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module SE
module API
class Post
attr_reader :body, :title, :link, :author, :score, :type
attr_reader :body, :title, :link, :author, :score, :type, :id, :last_activity_date
attr_reader :json

def initialize(item_json)
@json = item_json
@body = item_json["body"]
@title = item_json["title"]
@link = item_json["link"]
@score = item_json["score"]
@score = item_json["score"].to_i
@type = item_json["post_type"]
@id = item_json["id"].to_i
@last_activity_date = item_json["last_activity_date"]
# @author = User.new(item_json["author"])
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/se/api/types/question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module SE
module API
class Question < Post
attr_reader :answers

def initialize(item_json)
super(item_json)
@answers = item_json["answers"].map { |i| Answer.new(i) }
end
end
end
end

0 comments on commit d1f4227

Please sign in to comment.