Class: WordstreamClient::KeywordTool

Inherits:
Object
  • Object
show all
Defined in:
lib/wordstream_client/keyword_tool.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, keywords = []) ⇒ KeywordTool

Description

Initializing a KeywordTool object for the keywordtool API calls. It is more convenient to set the username and password for Wordstream::Config and use KeywordTool class methods for API calls.

Params

config

Required - WordstreamClient::Config object with valid username and passowrd

keywords

Optional - Defaults to an empty array. Can be an array of keywords or a string of keywords separated by newline “n”



24
25
26
27
28
29
30
31
# File 'lib/wordstream_client/keyword_tool.rb', line 24

def initialize(config, keywords = [])
  @config   = config
  @keywords = keywords.is_a?(Array) ? keywords.join("\n") : keywords.to_s
  # Apache Web Server URL limit is 4000 characters
  # The API is horrible and passes ALL the keywords
  # In the URL string
  @keywords = @keywords[0..3949]
end

Instance Attribute Details

#keywordsObject

Returns the value of attribute keywords.



5
6
7
# File 'lib/wordstream_client/keyword_tool.rb', line 5

def keywords
  @keywords
end

Class Method Details

.get_niches(keywords, max = 2500, block_adult = 'false') ⇒ Object



75
76
77
# File 'lib/wordstream_client/keyword_tool.rb', line 75

def self.get_niches(keywords, max = 2500, block_adult = 'false')
  Config.client.keyword_tool(keywords).get_niches(max, block_adult)
end

.get_questions(keywords, max = 100000, block_adult = 'false') ⇒ Object



115
116
117
# File 'lib/wordstream_client/keyword_tool.rb', line 115

def self.get_questions(keywords, max = 100000, block_adult = 'false')
  Config.client.keyword_tool(keywords).get_questions(max, block_adult)
end


134
135
136
# File 'lib/wordstream_client/keyword_tool.rb', line 134

def self.get_related(keywords, max = 100, block_adult = 'false')
  Config.client.keyword_tool(keywords).get_related(max, block_adult)
end

.get_suggestions(keywords, max = 100000, block_adult = 'false') ⇒ Object



96
97
98
# File 'lib/wordstream_client/keyword_tool.rb', line 96

def self.get_suggestions(keywords, max = 100000, block_adult = 'false')
  Config.client.keyword_tool(keywords).get_suggestions(max, block_adult)
end

.get_volumes(keywords, block_adult = 'false') ⇒ Array

Description

get_keyword_volumes Wordstream API call. Takes list of keywords and returns an array of arrays of keywords and their volumes.

Params

keywords

Required - Array of keywords or string of keywords separated by newline “n”

block_adult

Optional - Defaults to ‘false’. Set to ‘true’ if you want to block adult results.

Returns:

  • (Array)

    Array of arrays of keywords and their volumes.

Raises:



50
51
52
# File 'lib/wordstream_client/keyword_tool.rb', line 50

def self.get_volumes(keywords, block_adult = 'false')
  Config.client.keyword_tool(keywords).get_volumes(block_adult)
end

Instance Method Details

#get_niches(max = 2500, block_adult = 'false') ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wordstream_client/keyword_tool.rb', line 79

def get_niches(max = 2500, block_adult = 'false')
  max          = max.abs > 2500 ? 2500 : max.abs
  path         = '/keywordtool/get_keyword_niches'
  url_keywords = URI.encode( @keywords, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") )
  query        = "?session_id=#{@config.session_id}&seeds=#{url_keywords}&block_adult=#{block_adult}&max_niches=#{max}"
  resp         = RestClient.get( @config.default_host + path + query )
  data         = JSON.parse resp.body

  # TODO: Handle Error Code
  # "{\"code\": \"ERROR\", \"detail\": \"No keywords provided!!!\"}"
  raise KeywordToolError.new('get_niches', data['detail']) if data['code'].match(/error/i)

  return data['data']
rescue JSON::ParserError => e
  raise KeywordToolError.new('get_niches', 'Bad response from Wordstream when trying to get keyword niches.')
end

#get_questions(max = 100000, block_adult = 'false') ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/wordstream_client/keyword_tool.rb', line 119

def get_questions(max = 100000, block_adult = 'false')
  max          = max.abs > 100000 ? 100000 : max.abs
  path         = '/keywordtool/get_question_keywords'
  url_keywords = URI.encode( @keywords[0..19], Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") )
  query        = "?session_id=#{@config.session_id}&seeds=#{url_keywords}&block_adult=#{block_adult}&max_niches=#{max}"
  resp         = RestClient.get( @config.default_host + path + query )
  data         = JSON.parse resp.body

  raise KeywordToolError.new('get_questions', data['detail']) if data['code'].match(/error/i)

  return data['data']
rescue JSON::ParserError => e
  raise KeywordToolError.new('get_questions', 'Bad response from Wordstream when trying to get keyword questions.')
end


138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/wordstream_client/keyword_tool.rb', line 138

def get_related(max = 100, block_adult = 'false')
  max          = max.abs > 100 ? 100 : max.abs
  path         = '/keywordtool/get_related_keywords'
  url_keywords = URI.encode( @keywords[0..19], Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") )
  query        = "?session_id=#{@config.session_id}&seeds=#{url_keywords}&block_adult=#{block_adult}&max_niches=#{max}"
  resp         = RestClient.get( @config.default_host + path + query )
  data         = JSON.parse resp.body

  raise KeywordToolError.new('get_related', data['detail']) if data['code'].match(/error/i)

  return data['data']
rescue JSON::ParserError => e
  raise KeywordToolError.new('get_related', 'Bad response from Wordstream when trying to get related keyword.')
end

#get_suggestions(max = 100000, block_adult = 'false') ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/wordstream_client/keyword_tool.rb', line 100

def get_suggestions(max = 100000, block_adult = 'false')
  max          = max.abs > 100000 ? 100000 : max.abs
  path         = '/keywordtool/get_keywords'
  url_keywords = URI.encode( @keywords[0..19], Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") )
  query        = "?session_id=#{@config.session_id}&seeds=#{url_keywords}&block_adult=#{block_adult}&max_niches=#{max}"
  resp         = RestClient.get( @config.default_host + path + query )
  data         = JSON.parse resp.body

  raise KeywordToolError.new('get_suggestions', data['detail']) if data['code'].match(/error/i)

  return data['data']
rescue JSON::ParserError => e
  raise KeywordToolError.new('get_suggestions', 'Bad response from Wordstream when trying to get keyword suggestions.')
end

#get_volumes(block_adult = 'false') ⇒ Object

Description

get_keyword_volumes Wordstream API call. Takes list of keywords and returns an array of arrays of keywords and their volumes. See class method get_volumes.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/wordstream_client/keyword_tool.rb', line 61

def get_volumes(block_adult = 'false')
  path         = '/keywordtool/get_keyword_volumes'
  url_keywords = URI.encode( @keywords, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") )
  query        = "?session_id=#{@config.session_id}&keywords=#{url_keywords}&block_adult=#{block_adult}"
  resp         = RestClient.post( @config.default_host + path + query, {} )
  data         = JSON.parse resp.body

  raise KeywordToolError.new('get_volumes', data['detail']) if data['code'].match(/error/i)

  return data['data']
rescue JSON::ParserError => e
  raise KeywordToolError.new('get_volumes', 'Bad response from Wordstream when trying to get keyword volumes.')
end