Class: StravaWeb::Fetch

Inherits:
Object
  • Object
show all
Defined in:
lib/stravaweb/fetch.rb

Constant Summary collapse

AUTH_HELP =
"Set sync.strava.web_auth.jwt in config.yml — see docs/strava-web-auth.md".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jwt: nil, auth_seed: nil, cookie_path: nil) ⇒ Fetch

Returns a new instance of Fetch.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/stravaweb/fetch.rb', line 34

def initialize(jwt: nil, auth_seed: nil, cookie_path: nil)
  @cookie_path = cookie_path
  @jar = HTTP::CookieJar.new

  load_cookies

  jwt ||= decode_auth_seed(auth_seed) if auth_seed

  explicit = jwt || (auth_seed && !auth_seed.to_s.strip.empty?)

  if explicit
    @expires_at = decode_exp(jwt)
    (jwt)
  elsif authenticated?
    @expires_at = decode_exp(cookie_token)
    return
  else
    raise AuthError, "no web session — set jwt or auth_seed\n#{AUTH_HELP}"
  end

  save_cookies
end

Instance Attribute Details

#expires_atObject (readonly)

Returns the value of attribute expires_at.



32
33
34
# File 'lib/stravaweb/fetch.rb', line 32

def expires_at
  @expires_at
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
# File 'lib/stravaweb/fetch.rb', line 57

def authenticated?
  return false if @jar.cookies(URI(BASE_URL)).empty?

  resp = build_session.get("/me")
  resp.status == 302
rescue
  false
end

#export(activity_id, format: :original) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/stravaweb/fetch.rb', line 66

def export(activity_id, format: :original)
  raise ArgumentError, "unknown format: #{format.inspect}" unless VALID_FORMATS.include?(format)

  path = EXPORT_URLS[format.to_sym] % activity_id
  resp = build_session.get(path)

  unless resp.success?
    klass = (resp.status == 404) ? NotFoundError : ExportError
    raise klass, "HTTP #{resp.status} for activity #{activity_id}"
  end

  filename = extract_filename(resp, activity_id, format)
  fmt = infer_format(filename, format)

  ExportResult.new(filename: filename, content: resp.body, format: fmt)
end

#persist_cookies!Object



83
84
85
# File 'lib/stravaweb/fetch.rb', line 83

def persist_cookies!
  save_cookies
end