customer_miner 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6dccef15d21a4b982d9941e9758f178195f17836
4
- data.tar.gz: 5ddfe0047ead123ad08454d20152c431a88c9f75
3
+ metadata.gz: 3bbbc97ff18daae89541e0cf384f6dc204d7368b
4
+ data.tar.gz: ca0ec9286c324772c8d215a3261069e377f59f00
5
5
  SHA512:
6
- metadata.gz: 6d2a7dcc775de22b73e114e3f4cd1cd5e321d4907e06c3f670c3e45fedf7e839b90eb14f05326062f1ca0e194788de8caff59cca673dc662661cfd93a73f13e0
7
- data.tar.gz: b8909a924ae0f09e724b44a8552512825fb63223068b7b21f38126b249e19e9f5bc932afb2b9e4724f95fa47239d4a82c170d93efa96e96d90a4da55f34f80d4
6
+ metadata.gz: 95acf74554d25e1c9c76c155eb299c99ccfed3fb60efc6993160914ebee3221cc6ce54b2eacd5c61494e6f3377d3650ae83c2c2b124a63e34167c10191984e05
7
+ data.tar.gz: 61fac0d7615848f849e4e85d4b1279f14d3a7f15a84e031b771f1f15987003a7d0409296a80e8076b1806d74bbe1e45a6e10b339b353bd6e79d5923cf7c82a2c
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,19 +1,29 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- customer_miner.gemspec (0.0.1)
4
+ customer_miner (0.0.1)
5
+ httparty (~> 0.15.5)
5
6
  thor (~> 0.19.4)
7
+ typhoeus (~> 1.1.2)
6
8
 
7
9
  GEM
8
10
  remote: https://rubygems.org/
9
11
  specs:
12
+ ethon (0.10.1)
13
+ ffi (>= 1.3.0)
14
+ ffi (1.9.17)
15
+ httparty (0.15.5)
16
+ multi_xml (>= 0.5.2)
17
+ multi_xml (0.6.0)
10
18
  thor (0.19.4)
19
+ typhoeus (1.1.2)
20
+ ethon (>= 0.9.0)
11
21
 
12
22
  PLATFORMS
13
23
  ruby
14
24
 
15
25
  DEPENDENCIES
16
- customer_miner.gemspec!
26
+ customer_miner!
17
27
 
18
28
  BUNDLED WITH
19
29
  1.13.7
data/README.md ADDED
@@ -0,0 +1,19 @@
1
+ # customer_miner
2
+ > Fetch customer data using Clearbit API
3
+
4
+ ## install
5
+
6
+ ```sh
7
+ gem install customer_miner
8
+ ```
9
+
10
+ ## usage
11
+ ```sh
12
+ # Set API secret key
13
+ cm set_key key=your-secret-key
14
+
15
+ # Query customer from clearbit csv file export from ga
16
+ cm query ./Clearbit.csv
17
+ ```
18
+
19
+ MIT
@@ -17,4 +17,6 @@ Gem::Specification.new do |s|
17
17
  s.license = 'MIT'
18
18
 
19
19
  s.add_dependency('thor',["~> 0.19.4"])
20
- end
20
+ s.add_dependency('httparty',["~> 0.15.5"])
21
+ s.add_dependency('typhoeus',["~> 1.1"])
22
+ end
@@ -1,5 +1,6 @@
1
1
  require 'thor'
2
2
  require 'customer_miner/version'
3
+ require 'customer_miner/query'
3
4
 
4
5
  module CustomerMiner
5
6
  class CLI< Thor
@@ -10,5 +11,28 @@ module CustomerMiner
10
11
  puts "#{File.basename($0)} #{VERSION}"
11
12
  end
12
13
 
14
+ desc 'set_key', 'Set secret API key. You can get it from https://dashboard.clearbit.com/api'
15
+ option :key, required: true, banner: "your_secret_api_key"
16
+ def set_key
17
+ key = options[:key]
18
+ file = "#{Dir.home}/.customer_miner"
19
+ File.open(file, 'w') do |file|
20
+ file.write(key)
21
+ end
22
+ File.chmod(0600, file)
23
+ puts "Set secret API key successfully"
24
+ end
25
+
26
+ desc 'query', 'query customer data and generate csv file'
27
+ def query(args)
28
+ unless args
29
+ puts "plese speciy file name"
30
+ return
31
+ end
32
+
33
+ file_name = "#{Dir.home}/.customer_miner"
34
+ key = File.read(file_name)
35
+ Query.new(file: args, secret_key:key).perform
36
+ end
13
37
  end
14
38
  end
@@ -0,0 +1,80 @@
1
+ require 'csv'
2
+ require 'json'
3
+ require 'typhoeus'
4
+
5
+ module CustomerMiner
6
+ class Query
7
+ def initialize(file:, secret_key:)
8
+ @file = file
9
+ @secret_key = secret_key
10
+ end
11
+
12
+ def perform
13
+ domains = extract_domains
14
+ puts "get #{domains.size} domain from csv #{@file}"
15
+ puts "start request clearbit"
16
+ res = query_clearbit(domains)
17
+ build_csv(res)
18
+ end
19
+
20
+ private
21
+
22
+ def extract_domains
23
+ reg = /^#.*/
24
+ rows = CSV.read(@file, headers: true, skip_lines: reg)
25
+ rows.map { |row| row['Clearbit Company Domain'] }.compact.uniq
26
+ end
27
+
28
+ def query_clearbit(domains)
29
+ hydra = Typhoeus::Hydra.new(max_concurrency: 5)
30
+ url = 'https://prospector.clearbit.com/v1/people/search'
31
+ userpwd = "#{@secret_key}:"
32
+ requests = domains.map do |domain|
33
+ options = {
34
+ userpwd: userpwd,
35
+ params: {
36
+ domain: domain,
37
+ role: 'marketing'
38
+ }
39
+ }
40
+ req = Typhoeus::Request.new(url, options)
41
+ hydra.queue(req)
42
+ req
43
+ end
44
+ hydra.run
45
+ puts "complete requests"
46
+ requests.select { |req| req.response.success? }.map do |request|
47
+ res_hash = JSON.parse(request.response.body)
48
+ emails = res_hash.map { |person| person['email']}
49
+ domain = request.options[:params][:domain]
50
+ { domain: domain, people: res_hash }
51
+ end
52
+ end
53
+
54
+ def build_csv(res)
55
+ rows = res.map do |item|
56
+ domain = item[:domain]
57
+ item[:people].map do |person|
58
+ build_row(person, domain)
59
+ end
60
+ end
61
+ headers = ['domain', 'fullName', 'title', 'role', 'seniority', 'email',
62
+ 'verified', 'phone'].join(',')
63
+ file_content = [headers].concat(rows).join("\n")
64
+
65
+ file_path = "#{Dir.pwd}/result.csv"
66
+ File.open(file_path, 'w') do |file|
67
+ file.write(file_content)
68
+ end
69
+ puts "save file in #{file_path}"
70
+ end
71
+
72
+ def build_row(person, domain)
73
+ attrs = ['title', 'role', 'seniority', 'email', 'verified', 'phone']
74
+ person_attrs = attrs.map { |attr| person[attr] }
75
+ [domain, person['name']['fullName']].concat(person_attrs)
76
+ .map { |str| str.to_s.gsub(',', ";") }
77
+ .join(',')
78
+ end
79
+ end
80
+ end
@@ -1,3 +1,3 @@
1
1
  module CustomerMiner
2
- VERSION = '0.0.1'
3
- end
2
+ VERSION = '0.0.2'
3
+ end
data/script/build ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+
3
+ gem build customer_miner.gemspec
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: customer_miner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - ocowchun
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.19.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.15.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.15.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: typhoeus
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.1'
27
55
  description: Fetch customer data using Clearbit API
28
56
  email: ocowchun@gmail.com
29
57
  executables:
@@ -34,10 +62,13 @@ files:
34
62
  - ".gitignore"
35
63
  - Gemfile
36
64
  - Gemfile.lock
65
+ - README.md
37
66
  - bin/cm
38
67
  - customer_miner.gemspec
39
68
  - lib/customer_miner/cli.rb
69
+ - lib/customer_miner/query.rb
40
70
  - lib/customer_miner/version.rb
71
+ - script/build
41
72
  homepage: https://github.com/ocowchun/customer_miner
42
73
  licenses:
43
74
  - MIT