dont-overstay 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/dont-overstay.rb +78 -0
- metadata +46 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 62614d9d64410b2c509fe2ae6ff9b5dc1ba0bc25
|
|
4
|
+
data.tar.gz: acdd3ab18846d56efeebbdc655d4ddbc6e439655
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 546ca43ccad2439c7a4894b02a3cd6a6ef1b1388ec78f04be2f94bad382d0d356081f26a74ddde8baac4cfdd526ea964d959575a84f45acddf959d0571094a82
|
|
7
|
+
data.tar.gz: 6fe0853207f749980b508c910dde745f4c7c849a0d70b255c9d991bfdfd712ea6293f97573d86e373baa09e405087d34c0d88d030d0c8e02be966145e0e41228
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require 'rest-client'
|
|
2
|
+
require 'json'
|
|
3
|
+
|
|
4
|
+
class Daytracker
|
|
5
|
+
def initialize(tokenval=nil)
|
|
6
|
+
apiver = '20160226'
|
|
7
|
+
base_url = 'https://api.foursquare.com/v2/users/self/checkins'
|
|
8
|
+
token = nil || tokenval || ENV['oauth_token']
|
|
9
|
+
limit = 300
|
|
10
|
+
if token != nil then
|
|
11
|
+
@full_url = base_url + '?oauth_token=' + token + '&limit=' + limit.to_s + '&sort=oldestfirst&v=' + apiver
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def query(before=Time.now.to_i, after=Time.now.to_i - 31536000)
|
|
16
|
+
country_list = []
|
|
17
|
+
if @full_url != nil then
|
|
18
|
+
if before.to_i + 3600 > Time.now.to_i then
|
|
19
|
+
requested_url = @full_url + '&afterTimestamp=' + after.to_s + '&beforeTimestamp=' + before.to_s
|
|
20
|
+
after_timestamp = after
|
|
21
|
+
before_timestamp = before
|
|
22
|
+
begin
|
|
23
|
+
while (after_timestamp + 3600) < Time.now.to_i
|
|
24
|
+
requested_url = @full_url + '&afterTimestamp=' + after_timestamp.to_s + '&beforeTimestamp=' + before_timestamp.to_s
|
|
25
|
+
parsed_body = JSON.parse(RestClient.get(requested_url))
|
|
26
|
+
meta = parsed_body["meta"]
|
|
27
|
+
response = parsed_body["response"]
|
|
28
|
+
checkins = response["checkins"]["items"]
|
|
29
|
+
|
|
30
|
+
if meta["code"] == 200 then
|
|
31
|
+
checkins.each{|checkin|
|
|
32
|
+
if country_list.length == 0 then
|
|
33
|
+
country_list << {:visit => checkin["createdAt"].to_i, :code => checkin["venue"]["location"]["cc"].to_s, :name => checkin["venue"]["location"]["country"].to_s, length: 1}
|
|
34
|
+
else
|
|
35
|
+
if country_list[country_list.length - 1][:code] == checkin["venue"]["location"]["cc"] then
|
|
36
|
+
# Work out days between the last checkin
|
|
37
|
+
days = (checkin["createdAt"] - country_list[country_list.length - 1][:visit]) / 86400
|
|
38
|
+
if days == 0 then
|
|
39
|
+
days = 1
|
|
40
|
+
end
|
|
41
|
+
country_list[country_list.length - 1][:length] = days.to_i # Set the number of days stayed here
|
|
42
|
+
else
|
|
43
|
+
# Then just create a new entry
|
|
44
|
+
country_list << {:visit => checkin["createdAt"].to_i, :code => checkin["venue"]["location"]["cc"].to_s, :name => checkin["venue"]["location"]["country"].to_s, length: 1}
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
after_timestamp = checkin["createdAt"].to_i + 1800 # Set the createdAt to after Timestamp
|
|
48
|
+
#puts checkin["createdAt"]
|
|
49
|
+
#puts checkin["venue"]["location"]["cc"]
|
|
50
|
+
#puts country_list.inspect
|
|
51
|
+
#puts checkin["venue"]["location"]["country"]
|
|
52
|
+
#puts "---"
|
|
53
|
+
}
|
|
54
|
+
if checkins.length == 0 then
|
|
55
|
+
after_timestamp += 1800
|
|
56
|
+
end
|
|
57
|
+
#puts "Plus 1 hour: #{(after_timestamp + 3600).to_i} Current: #{Time.now.to_i}"
|
|
58
|
+
#puts "---"
|
|
59
|
+
|
|
60
|
+
else
|
|
61
|
+
{:status => "Non 200 response from API"}
|
|
62
|
+
break
|
|
63
|
+
end
|
|
64
|
+
# TODO: Set after_timestamp with the last checkin rather than 2419200 after the after_timestamp
|
|
65
|
+
#after_timestamp = after_timestamp + (2419200 * 3)
|
|
66
|
+
end
|
|
67
|
+
rescue
|
|
68
|
+
{:status => "Exception found #{$!}"}
|
|
69
|
+
end
|
|
70
|
+
{:status => "OK", countries: country_list}
|
|
71
|
+
else
|
|
72
|
+
{:status => "Before time is too soon"}
|
|
73
|
+
end
|
|
74
|
+
else
|
|
75
|
+
{:status => "Please set an 'oauth_token' and try again"}
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: dont-overstay
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Barry Teoh
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-03-21 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Useful Ruby gem for digital nomads for keeping under the 180 day rule
|
|
14
|
+
by working out how long was spent in each country that was visited in the last XXX
|
|
15
|
+
days
|
|
16
|
+
email: hello@barryteoh.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- lib/dont-overstay.rb
|
|
22
|
+
homepage: https://github.com/nolim1t/dont-overstay
|
|
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: '0'
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubyforge_project:
|
|
42
|
+
rubygems_version: 2.5.1
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 4
|
|
45
|
+
summary: Useful Ruby gem for digital nomads for keeping under the 180 day rule
|
|
46
|
+
test_files: []
|