Class: Daytracker
- Inherits:
-
Object
- Object
- Daytracker
- Defined in:
- lib/dont-overstay.rb
Instance Method Summary collapse
-
#initialize(tokenval = nil) ⇒ Daytracker
constructor
A new instance of Daytracker.
- #query(before = Time.now.to_i, after = ((Date.today - 365).to_time.to_i), padded_days = 0) ⇒ Object
Constructor Details
#initialize(tokenval = nil) ⇒ Daytracker
Returns a new instance of Daytracker.
5 6 7 8 9 10 11 12 13 |
# File 'lib/dont-overstay.rb', line 5 def initialize(tokenval=nil) apiver = '20160226' base_url = 'https://api.foursquare.com/v2/users/self/checkins' token = nil || tokenval || ENV['oauth_token'] limit = 300 if token != nil then @full_url = base_url + '?oauth_token=' + token + '&limit=' + limit.to_s + '&sort=oldestfirst&v=' + apiver end end |
Instance Method Details
#query(before = Time.now.to_i, after = ((Date.today - 365).to_time.to_i), padded_days = 0) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dont-overstay.rb', line 15 def query(before=Time.now.to_i, after=((Date.today - 365).to_time.to_i), padded_days=0) if ENV['dryrun'] then if ENV['dryrun'] == "true" then return {:status => "Test", before: before, after: after, padded_days: (padded_days || 0)} end end country_list = [] if @full_url != nil then if before.to_i + 3600 > Time.now.to_i then requested_url = @full_url + '&afterTimestamp=' + after.to_s + '&beforeTimestamp=' + before.to_s = after = before last_country_code = "" last_country_name = "" begin while ( + 3600) < Time.now.to_i requested_url = @full_url + '&afterTimestamp=' + .to_s + '&beforeTimestamp=' + .to_s parsed_body = JSON.parse(RestClient.get(requested_url)) = parsed_body["meta"] response = parsed_body["response"] checkins = response["checkins"]["items"] if ["code"] == 200 then checkins.each{|checkin| if country_list.length == 0 then country_list << {:visit => checkin["createdAt"].to_i, :code => checkin["venue"]["location"]["cc"].to_s, :name => checkin["venue"]["location"]["country"].to_s, length: padded_days.to_i} else if country_list[country_list.length - 1][:code] == checkin["venue"]["location"]["cc"] then # Work out days between the last checkin days = (checkin["createdAt"] - country_list[country_list.length - 1][:visit]) / 86400 if days == 0 then days = 1 end country_list[country_list.length - 1][:length] = days.to_i # Set the number of days stayed here else # Then just create a new entry country_list << {:visit => checkin["createdAt"].to_i, :code => checkin["venue"]["location"]["cc"].to_s, :name => checkin["venue"]["location"]["country"].to_s, length: padded_days.to_i} end end = checkin["createdAt"].to_i + 1800 # Set the createdAt to after Timestamp last_country_code = checkin["venue"]["location"]["cc"].to_s last_country_name = checkin["venue"]["location"]["country"].to_s } if checkins.length == 0 then += 1800 end #puts "Plus 1 hour: #{(after_timestamp + 3600).to_i} Current: #{Time.now.to_i}" #puts "---" else {:status => "Non 200 response from API"} break end # TODO: Set after_timestamp with the last checkin rather than 2419200 after the after_timestamp #after_timestamp = after_timestamp + (2419200 * 3) end rescue {:status => "Exception found #{$!}"} end days_since_last_ts = (Time.now.to_i - .to_i).abs / 86400 {:status => "OK", countries: country_list, last_seen: {days: days_since_last_ts, country: {code: last_country_code, name: last_country_name}}} else {:status => "Before time is too soon"} end else {:status => "Please set an 'oauth_token' and try again"} end end |