serpapi-uule-converter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/serpapi-uule-converter.rb +69 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 930e7fbfa964f3cbbd90372017da0be2b2c29f5f3d8252db91f1cd88194d0497
4
+ data.tar.gz: dacf03af6c226a8e694c29dcb1fd3182c3db207b31cb4240c5933804ea92e4c8
5
+ SHA512:
6
+ metadata.gz: fbf77711f737acdcd47bb198b0213fee63312f2228986da018811128bf04155a476f3b4d55d710de2f7b6b8ce2a52639f0ee0523577abd5dfc3bd03bcce14122
7
+ data.tar.gz: b6b0e7288c4a8b68cc3e18684aaa7404e7ef4e5e7fb755ed6fdcfdbd750682511b8a29c3819dc0ff69b3866d5c5befa2d8d9d838dd0dc2b1bd6aee0afd588c0d
@@ -0,0 +1,69 @@
1
+ # lib/serpapi-uule-converter.rb
2
+ require 'base64'
3
+ require 'geocoder'
4
+
5
+ class UuleConverter
6
+ E7_FACTOR = 10_000_000
7
+
8
+ def self.get_gl(latitude, longitude)
9
+ results = Geocoder.search([latitude, longitude])
10
+
11
+ # If a result is found, return the country code (gl parameter)
12
+ results.first&.country_code&.downcase
13
+ end
14
+
15
+ def self.encode(latitude, longitude, radius: -1, role: 1, producer: 12, provenance: 0, timestamp: nil)
16
+ timestamp ||= (Time.now.to_f * 1_000_000).to_i
17
+ lat_e7 = (latitude * E7_FACTOR).to_i
18
+ lon_e7 = (longitude * E7_FACTOR).to_i
19
+
20
+ uule_string = <<~UULE
21
+ role:#{role}
22
+ producer:#{producer}
23
+ provenance:#{provenance}
24
+ timestamp:#{timestamp}
25
+ latlng{
26
+ latitude_e7:#{lat_e7}
27
+ longitude_e7:#{lon_e7}
28
+ }
29
+ radius:#{radius}
30
+ UULE
31
+
32
+ 'a+' + urlsafe_encode64(uule_string)
33
+ end
34
+
35
+ def self.decode(uule_encoded)
36
+ uule_string = urlsafe_decode64(uule_encoded[2..]) # Remove the 'a+' prefix before decoding
37
+ data = {}
38
+
39
+ uule_string.lines.each do |line|
40
+ key, value = line.strip.split(':')
41
+ data[key.to_sym] = value
42
+ end
43
+
44
+ data[:latitude] = data[:latitude_e7].to_i / E7_FACTOR.to_f
45
+ data[:longitude] = data[:longitude_e7].to_i / E7_FACTOR.to_f
46
+ data.delete(:latitude_e7)
47
+ data.delete(:longitude_e7)
48
+
49
+ data
50
+ end
51
+
52
+ private
53
+
54
+ def self.urlsafe_encode64(str)
55
+ Base64.strict_encode64(str).tr('+/', '-_').delete('=')
56
+ end
57
+
58
+ def self.urlsafe_decode64(str)
59
+ str = str.tr('-_', '+/').ljust((str.length + 3) & ~3, '=')
60
+ Base64.strict_decode64(str)
61
+ end
62
+ end
63
+
64
+ # No change has been observed by changing provenance and radius parameters
65
+ # I still added them there in case they matter in the future.
66
+ # Setting radius to `-1` and provenance to `0` works for now.
67
+ # Setting user to `1` means `USER_SPECIFIED_FOR_REQUEST`.
68
+ # Setting producer to `12`` means `LOGGED_IN_USER_SPECIFIED`.
69
+ # Changing the value for the user and producer is shifting precision of the search.
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serpapi-uule-converter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emirhan Akdeniz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-03-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: geocoder
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ description: A Ruby library for encoding and decoding UULE parameters in Google search
28
+ URLs using coordinates
29
+ email:
30
+ - kagermanovtalks@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/serpapi-uule-converter.rb
36
+ homepage: https://github.com/serpapi/uule_converter
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.5.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.1.4
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Coordinates to UULE parameter converter for Google search
59
+ test_files: []