Module: Fbi
- Defined in:
- lib/fbi.rb,
lib/fbi/version.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"0.1.6"
Class Method Summary collapse
- .access_token_check ⇒ Object
- .access_token_request(error_message) ⇒ Object
- .audience_bracket_display(verbose, audience_size_lower_bound, audience_size_upper_bound) ⇒ Object
- .audience_size_to_kmb(audience_size) ⇒ Object
- .get_json_hash_from_interest(interest) ⇒ Object
- .get_rows(json_hash, filter_string, verbose = nil) ⇒ Object
- .print_json_hash(json_hash, mode = nil, filter_string = nil) ⇒ Object
- .start ⇒ Object
Class Method Details
.access_token_check ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/fbi.rb', line 117 def self.access_token_check @access_token = @store.transaction { @store.fetch(@store_sym, nil) } if @access_token.nil? access_token_request "Welcome!" end return true end |
.access_token_request(error_message) ⇒ Object
128 129 130 131 132 133 134 135 136 |
# File 'lib/fbi.rb', line 128 def self.access_token_request puts Terminal::Table.new(rows: [[]]) print "Please enter App Token: " @access_token = gets.chomp @store.transaction do @store[@store_sym] = @access_token end puts Terminal::Table.new(rows: [["App Token saved"],["Please proceed with search!"]]) end |
.audience_bracket_display(verbose, audience_size_lower_bound, audience_size_upper_bound) ⇒ Object
38 39 40 |
# File 'lib/fbi.rb', line 38 def self.audience_bracket_display verbose, audience_size_lower_bound, audience_size_upper_bound 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)})" end |
.audience_size_to_kmb(audience_size) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/fbi.rb', line 42 def self.audience_size_to_kmb audience_size delimited_str = audience_size.to_s(:delimited) arr = delimited_str.split(",") case arr.length when 1 return arr[0] when 2 return arr[0]+"k" when 3 return arr[0].length == 1 && arr[0] == "1" ? arr[0] + (arr[1][0] == "0" ? "" : "." + arr[1][0]) + "M" : arr[0] + "M" when 4 return arr[0]+"B" end end |
.get_json_hash_from_interest(interest) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/fbi.rb', line 57 def self.get_json_hash_from_interest interest uri = "https://graph.facebook.com/search?type=adinterest&q=[#{interest}]&limit=10000&locale=en_US&access_token=#{@access_token}" begin return JSON.parse(URI.open(uri).read) rescue return nil # return nil, caller will handle error else #... executes when no error ensure #... always executed end end |
.get_rows(json_hash, filter_string, verbose = nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/fbi.rb', line 19 def self.get_rows json_hash, filter_string, verbose=nil rows = [] json_hash['data'].each do |child| 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 if (@normal_path_elements - child["path"]).empty? rows << ["#{child['name']} #{audience_bracket_display(verbose, child['audience_size_lower_bound'], child['audience_size_upper_bound'])}", child['topic'], child['disambiguation_category'].nil? ? "" : child['disambiguation_category']] else @additional_paths = true rows << ["#{child['name']} #{audience_bracket_display(verbose, child['audience_size_lower_bound'], child['audience_size_upper_bound'])}", child['topic'], child['disambiguation_category'], (child["path"]-@normal_path_elements).join(", ")] end end end rows end |
.print_json_hash(json_hash, mode = nil, filter_string = nil) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/fbi.rb', line 72 def self.print_json_hash json_hash, mode=nil, filter_string=nil rows = [] @additional_paths = false print_table = true case mode when "c" # TODO: return copyable of filtered rows json_hash['data'].each do |child| rows << ["#{child['name']} #{audience_bracket_display(false, child['audience_size_lower_bound'], child['audience_size_upper_bound'])}"] end print_table = false when "m" rows = get_rows json_hash, "magazine" when "t" rows = get_rows json_hash, "tv" when "w" rows = get_rows json_hash, "website" when "v" # TODO: return verbose of filtered rows rows = get_rows json_hash, "", true # verbose, no filter when "f" rows = get_rows json_hash, filter_string else rows = get_rows json_hash, "" # normal, no filter end if print_table # puts Terminal::Table.new(headings: ['Keyword', 'Topic', 'Disambiguation Category', 'Path'], style: {all_separators: true}, rows: rows) if @additional_paths # rows.map {|sub_array| sub_array.in_groups_of(4, "").flatten } # in_groups_of is a rails method max_size = rows.map(&:size).max rows = rows.map { |a| Array.new(max_size) { |i| a[i] || '' } } puts Terminal::Table.new(headings: ['Keyword', 'Topic', 'Disambiguation Category', 'Additional Paths'], rows: rows) else puts Terminal::Table.new(headings: ['Keyword', 'Topic', 'Disambiguation Category'], rows: rows, style: {border_right: true}) end else rows.each do |row| puts row end end # @table.rows = rows # puts @table end |
.start ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/fbi.rb', line 138 def self.start access_token_check loop do print "❯ " interest = gets.chomp case interest.strip #strip removes front and back empty spaces when "q" puts Terminal::Table.new(rows: [["Bye!"]]) break when "" puts Terminal::Table.new(rows: [["Empty input"]]) when "c" #for copying print_json_hash @json_hash, "c" when "v" #verbose print_json_hash @json_hash, "v" when "m" #magazine filter print_json_hash @json_hash, "m" when "t" #tv filter print_json_hash @json_hash, "t" when "w" #website filter print_json_hash @json_hash, "w" when "r" #reprint print_json_hash @json_hash when /^f\s/ # filter results # match string starting with "f " https://www.w3resource.com/ruby/ruby-case-statement.php, https://regexone.com/lesson/whitespaces print_json_hash @json_hash, "f", interest.sub(/^f\s/, '') # removing "f " else @json_hash = get_json_hash_from_interest interest if @json_hash.nil? access_token_request "App Token invalid" else print_json_hash @json_hash end end end end |