Class: Chronicle::Foursquare::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/chronicle/foursquare/proxy.rb

Constant Summary collapse

API_VERSION =
20_170_310

Instance Method Summary collapse

Constructor Details

#initialize(access_token:) ⇒ Proxy

Returns a new instance of Proxy.



8
9
10
# File 'lib/chronicle/foursquare/proxy.rb', line 8

def initialize(access_token:)
  @access_token = access_token
end

Instance Method Details

#load_checkin_page(limit: 50, offset: 0, since: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/chronicle/foursquare/proxy.rb', line 32

def load_checkin_page(limit: 50, offset: 0, since: nil)
  params = {
    limit: limit,
    offset: offset,
    afterTimestamp: since.to_i
  }

  load_endpoint('users/self/checkins', params)[:response][:checkins][:items]
end

#load_checkins(since: nil, limit: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/chronicle/foursquare/proxy.rb', line 12

def load_checkins(since: nil, limit: nil)
  has_more = true
  visits = []
  count = 0

  while has_more
    results = load_checkin_page(limit: 50, offset: count, since: since)
    results = results.first(limit - count) if limit
    visits += results

    count += results.length
    has_more = results.any?
  end
  visits
end

#load_endpoint(endpoint, params = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chronicle/foursquare/proxy.rb', line 42

def load_endpoint(endpoint, params = {})
  params = params.merge({
    oauth_token: @access_token,
    v: API_VERSION
  })

  conn = Faraday.new(
    url: 'https://api.foursquare.com/v2/',
    params: params
  )

  response = conn.get(endpoint)
  JSON.parse(response.body, { symbolize_names: true })
end

#load_selfObject



28
29
30
# File 'lib/chronicle/foursquare/proxy.rb', line 28

def load_self
  load_endpoint('users/self')[:response][:user]
end