diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml
index 3cbd02c..cff7a5a 100644
--- a/.rubocop_todo.yml
+++ b/.rubocop_todo.yml
@@ -1,6 +1,6 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
-# on 2019-11-10 19:31:50 +0300 using RuboCop version 0.76.0.
+# on 2019-11-10 21:16:27 +0300 using RuboCop version 0.76.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
@@ -8,9 +8,9 @@
# Offense count: 2
Metrics/AbcSize:
- Max: 27
+ Max: 25
-# Offense count: 4
+# Offense count: 3
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/MethodLength:
Max: 23
diff --git a/instagram_tags_searcher.gemspec b/instagram_tags_searcher.gemspec
index 1fd08dc..436a95a 100644
--- a/instagram_tags_searcher.gemspec
+++ b/instagram_tags_searcher.gemspec
@@ -3,7 +3,7 @@
Gem::Specification.new do |s|
s.name = 'instagram_tags_searcher'
s.summary = 'Searches for Instagram tags related to the given tag'
- s.version = '0.0.3'
+ s.version = '0.0.4'
s.authors = ['Andrey Kurashev']
s.files = ['lib/instagram_tags_searcher.rb']
diff --git a/lib/instagram_tags_searcher.rb b/lib/instagram_tags_searcher.rb
index 3ab85e6..cf0e2a9 100644
--- a/lib/instagram_tags_searcher.rb
+++ b/lib/instagram_tags_searcher.rb
@@ -1,8 +1,6 @@
# frozen_string_literal: true
-require 'cgi'
-
-require 'instagram_tags_searcher/http_client'
+require 'instagram_tags_searcher/instagram_client'
module InstagramTagsSearcher
def self.search(tag)
@@ -39,10 +37,7 @@ def self.search(tag)
end
def self.from_top(tag)
- tag = CGI.escape(tag)
- data = HttpClient.new.read_hash(
- "https://www.instagram.com/explore/tags/#{tag}/?__a=1"
- )
+ data = InstagramClient.new.top(tag)
moretags = []
codes = []
@@ -68,9 +63,7 @@ def self.from_top(tag)
end
def self.from_first_comment(code)
- data = HttpClient.new.read_hash(
- "https://www.instagram.com/p/#{code}/?__a=1"
- )
+ data = InstagramClient.new.post(code)
comments = data['graphql']['shortcode_media']['edge_media_to_parent_comment']
comments_count = comments['count']
@@ -109,9 +102,7 @@ def self.sort_by_frequency(tags)
end
def self.posts_count(tag)
- tag_name = CGI.escape(tag[1..-1])
- url = "https://www.instagram.com/explore/tags/#{tag_name}/?__a=1"
- data = HttpClient.new.read_hash(url)
+ data = InstagramClient.new.top(tag)
posts = data['graphql']['hashtag']['edge_hashtag_to_media']
posts['count'].to_i
diff --git a/lib/instagram_tags_searcher/instagram_client.rb b/lib/instagram_tags_searcher/instagram_client.rb
new file mode 100644
index 0000000..b8183e4
--- /dev/null
+++ b/lib/instagram_tags_searcher/instagram_client.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+require 'cgi'
+
+require 'instagram_tags_searcher/http_client'
+
+module InstagramTagsSearcher
+ # This class is responsible for making requests to Instagram
+ # It knows which URL should be used and which params should be passed
+ class InstagramClient
+ URL_TOP = 'https://www.instagram.com/explore/tags/%s/?__a=1'
+ URL_POST = 'https://www.instagram.com/p/%s/?__a=1'
+
+ def initialize(http_client = HttpClient.new)
+ @http_client = http_client
+ end
+
+ def top(tag)
+ @param = sanitize_tag(tag)
+ @url_template = URL_TOP
+ make_request
+ end
+
+ def post(code)
+ @param = code
+ @url_template = URL_POST
+ make_request
+ end
+
+ private
+
+ attr_reader :http_client, :url_template, :param
+
+ def make_request
+ http_client.read_hash(url)
+ end
+
+ def url
+ format(url_template, param: param)
+ end
+
+ def sanitize_tag(tag)
+ tag = tag[1..-1] if tag.start_with?('#')
+ CGI.escape(tag)
+ end
+ end
+end