Class: StravaWebLib::Client
- Inherits:
-
Object
- Object
- StravaWebLib::Client
- Defined in:
- lib/stravaweblib/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
- #authenticated? ⇒ Boolean
- #export(activity_id, format: :original) ⇒ Object
-
#initialize(jwt: nil, auth_seed: nil, cookie_path: nil) ⇒ Client
constructor
A new instance of Client.
- #persist_cookies! ⇒ Object
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/stravaweblib/client.rb', line 32 def initialize(jwt: nil, auth_seed: nil, cookie_path: nil) @cookie_path = @jar = HTTP::CookieJar.new jwt ||= decode_auth_seed(auth_seed) if auth_seed explicit = jwt || (auth_seed && !auth_seed.to_s.strip.empty?) if explicit login_with_jwt(jwt) elsif authenticated? return else raise AuthError, "no web session — set jwt or auth_seed\n#{AUTH_HELP}" end end |
Instance Method Details
#authenticated? ⇒ Boolean
53 54 55 56 57 58 59 60 |
# File 'lib/stravaweblib/client.rb', line 53 def authenticated? return false if @jar.(URI(BASE_URL)).empty? resp = build_session.get("/me") resp.status == 302 rescue false end |
#export(activity_id, format: :original) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/stravaweblib/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/stravaweblib/client.rb', line 79 def end |