Class: Poke::API::Helpers
- Inherits:
-
Object
- Object
- Poke::API::Helpers
- Defined in:
- lib/poke-api/helpers.rb
Class Method Summary collapse
- .camel_case_lower(sym) ⇒ Object
- .d2h(input) ⇒ Object
- .fetch_time(ms: true) ⇒ Object
- .format_time_diff(low, high, ms = true) ⇒ Object
- .generate_location_one(auth_ticket, pos) ⇒ Object
- .generate_location_two(pos) ⇒ Object
- .generate_request(auth_ticket, request) ⇒ Object
- .get_cells(lat, lng, radius = 10) ⇒ Object
- .get_position(pos) ⇒ Object
Class Method Details
.camel_case_lower(sym) ⇒ Object
12 13 14 |
# File 'lib/poke-api/helpers.rb', line 12 def camel_case_lower(sym) sym.to_s.split('_').collect(&:capitalize).join end |
.d2h(input) ⇒ Object
39 40 41 |
# File 'lib/poke-api/helpers.rb', line 39 def d2h(input) [input].pack('d').unpack('Q').first.to_s(16).split.pack('H*') end |
.fetch_time(ms: true) ⇒ Object
16 17 18 |
# File 'lib/poke-api/helpers.rb', line 16 def fetch_time(ms: true) ms ? (Time.now.to_f * 1000).to_i : Time.now.to_i end |
.format_time_diff(low, high, ms = true) ⇒ Object
43 44 45 46 47 |
# File 'lib/poke-api/helpers.rb', line 43 def format_time_diff(low, high, ms = true) diff = high - low m, s = ms ? (diff / 1000).divmod(60) : diff.divmod(60) m.divmod(60) + [s] end |
.generate_location_one(auth_ticket, pos) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/poke-api/helpers.rb', line 20 def generate_location_one(auth_ticket, pos) first_hash = XXhash.xxh32(auth_ticket, 0x1B845238) location_bytes = d2h(pos[0]) + d2h(pos[1]) + d2h(pos[2]) XXhash.xxh32(location_bytes, first_hash) end |
.generate_location_two(pos) ⇒ Object
27 28 29 30 31 |
# File 'lib/poke-api/helpers.rb', line 27 def generate_location_two(pos) location_bytes = d2h(pos[0]) + d2h(pos[1]) + d2h(pos[2]) XXhash.xxh32(location_bytes, 0x1B845238) end |
.generate_request(auth_ticket, request) ⇒ Object
33 34 35 36 37 |
# File 'lib/poke-api/helpers.rb', line 33 def generate_request(auth_ticket, request) first_hash = XXhash.xxh64(auth_ticket, 0x1B845238) XXhash.xxh64(request, first_hash) end |
.get_cells(lat, lng, radius = 10) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/poke-api/helpers.rb', line 49 def get_cells(lat, lng, radius = 10) s2_point = Poke::API::Geometry::S2LatLon.new(lat, lng).to_point s2_cell = Poke::API::Geometry::S2CellId.from_point(s2_point).parent(15) next_cell = s2_cell.next prev_cell = s2_cell.prev radius.times.reduce([s2_cell.id]) do |acc, _| acc += [next_cell.id, prev_cell.id] next_cell = next_cell.next prev_cell = prev_cell.prev acc end.sort end |
.get_position(pos) ⇒ Object
8 9 10 |
# File 'lib/poke-api/helpers.rb', line 8 def get_position(pos) Geocoder.search(pos) end |