dexcom_share_api 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 40943264dd61ec2ce957a1617899581d73f2a66f2d088a601aae0a0dd1180b52
4
+ data.tar.gz: aa10acce28d4aab248794f10f686b1e70ea6c1d852b941c370fb50d36302bd08
5
+ SHA512:
6
+ metadata.gz: 02ecbe4de4148b9bbd39706f861fa17847bb35c7ca3c3fc2e2ef35834dd97701c59f4683b343ec14466e4536fddac8472bce05c1f0435b461ca3dfb6d4644daa
7
+ data.tar.gz: b9c8f474f03ea7c342da6193475567ebbbd81d5e2928743bf148c1c4104ee9db84b0c941e1b2bc694d2774b8be6adc4b58908da8a7f0afcb0c868f6dfef092a9
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in dexcom_share_api.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ dexcom_share_api (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.8.6)
10
+ public_suffix (>= 2.0.2, < 6.0)
11
+ bigdecimal (3.1.6)
12
+ byebug (11.1.3)
13
+ coderay (1.1.3)
14
+ crack (1.0.0)
15
+ bigdecimal
16
+ rexml
17
+ hashdiff (1.1.0)
18
+ method_source (1.0.0)
19
+ minitest (5.22.2)
20
+ pry (0.14.1)
21
+ coderay (~> 1.1)
22
+ method_source (~> 1.0)
23
+ pry-byebug (3.10.1)
24
+ byebug (~> 11.0)
25
+ pry (>= 0.13, < 0.15)
26
+ public_suffix (5.0.4)
27
+ rake (10.5.0)
28
+ rexml (3.2.6)
29
+ vcr (6.2.0)
30
+ webmock (3.23.0)
31
+ addressable (>= 2.8.0)
32
+ crack (>= 0.3.2)
33
+ hashdiff (>= 0.4.0, < 2.0.0)
34
+
35
+ PLATFORMS
36
+ arm64-darwin-21
37
+ arm64-darwin-23
38
+
39
+ DEPENDENCIES
40
+ bundler
41
+ dexcom_share_api!
42
+ minitest (~> 5.0)
43
+ pry-byebug
44
+ rake (~> 10.0)
45
+ vcr
46
+ webmock
47
+
48
+ BUNDLED WITH
49
+ 2.4.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Elliot Dohm
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ TLDR; This is a tiny gem to interact with the Dexcom Share API. The API is
2
+ (unfortunately) private, so _any_ implementation can't really have stability
3
+ guarantees. However, it hasn't changed for the past few years so it should be
4
+ fairly stable. Feel free to report any bugs and I'll fix them!
5
+
6
+ ## Usage
7
+ ```ruby
8
+ require "dexcom_share_api"
9
+
10
+ # Create a client with your username and password from Dexcom Share.
11
+ # If you get authentication issues, try to confirm your username/password by
12
+ # logging into Dexcom Share directly.
13
+ client = DexcomShareApi.create_client(
14
+ username: "Dexcom share username",
15
+ password: "Dexcom share password",
16
+ # If your account is registered in the US, otherwise `outside-us`.
17
+ server: "us",
18
+ )
19
+
20
+ # Defaults to outputting the last 10 entries
21
+ # Optionally can pass in the _last_ parameter.
22
+ # ```
23
+ # client.estimated_glucose(last: 5)
24
+ # ```
25
+ glucose = client.estimated_glucose
26
+ glucose.each do |entry|
27
+ puts entry.mmol # => 5.39
28
+ puts entry.mgdl # => 151.0
29
+ puts entry.trend # => "flat"
30
+ # Timestamp is an iso8601
31
+ puts entry.timestamp # => "2024-03-06T04:14:53+00:00"
32
+ end
33
+
34
+ # Alternatively, to grab the last entry:
35
+ glucose = client.last_estimated_glucose
36
+ puts glucose.to_h # => {:trend=>"flat", :timestamp=>"2024-03-06T04:14:53+00:00", :mmol=>8.39, :mgdl=>151.0}
37
+ ```
38
+
39
+ ## History
40
+ I originally made this library in
41
+ [JavaScript](https://github.com/aud/dexcom-share-api) many years ago to enable
42
+ some watch-face development. Recently I had a use-case where I wanted to store
43
+ a bunch of Dexcom data in a database for some offline analysis, and decided I'd
44
+ port it to Ruby to make that process more enjoyable.
45
+
46
+ ## Development
47
+
48
+ To test:
49
+ * `bin/test`
50
+
51
+ To build:
52
+ * `gem build && gem install dexcom_share_api`
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "dexcom_share_api"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/test ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env bash
2
+ set -x
3
+ bundle exec rake test TEST="test/dexcom_share_api_test.rb"
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "dexcom_share_api/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "dexcom_share_api"
7
+ spec.version = DexcomShareApi::VERSION
8
+ spec.authors = ["Elliot Dohm"]
9
+ spec.email = ["elliotdohm+rubygems@gmail.com"]
10
+
11
+ spec.summary = "Tiny API layer for interacting with Dexcom Share API"
12
+ spec.description = "Tiny API layer for interacting with Dexcom Share API"
13
+ spec.homepage = "https://github.com/aud/dexcom_share_ruby"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler"
26
+ spec.add_development_dependency "pry-byebug"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "vcr"
29
+ spec.add_development_dependency "webmock"
30
+ spec.add_development_dependency "minitest", "~> 5.0"
31
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dexcom_share_api/http_api"
4
+ require "dexcom_share_api/glucose"
5
+
6
+ module DexcomShareApi
7
+ class Client
8
+ attr_reader(:dexcom_api)
9
+
10
+ def initialize(username:, password:, server:)
11
+ @dexcom_api ||= HttpApi.new(username:, password:, server:)
12
+ end
13
+
14
+ def estimated_glucose(last: 10)
15
+ result = dexcom_api.fetch_estimated_glucose!(last:)
16
+
17
+ result
18
+ .map { |entry| Glucose.new(entry) }
19
+ .reverse
20
+ end
21
+
22
+ def last_estimated_glucose
23
+ result = dexcom_api.fetch_estimated_glucose!(last: 1)
24
+
25
+ Glucose.new(result[0])
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module DexcomShareApi
6
+ class Glucose
7
+ # {
8
+ # "WT"=>"Date(1709620791000)",
9
+ # "ST"=>"Date(1709620791000)",
10
+ # "DT"=>"Date(1709620791000-0500)",
11
+ # "Value"=>164,
12
+ # "Trend"=>"Flat",
13
+ # }
14
+ def initialize(raw_glucose_data)
15
+ @raw_glucose_data = raw_glucose_data
16
+ end
17
+
18
+ def to_h
19
+ {
20
+ trend:,
21
+ timestamp:,
22
+ mmol:,
23
+ mgdl:,
24
+ }
25
+ end
26
+
27
+ def inspect
28
+ out = "#<#{self.class}:#{object_id}"
29
+ to_h.each do |key, value|
30
+ out << " @#{key}=#{value}"
31
+ end
32
+ out << ">"
33
+ out
34
+ end
35
+
36
+ def trend
37
+ @raw_glucose_data["Trend"].split(/(?=[A-Z])/).join("-").downcase
38
+ end
39
+
40
+ # This is parsing out an epoch time from "Date(1709620791000)". It's messy.
41
+ def timestamp
42
+ raw_timestamp = @raw_glucose_data["ST"].match(/\d+/)[0]
43
+ DateTime.strptime(raw_timestamp, '%Q').iso8601
44
+ end
45
+
46
+ def mmol
47
+ mgdl_to_mmol(mgdl)
48
+ end
49
+
50
+ def mgdl
51
+ @raw_glucose_data["Value"].to_f
52
+ end
53
+
54
+ private
55
+
56
+ # http://www.bcchildrens.ca/endocrinology-diabetes-site/documents/glucoseunits.pdf
57
+ # [BG (mmol/L) * 18] = BG (mg/dL)
58
+ #
59
+ # Return the normalized mmol/L
60
+ def mgdl_to_mmol(mgdl)
61
+ (mgdl.to_f / 18).round(2)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+ require "dexcom_share_api/glucose"
6
+
7
+ module DexcomShareApi
8
+ class HttpApi
9
+ InvalidServerError = Class.new(ArgumentError)
10
+ ApiError = Class.new(StandardError)
11
+
12
+ # This server needs to be either "us" or "eu. If you're in the US, the server
13
+ # should be "us". Any other country outside of the US (eg. Canada) is
14
+ # classified as "eu" by Dexcom.
15
+ VALID_SERVERS = ["us", "outside-us"]
16
+
17
+ # This seems to be a blessed ID the Dexcom Share app uses. This is in no way
18
+ # special to this library, is it used in several (nightscout, dexcomedy,
19
+ # etc).
20
+ APPLICATION_ID = "d8665ade-9673-4e27-9ff6-92db4ce13d13"
21
+
22
+ def initialize(username:, password:, server:)
23
+ validate_server!(server)
24
+
25
+ @username = username
26
+ @password = password
27
+ @server = server
28
+ end
29
+
30
+ def fetch_estimated_glucose!(last:, minutes: 24 * 60)
31
+ session_id = fetch_session_id!
32
+
33
+ uri = URI::HTTPS.build(
34
+ host: base_host,
35
+ path: "/ShareWebServices/Services/Publisher/ReadPublisherLatestGlucoseValues",
36
+ )
37
+
38
+ data = {
39
+ maxCount: last,
40
+ sessionId: session_id,
41
+ minutes: minutes,
42
+ }
43
+
44
+ dexcom_request!(uri, data)
45
+ end
46
+
47
+ def fetch_session_id!
48
+ account_id = fetch_account_id!
49
+
50
+ uri = URI::HTTPS.build(
51
+ host: base_host,
52
+ path: "/ShareWebServices/Services/General/LoginPublisherAccountById",
53
+ )
54
+
55
+ data = {
56
+ applicationId: APPLICATION_ID,
57
+ accountId: account_id,
58
+ password: @password,
59
+ }
60
+
61
+ dexcom_request!(uri, data)
62
+ end
63
+
64
+ def fetch_account_id!
65
+ uri = URI::HTTPS.build(
66
+ host: base_host,
67
+ path: "/ShareWebServices/Services/General/AuthenticatePublisherAccount",
68
+ )
69
+
70
+ data = {
71
+ applicationId: APPLICATION_ID,
72
+ accountName: @username,
73
+ password: @password,
74
+ }
75
+
76
+ dexcom_request!(uri, data)
77
+ end
78
+
79
+ private
80
+
81
+ def dexcom_request!(uri, data)
82
+ request = Net::HTTP::Post.new(
83
+ uri.request_uri,
84
+ {
85
+ "Content-Type" => "application/json",
86
+ }
87
+ )
88
+
89
+ request.body = data.to_json
90
+
91
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
92
+ http.request(request)
93
+ end
94
+
95
+ body = JSON.parse(response.body)
96
+
97
+ if response.code.to_i != 200
98
+ err_msg = <<~MSG
99
+ Error! Dexcom request to "#{uri}" failed.
100
+
101
+ Response code: #{response.code}
102
+ Raw request body:
103
+ `Code=#{body["Code"]}`
104
+ `Message=#{body["Message"]}`
105
+ MSG
106
+
107
+ raise(ApiError, err_msg)
108
+ end
109
+
110
+ body
111
+ rescue ApiError => err
112
+ raise
113
+ rescue => err
114
+ raise(ApiError, err)
115
+ end
116
+
117
+ def base_host
118
+ case @server
119
+ when "us"
120
+ "share2.dexcom.com"
121
+ when "outside-us"
122
+ "shareous1.dexcom.com"
123
+ end
124
+ end
125
+
126
+ def validate_server!(server)
127
+ return if VALID_SERVERS.include?(server)
128
+
129
+ err_msg = <<~MSG
130
+ Server must either be `us` or `outside-us`.
131
+
132
+ If you're in the US, the server should be "us". Any other country
133
+ outside of the US (eg. Canada) should be "outside-us".
134
+ MSG
135
+
136
+ raise(InvalidServerError, err_msg)
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DexcomShareApi
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dexcom_share_api/version"
4
+ require "dexcom_share_api/client"
5
+
6
+ module DexcomShareApi
7
+ def self.create_client(...)
8
+ DexcomShareApi::Client.new(...)
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dexcom_share_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Elliot Dohm
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry-byebug
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: vcr
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: Tiny API layer for interacting with Dexcom Share API
98
+ email:
99
+ - elliotdohm+rubygems@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - bin/console
111
+ - bin/setup
112
+ - bin/test
113
+ - dexcom_share_api.gemspec
114
+ - lib/dexcom_share_api.rb
115
+ - lib/dexcom_share_api/client.rb
116
+ - lib/dexcom_share_api/glucose.rb
117
+ - lib/dexcom_share_api/http_api.rb
118
+ - lib/dexcom_share_api/version.rb
119
+ homepage: https://github.com/aud/dexcom_share_ruby
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubygems_version: 3.3.26
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Tiny API layer for interacting with Dexcom Share API
142
+ test_files: []