keywords_everywhere 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a3d07491afe85d0828f5d8803feb6b9654c7174de1090dc91030ef6810f27559
4
+ data.tar.gz: 6c858f17aa1e209ab4823a83ca162d9852a7da7cfc5d0b35943ced1548934ac9
5
+ SHA512:
6
+ metadata.gz: cc737c42291e12b3d4cde4e658a74334ba780149ab3b5d544a9b897f4b06958779f490b2702e8ea1437b8ad2cc088b620e458554211342d1bca9a6521317c913
7
+ data.tar.gz: 3ee10fa75db56c82a7e3835e6f40734bf6439ad016a76dbdf73e892d44b76a63ea6f2fc798dde79acdbee5ff25c0396f832420468bcba216c17df2d95d1f8d21
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.gem
13
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in keywords_everywhere.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Sead Feng
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # KeywordsEverywhere
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/keywords_everywhere`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'keywords_everywhere'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install keywords_everywhere
22
+
23
+ ## Usage
24
+
25
+ # configure
26
+ ```ruby
27
+ KeywordsEverywhere.configure do |config|
28
+ config.secret_key = 'some_key'
29
+ end
30
+ c = KeywordsEverywhere::Client.new
31
+ c.search_items(['nike','hp'])
32
+ ```
33
+ # or
34
+ ```ruby
35
+ c = KeywordsEverywhere::Client.new(secret_key: 'some_key')
36
+ c.search_items(['nike','hp'])
37
+ ```
38
+
39
+ ## Development
40
+
41
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
42
+
43
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/seadfeng/keywords_everywhere. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/seadfeng/keywords_everywhere/blob/master/CODE_OF_CONDUCT.md).
48
+
49
+
50
+ ## License
51
+
52
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
53
+
54
+ ## Code of Conduct
55
+
56
+ Everyone interacting in the KeywordsEverywhere project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/seadfeng/keywords_everywhere/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "keywords_everywhere"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ lib = File.expand_path('lib', __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require_relative 'lib/keywords_everywhere/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "keywords_everywhere"
7
+ spec.version = KeywordsEverywhere::VERSION
8
+ spec.authors = ["Sead Feng"]
9
+ spec.email = ["seadfeng@icoud.com"]
10
+
11
+ spec.summary = %q{Client library for KeywordsEverywhere}
12
+ spec.homepage = "https://github.com/seadfeng/keywords_everywhere"
13
+ spec.license = "MIT"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency 'rest-client', '~> 2.1.0'
29
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+ require 'rest-client'
3
+ module KeywordsEverywhere
4
+ class Client
5
+ attr_accessor :source, :currency, :market
6
+ attr_reader :response, :secret_key
7
+
8
+ def initialize(
9
+ secret_key: KeywordsEverywhere.secret_key || ENV['KW_SECRET'],
10
+ market: KeywordsEverywhere.market || DEFAULT_MARKET,
11
+ source: KeywordsEverywhere.source || DEFAULT_SOURCE,
12
+ currency: KeywordsEverywhere.currency || DEFAULT_CURRENCY
13
+ )
14
+
15
+ @source = source.to_s.downcase
16
+ @secret_key = secret_key
17
+ @market = market.to_s.downcase
18
+ @currency = currency.to_s.downcase
19
+
20
+ end
21
+
22
+ def search_items(keywords)
23
+ if keywords.is_a?(String)
24
+ keywords = [keywords]
25
+ end
26
+ raise StandardError, "Keyword Up to 100" if keywords.size > 100 # https://api.keywordseverywhere.com/docs/#/keywords/get_keywords_data
27
+
28
+ client = rest_client( method: :post,
29
+ url: "https://api.keywordseverywhere.com/v1/get_keyword_data",
30
+ payload: payload( kw: keywords ),
31
+ headers: headers )
32
+ @response = JSON.parse(client.body)
33
+ to_items
34
+ end
35
+
36
+ def credits
37
+ response.dig('credits')
38
+ end
39
+
40
+ def time
41
+ response.dig('time')
42
+ end
43
+
44
+ def list
45
+ List.new(response.dig('data'))
46
+ end
47
+
48
+ def to_items
49
+ list.to_items
50
+ end
51
+
52
+ protected
53
+
54
+ def payload(merge_params = {} )
55
+ {
56
+ dataSource: source,
57
+ country: market,
58
+ currency: currency
59
+ }.merge(merge_params)
60
+ end
61
+
62
+ def headers
63
+ {
64
+ "authority": "api.keywordseverywhere.com",
65
+ "accept": "application/json",
66
+ "authorization": "Bearer #{secret_key}",
67
+ }
68
+ end
69
+
70
+ private
71
+
72
+ def rest_client( opts = {} )
73
+ begin
74
+ RestClient::Request.execute(opts)
75
+ rescue RestClient::ExceptionWithResponse => e
76
+ case e.http_code
77
+ when 301, 302, 307
78
+ e.response.follow_redirection
79
+ else
80
+ raise
81
+ end
82
+ end
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+ require 'forwardable'
3
+ module KeywordsEverywhere
4
+ class Item
5
+ extend Forwardable
6
+ attr_accessor :hash
7
+
8
+ def_delegators :@hash, :dig, :to_json
9
+
10
+ def initialize(data)
11
+ @hash = data
12
+ end
13
+
14
+ def vol
15
+ get(%w{vol})
16
+ end
17
+
18
+ def cpc
19
+ get(%w{cpc})
20
+ end
21
+
22
+ def keyword
23
+ get(%w{keyword})
24
+ end
25
+
26
+ def competition
27
+ get(%w{competition})
28
+ end
29
+
30
+ def trend
31
+ get(%w{trend})
32
+ end
33
+
34
+ def get(keys)
35
+ @hash.dig(*keys)
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+ require 'forwardable'
3
+ module KeywordsEverywhere
4
+ class List
5
+ extend Forwardable
6
+ attr_accessor :items
7
+
8
+ def_delegators :@items, :to_json, :size
9
+
10
+ def initialize(data)
11
+ @items = data
12
+ end
13
+
14
+ def self.to_items(data)
15
+ data.map {|d| Item.new(d)}
16
+ end
17
+
18
+ def to_items
19
+ items.map {|d| Item.new(d)}
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module KeywordsEverywhere
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+ require "keywords_everywhere/version"
3
+
4
+ require 'keywords_everywhere/client'
5
+ require 'keywords_everywhere/list'
6
+ require 'keywords_everywhere/item'
7
+
8
+ require 'json'
9
+
10
+ module KeywordsEverywhere
11
+ class Error < StandardError; end
12
+ DEFAULT_MARKET = :us
13
+ DEFAULT_SOURCE = :gkp
14
+ DEFAULT_CURRENCY = :usd
15
+
16
+
17
+ class << self
18
+ attr_accessor :secret_key, :source, :market, :currency
19
+
20
+ def configure
21
+ yield self
22
+ true
23
+ end
24
+ alias_method :config, :configure
25
+ end
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keywords_everywhere
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sead Feng
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.1.0
27
+ description:
28
+ email:
29
+ - seadfeng@icoud.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".travis.yml"
37
+ - Gemfile
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - bin/console
42
+ - bin/setup
43
+ - keywords_everywhere.gemspec
44
+ - lib/keywords_everywhere.rb
45
+ - lib/keywords_everywhere/client.rb
46
+ - lib/keywords_everywhere/item.rb
47
+ - lib/keywords_everywhere/list.rb
48
+ - lib/keywords_everywhere/version.rb
49
+ homepage: https://github.com/seadfeng/keywords_everywhere
50
+ licenses:
51
+ - MIT
52
+ metadata:
53
+ homepage_uri: https://github.com/seadfeng/keywords_everywhere
54
+ source_code_uri: https://github.com/seadfeng/keywords_everywhere
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.1.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Client library for KeywordsEverywhere
74
+ test_files: []