Class: StravaExport::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/strava-export/client.rb

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Client.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/strava-export/client.rb', line 32

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
    (jwt)
  elsif authenticated?
    return
  else
    raise AuthError, "no web session — set jwt or auth_seed\n#{AUTH_HELP}"
  end

  save_cookies
end

Instance Method Details

#authenticated?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/strava-export/client.rb', line 53

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)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/strava-export/client.rb', line 62

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



79
80
81
# File 'lib/strava-export/client.rb', line 79

def persist_cookies!
  save_cookies
end