From 023ab15a8eeb61ddf163f122c101314f8287ff92 Mon Sep 17 00:00:00 2001 From: thesecretmaster Date: Wed, 5 Sep 2018 15:30:25 -0400 Subject: [PATCH] Add tags --- .gitignore | 3 +++ lib/se/api.rb | 15 ++++++++++++--- lib/se/api/types/tag.rb | 14 ++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 lib/se/api/types/tag.rb diff --git a/.gitignore b/.gitignore index 0cb6eeb..caf7ab8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,6 @@ /pkg/ /spec/reports/ /tmp/ + +*.log +*.gem diff --git a/lib/se/api.rb b/lib/se/api.rb index 2ffedb9..ed20d74 100644 --- a/lib/se/api.rb +++ b/lib/se/api.rb @@ -4,6 +4,7 @@ require "se/api/types/comment" require "se/api/types/user" require "se/api/types/post" +require "se/api/types/tag" require "net/http" require "json" @@ -73,12 +74,20 @@ def user(id, **params) users(id, **params).first end + def tags(*names, query_type: 'info', **params) + objectify Tag, names, **params.merge({uri_suffix: query_type, delimiter: ','}) + end + + def tag(name, **params) + tags(name, **params).first + end + private - def objectify(type, ids = "", uri: nil, **params) + def objectify(type, ids = "", uri_prefix: nil, uri_suffix: nil, uri: nil, delimiter: ';', **params) return if ids == "" - uri = "#{type.to_s.split('::').last.downcase}s" if uri.nil? - json("#{uri}/#{Array(ids).join(';')}", **params).map do |i| + uri_prefix = "#{type.to_s.split('::').last.downcase}s" if uri_prefix.nil? + json([uri_prefix, Array(ids).join(delimiter), uri_suffix].reject(&:nil?).join('/'), **params).map do |i| type.new(i) end end diff --git a/lib/se/api/types/tag.rb b/lib/se/api/types/tag.rb new file mode 100644 index 0000000..3d29446 --- /dev/null +++ b/lib/se/api/types/tag.rb @@ -0,0 +1,14 @@ +module SE + module API + class Tag + attr_reader :json, :count, :synonyms, :name + + def initialize(item_json) + @json = Hash(item_json) + @count = @json['count'] + @synonyms = Array(@json['synonyms']) + @name = @json['name'] + end + end + end +end