fbi 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3ac16f4bfeb0862537f5cf49149c5c9522b7e586cd645e40e2edc8df6494bade
4
+ data.tar.gz: a43b951353a797bc2c36d2ddefdf7979642fed5740c05f89718c00744165291d
5
+ SHA512:
6
+ metadata.gz: 11a296da85e559149a3f653bf7b13273808584ccd4c5f0161e77dc3f6c2646629e88f11c98343c319ec511204ab6ceb2ade646837b25ea720691c1bb6743f095
7
+ data.tar.gz: 2d789f1677b326d63bab599d3f8a05fb80abae8e8bfeef638ce18fe8c756da5ec6f5789f4658fb6e78307fb82b819fa55b7d9d7fd67e281c99e3e2698184fd0c
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /*.gem
10
+ /*.yml
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in fbi.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,98 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fbi (0.1.4)
5
+ facebookbusiness (~> 0.11.0.0)
6
+ fb_graph2 (~> 1.2)
7
+ terminal-table (~> 3.0, >= 3.0.1)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (6.1.4)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 1.6, < 2)
15
+ minitest (>= 5.1)
16
+ tzinfo (~> 2.0)
17
+ zeitwerk (~> 2.3)
18
+ aes_key_wrap (1.1.0)
19
+ attr_required (1.0.1)
20
+ bindata (2.4.10)
21
+ concurrent-ruby (1.1.10)
22
+ countries (3.1.0)
23
+ i18n_data (~> 0.11.0)
24
+ sixarm_ruby_unaccent (~> 1.1)
25
+ unicode_utils (~> 1.4)
26
+ facebookbusiness (0.11.0.0)
27
+ concurrent-ruby (~> 1.1)
28
+ countries (~> 3.0)
29
+ faraday (~> 1.0)
30
+ json (~> 2.2)
31
+ money (~> 6.13)
32
+ faraday (1.7.0)
33
+ faraday-em_http (~> 1.0)
34
+ faraday-em_synchrony (~> 1.0)
35
+ faraday-excon (~> 1.1)
36
+ faraday-httpclient (~> 1.0.1)
37
+ faraday-net_http (~> 1.0)
38
+ faraday-net_http_persistent (~> 1.1)
39
+ faraday-patron (~> 1.0)
40
+ faraday-rack (~> 1.0)
41
+ multipart-post (>= 1.2, < 3)
42
+ ruby2_keywords (>= 0.0.4)
43
+ faraday-em_http (1.0.0)
44
+ faraday-em_synchrony (1.0.0)
45
+ faraday-excon (1.1.0)
46
+ faraday-httpclient (1.0.1)
47
+ faraday-net_http (1.0.1)
48
+ faraday-net_http_persistent (1.2.0)
49
+ faraday-patron (1.0.0)
50
+ faraday-rack (1.0.0)
51
+ fb_graph2 (1.2.0)
52
+ activesupport (>= 3.2)
53
+ httpclient (>= 2.4)
54
+ multi_json
55
+ rack-oauth2 (>= 1.1)
56
+ url_safe_base64
57
+ httpclient (2.8.3)
58
+ i18n (1.8.10)
59
+ concurrent-ruby (~> 1.0)
60
+ i18n_data (0.11.0)
61
+ json (2.5.1)
62
+ json-jwt (1.13.0)
63
+ activesupport (>= 4.2)
64
+ aes_key_wrap
65
+ bindata
66
+ minitest (5.16.1)
67
+ money (6.16.0)
68
+ i18n (>= 0.6.4, <= 2)
69
+ multi_json (1.15.0)
70
+ multipart-post (2.1.1)
71
+ rack (2.2.3)
72
+ rack-oauth2 (1.18.0)
73
+ activesupport
74
+ attr_required
75
+ httpclient
76
+ json-jwt (>= 1.11.0)
77
+ rack (>= 2.1.0)
78
+ rake (12.3.3)
79
+ ruby2_keywords (0.0.5)
80
+ sixarm_ruby_unaccent (1.2.0)
81
+ terminal-table (3.0.1)
82
+ unicode-display_width (>= 1.1.1, < 3)
83
+ tzinfo (2.0.4)
84
+ concurrent-ruby (~> 1.0)
85
+ unicode-display_width (2.0.0)
86
+ unicode_utils (1.4.0)
87
+ url_safe_base64 (0.2.2)
88
+ zeitwerk (2.4.2)
89
+
90
+ PLATFORMS
91
+ ruby
92
+
93
+ DEPENDENCIES
94
+ fbi!
95
+ rake (~> 12.0)
96
+
97
+ BUNDLED WITH
98
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ivan Chong
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,40 @@
1
+ # Fbi
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/fbi`. 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 'fbi'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fbi
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fbi.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fbi"
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
data/exe/fbi ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fbi"
5
+
6
+ Fbi.start
data/fbi.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ require_relative 'lib/fbi/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "fbi"
5
+ spec.version = Fbi::VERSION
6
+ spec.authors = ["Ivan Chong"]
7
+ spec.email = ["ivan@ambient.sg"]
8
+
9
+ spec.summary = %q{:)}
10
+ # spec.description = %q{TD: Write a longer description or delete this line.}
11
+ spec.homepage = "https://ambient.sg"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ # spec.metadata["source_code_uri"] = spec.homepage
19
+ # spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+
31
+ spec.add_dependency 'facebookbusiness', '~> 0.11.0.0'
32
+ spec.add_dependency 'fb_graph2', '~> 1.2'
33
+ spec.add_dependency 'terminal-table', '~> 3.0', '>= 3.0.1'
34
+
35
+ end
data/fbi_data.yml ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ :fb_access_token: 793869374639705|645PhCPEyJhDhmGOld5YVbJAIBQ
@@ -0,0 +1,3 @@
1
+ module Fbi
2
+ VERSION = "0.1.6"
3
+ end
data/lib/fbi.rb ADDED
@@ -0,0 +1,178 @@
1
+ require "fbi/version"
2
+
3
+ require 'active_support'
4
+ require 'active_support/core_ext'
5
+ require "yaml/store"
6
+
7
+ require 'terminal-table'
8
+ require 'open-uri'
9
+ require 'json'
10
+
11
+ module Fbi
12
+ class Error < StandardError; end
13
+
14
+ # @table = Terminal::Table.new(all_separators: true)
15
+ # @table.headings = ['Keyword', 'Topic', 'Disambiguation Category', 'Path']
16
+
17
+ @normal_path_elements = ["Interests", "Additional interests"]
18
+
19
+ def self.get_rows json_hash, filter_string, verbose=nil
20
+ rows = []
21
+ json_hash['data'].each do |child|
22
+ if (child['disambiguation_category'].to_s.downcase.include? filter_string.downcase) || (child['name'].to_s.downcase.include? filter_string.downcase) # .to_s returns "" if nil
23
+ if (@normal_path_elements - child["path"]).empty?
24
+ rows << ["#{child['name']} #{audience_bracket_display(verbose, child['audience_size_lower_bound'], child['audience_size_upper_bound'])}",
25
+ child['topic'],
26
+ child['disambiguation_category'].nil? ? "" : child['disambiguation_category']]
27
+ else
28
+ @additional_paths = true
29
+ rows << ["#{child['name']} #{audience_bracket_display(verbose, child['audience_size_lower_bound'], child['audience_size_upper_bound'])}",
30
+ child['topic'],
31
+ child['disambiguation_category'], (child["path"]-@normal_path_elements).join(", ")]
32
+ end
33
+ end
34
+ end
35
+ rows
36
+ end
37
+
38
+ def self.audience_bracket_display verbose, audience_size_lower_bound, audience_size_upper_bound
39
+ return "(#{verbose ? audience_size_lower_bound.to_s(:delimited) : audience_size_to_kmb(audience_size_lower_bound)} - #{verbose ? audience_size_upper_bound.to_s(:delimited) : audience_size_to_kmb(audience_size_upper_bound)})"
40
+ end
41
+
42
+ def self.audience_size_to_kmb audience_size
43
+ delimited_str = audience_size.to_s(:delimited)
44
+ arr = delimited_str.split(",")
45
+ case arr.length
46
+ when 1
47
+ return arr[0]
48
+ when 2
49
+ return arr[0]+"k"
50
+ when 3
51
+ return arr[0].length == 1 && arr[0] == "1" ? arr[0] + (arr[1][0] == "0" ? "" : "." + arr[1][0]) + "M" : arr[0] + "M"
52
+ when 4
53
+ return arr[0]+"B"
54
+ end
55
+ end
56
+
57
+ def self.get_json_hash_from_interest interest
58
+ uri = "https://graph.facebook.com/search?type=adinterest&q=[#{interest}]&limit=10000&locale=en_US&access_token=#{@access_token}"
59
+
60
+ begin
61
+ return JSON.parse(URI.open(uri).read)
62
+ rescue
63
+ return nil # return nil, caller will handle error
64
+ else
65
+ #... executes when no error
66
+ ensure
67
+ #... always executed
68
+ end
69
+
70
+ end
71
+
72
+ def self.print_json_hash json_hash, mode=nil, filter_string=nil
73
+ rows = []
74
+ @additional_paths = false
75
+ print_table = true
76
+ case mode
77
+ when "c" # TODO: return copyable of filtered rows
78
+ json_hash['data'].each do |child|
79
+ rows << ["#{child['name']} #{audience_bracket_display(false, child['audience_size_lower_bound'], child['audience_size_upper_bound'])}"]
80
+ end
81
+ print_table = false
82
+ when "m"
83
+ rows = get_rows json_hash, "magazine"
84
+ when "t"
85
+ rows = get_rows json_hash, "tv"
86
+ when "w"
87
+ rows = get_rows json_hash, "website"
88
+ when "v" # TODO: return verbose of filtered rows
89
+ rows = get_rows json_hash, "", true # verbose, no filter
90
+ when "f"
91
+ rows = get_rows json_hash, filter_string
92
+ else
93
+ rows = get_rows json_hash, "" # normal, no filter
94
+ end
95
+ if print_table
96
+ # puts Terminal::Table.new(headings: ['Keyword', 'Topic', 'Disambiguation Category', 'Path'], style: {all_separators: true}, rows: rows)
97
+ if @additional_paths
98
+ # rows.map {|sub_array| sub_array.in_groups_of(4, "").flatten } # in_groups_of is a rails method
99
+ max_size = rows.map(&:size).max
100
+ rows = rows.map { |a| Array.new(max_size) { |i| a[i] || '' } }
101
+ puts Terminal::Table.new(headings: ['Keyword', 'Topic', 'Disambiguation Category', 'Additional Paths'], rows: rows)
102
+ else
103
+ puts Terminal::Table.new(headings: ['Keyword', 'Topic', 'Disambiguation Category'], rows: rows, style: {border_right: true})
104
+ end
105
+ else
106
+ rows.each do |row|
107
+ puts row
108
+ end
109
+ end
110
+ # @table.rows = rows
111
+ # puts @table
112
+ end
113
+
114
+ @store = YAML::Store.new("fbi_data.yml")
115
+ @store_sym = :fb_access_token
116
+
117
+ def self.access_token_check
118
+
119
+ @access_token = @store.transaction { @store.fetch(@store_sym, nil) }
120
+
121
+ if @access_token.nil?
122
+ access_token_request "Welcome!"
123
+ end
124
+
125
+ return true
126
+ end
127
+
128
+ def self.access_token_request error_message
129
+ puts Terminal::Table.new(rows: [[error_message]])
130
+ print "Please enter App Token: "
131
+ @access_token = gets.chomp
132
+ @store.transaction do
133
+ @store[@store_sym] = @access_token
134
+ end
135
+ puts Terminal::Table.new(rows: [["App Token saved"],["Please proceed with search!"]])
136
+ end
137
+
138
+ def self.start
139
+
140
+ access_token_check
141
+
142
+ loop do
143
+ print "❯ "
144
+ interest = gets.chomp
145
+
146
+ case interest.strip #strip removes front and back empty spaces
147
+ when "q"
148
+ puts Terminal::Table.new(rows: [["Bye!"]])
149
+ break
150
+ when ""
151
+ puts Terminal::Table.new(rows: [["Empty input"]])
152
+ when "c" #for copying
153
+ print_json_hash @json_hash, "c"
154
+ when "v" #verbose
155
+ print_json_hash @json_hash, "v"
156
+ when "m" #magazine filter
157
+ print_json_hash @json_hash, "m"
158
+ when "t" #tv filter
159
+ print_json_hash @json_hash, "t"
160
+ when "w" #website filter
161
+ print_json_hash @json_hash, "w"
162
+ when "r" #reprint
163
+ print_json_hash @json_hash
164
+ when /^f\s/ # filter results # match string starting with "f " https://www.w3resource.com/ruby/ruby-case-statement.php, https://regexone.com/lesson/whitespaces
165
+ print_json_hash @json_hash, "f", interest.sub(/^f\s/, '') # removing "f "
166
+ else
167
+ @json_hash = get_json_hash_from_interest interest
168
+ if @json_hash.nil?
169
+ access_token_request "App Token invalid"
170
+ else
171
+ print_json_hash @json_hash
172
+ end
173
+ end
174
+ end
175
+
176
+ end
177
+
178
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fbi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.6
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Chong
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: facebookbusiness
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.11.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.11.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: fb_graph2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: terminal-table
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 3.0.1
51
+ type: :runtime
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '3.0'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 3.0.1
61
+ description:
62
+ email:
63
+ - ivan@ambient.sg
64
+ executables:
65
+ - fbi
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - ".gitignore"
70
+ - Gemfile
71
+ - Gemfile.lock
72
+ - LICENSE.txt
73
+ - README.md
74
+ - Rakefile
75
+ - bin/console
76
+ - bin/setup
77
+ - exe/fbi
78
+ - fbi.gemspec
79
+ - fbi_data.yml
80
+ - lib/fbi.rb
81
+ - lib/fbi/version.rb
82
+ homepage: https://ambient.sg
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ allowed_push_host: https://rubygems.org
87
+ homepage_uri: https://ambient.sg
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: 2.3.0
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubygems_version: 3.1.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: ":)"
107
+ test_files: []