serpapi-uule-converter-light 0.1.1

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: 518adb1455cfca249af3911fa9b396c4d9660e78319d02bc2247cdf1bec7cab4
4
+ data.tar.gz: 43872e6da6ad0a1220b7dca1bcc021fa5d75035914965a36c89659d7a4dc7d10
5
+ SHA512:
6
+ metadata.gz: 4f9b29008a146344f2f07cf73ce7fff55f988c76cb933366c0460a6b79978307dc1eeeecb4dfacc74acff094e7022b7f231e2b62ed6ec484ff99c38b607c063c
7
+ data.tar.gz: 59c6cf7c936f1a1efd3412c17990147f93f8a76156663f2e14f7d85e5cb1a60d4dce4add389a3afde38f851850c01b37bb4e396f5263af3fd56df725c33fb5a7
@@ -0,0 +1,61 @@
1
+ # lib/serpapi-uule-converter-light.rb
2
+ require 'base64'
3
+
4
+ class UuleConverter
5
+ E7_FACTOR = 10_000_000
6
+
7
+ def self.encode(latitude, longitude, radius: -1, role: 1, producer: 12, provenance: 0, timestamp: nil)
8
+ timestamp ||= (Time.now.to_f * 1_000_000).to_i
9
+ lat_e7 = (latitude * E7_FACTOR).to_i
10
+ lon_e7 = (longitude * E7_FACTOR).to_i
11
+
12
+ uule_string = <<~UULE
13
+ role:#{role}
14
+ producer:#{producer}
15
+ provenance:#{provenance}
16
+ timestamp:#{timestamp}
17
+ latlng{
18
+ latitude_e7:#{lat_e7}
19
+ longitude_e7:#{lon_e7}
20
+ }
21
+ radius:#{radius}
22
+ UULE
23
+
24
+ 'a+' + urlsafe_encode64(uule_string)
25
+ end
26
+
27
+ def self.decode(uule_encoded)
28
+ uule_string = urlsafe_decode64(uule_encoded[2..]) # Remove the 'a+' prefix before decoding
29
+ data = {}
30
+
31
+ uule_string.lines.each do |line|
32
+ key, value = line.strip.split(':')
33
+ data[key.to_sym] = value
34
+ end
35
+
36
+ data[:latitude] = data[:latitude_e7].to_i / E7_FACTOR.to_f
37
+ data[:longitude] = data[:longitude_e7].to_i / E7_FACTOR.to_f
38
+ data.delete(:latitude_e7)
39
+ data.delete(:longitude_e7)
40
+
41
+ data
42
+ end
43
+
44
+ private
45
+
46
+ def self.urlsafe_encode64(str)
47
+ Base64.strict_encode64(str).tr('+/', '-_').delete('=')
48
+ end
49
+
50
+ def self.urlsafe_decode64(str)
51
+ str = str.tr('-_', '+/').ljust((str.length + 3) & ~3, '=')
52
+ Base64.strict_decode64(str)
53
+ end
54
+ end
55
+
56
+ # No change has been observed by changing provenance and radius parameters
57
+ # I still added them there in case they matter in the future.
58
+ # Setting radius to `-1` and provenance to `0` works for now.
59
+ # Setting user to `1` means `USER_SPECIFIED_FOR_REQUEST`.
60
+ # Setting producer to `12`` means `LOGGED_IN_USER_SPECIFIED`.
61
+ # Changing the value for the user and producer is shifting precision of the search.
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serpapi-uule-converter-light
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
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
+ description: A Ruby library for encoding and decoding UULE parameters in Google search
14
+ URLs using coordinates
15
+ email:
16
+ - kagermanovtalks@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/serpapi-uule-converter-light.rb
22
+ homepage: https://github.com/serpapi/uule_converter_light
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 2.5.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.1.4
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Coordinates to UULE parameter converter for Google search
45
+ test_files: []