fitbyte 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +15 -9
- data/fitbyte.gemspec +1 -1
- data/lib/fitbyte/client.rb +1 -1
- data/lib/fitbyte/helpers.rb +18 -7
- data/lib/fitbyte/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4fc521fa00248d4a2349cbf48840ca97bc567e5d
|
4
|
+
data.tar.gz: e7d534a59e218901e7c81b36e4ad8cf4a2a7712c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3af70dae21133cf999be7567e23859fa5d16edf7179f7cb86c55a3be0287737edb3ee1789261df5d6c472a57731d5ad10a08c9bdef46272e2eabfd3bdbdb89a9
|
7
|
+
data.tar.gz: 172a7eff592f180bcf973d949d67aa934b026df2ab300adfbf4b855f00fc9afe4c617f61cd37f583cd43918eb096666bd97315a059d358f86d5612fcd1e4c169
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,9 @@
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/fitbyte.svg)](https://badge.fury.io/rb/fitbyte)
|
4
4
|
[![Build Status](https://travis-ci.org/zokioki/fitbyte.svg?branch=master)](https://travis-ci.org/zokioki/fitbyte)
|
5
5
|
|
6
|
-
|
6
|
+
Fitbyte allows interaction with [Fitbit's REST API](https://dev.fitbit.com/docs/basics/).
|
7
|
+
|
8
|
+
It was started as a small personal project to provide a Ruby API interface using OAuth2, after Fitbit announced the eventual drop of OAuth1 support for the developer API.
|
7
9
|
|
8
10
|
## Installation
|
9
11
|
|
@@ -36,7 +38,7 @@ client.auth_page_link
|
|
36
38
|
# => https://fitbit.com/oauth2/authorize?client_id=123XYZ&redirect_uri=...
|
37
39
|
```
|
38
40
|
|
39
|
-
- Follow generated link to Fitbit's authorization page. After approving your app, you're sent to the `redirect_uri`, with an appended authorization `code` param, which you'll exchange for an access token:
|
41
|
+
- Follow the generated link to Fitbit's authorization page. After approving your app, you're sent to the `redirect_uri`, with an appended authorization `code` param, which you'll exchange for an access token:
|
40
42
|
|
41
43
|
```ruby
|
42
44
|
client.get_token(auth_code)
|
@@ -49,35 +51,39 @@ client.food_logs Date.today
|
|
49
51
|
# => { "foods" => [{ "isFavorite" => true, "logDate" => "2015-06-26", "logId" => 1820, "loggedFood" => { "accessLevel" => "PUBLIC", "amount" => 132.57, "brand" => "", "calories" => 752, ...}] }
|
50
52
|
```
|
51
53
|
|
52
|
-
To
|
54
|
+
To make the response more easily suited for attribute-assignment, it can be parsed to return a hash whose keys are in snake_case format. This can be done by setting the `snake_case` option to `true`, like so:
|
53
55
|
|
54
56
|
```ruby
|
55
57
|
client.food_logs Date.today, snake_case: true
|
56
58
|
# => { "foods" => [{ "is_favorite" => true, "log_date" => "2015-06-26", "log_id" => 1820, "logged_food" => { "access_level" => "PUBLIC", "amount" => 132.57, "brand" => "", "calories" => 752, ...}] }
|
57
59
|
```
|
58
60
|
|
61
|
+
Similarly, all arguments passed in through a POST request are automatically converted to camelCase before they hit Fitbit's API, making it easy to keep your codebase stylistically consistent. For example, all of the following would result in valid API calls:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
client.log_activity activity_id: 12345, duration_millis: "50000"
|
65
|
+
client.log_activity activityId: 54321, durationMillis: "44100"
|
66
|
+
# If for some reason you had to mix snake and camel case like below,
|
67
|
+
# Fitbyte would make sure the result is a validly formatted request
|
68
|
+
client.log_activity activity_id: 12345, durationMillis: "683300"
|
69
|
+
```
|
70
|
+
|
59
71
|
### Options
|
60
72
|
|
61
73
|
When initializing a `Fitbyte::Client` instance, you're given access to a handful of options:
|
62
74
|
|
63
75
|
- `:api_version` - API version to be used when making requests (default: "1")
|
64
76
|
|
65
|
-
---
|
66
77
|
- `:unit_system` - The measurement unit system to use for response values (default: "en_US" | available: "en_US", "en_GB", and "any" for metric)
|
67
78
|
|
68
|
-
---
|
69
79
|
- `:locale` - The locale to use for response values (default: "en_US" | available: "en_US", "fr_FR", "de_DE", "es_ES", "en_GB", "en_AU", "en_NZ" and "ja_JP")
|
70
80
|
|
71
|
-
---
|
72
81
|
- `:scope` - A space-delimited list of the permissions you are requesting (default: "activity nutrition profile settings sleep social weight" | available: "activity", "heartrate", "location", "nutrition", "profile", "settings" "sleep", "social" and "weight")
|
73
82
|
|
74
|
-
---
|
75
83
|
- `:snake_case` - Transform returned object's keys to snake case format (default: false)
|
76
84
|
|
77
|
-
---
|
78
85
|
- `:symbolize_keys` - Transform returned object's keys to symbols (default: false)
|
79
86
|
|
80
|
-
---
|
81
87
|
|
82
88
|
## License
|
83
89
|
|
data/fitbyte.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Zoran Pesic"]
|
10
10
|
spec.email = ["zoran1991@gmail.com"]
|
11
11
|
|
12
|
-
spec.summary = %q{
|
12
|
+
spec.summary = %q{A Ruby interface to the Fitbit API, using OAuth2.}
|
13
13
|
spec.homepage = Fitbyte::REPO_URL
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
data/lib/fitbyte/client.rb
CHANGED
@@ -77,7 +77,7 @@ module Fitbyte
|
|
77
77
|
end
|
78
78
|
|
79
79
|
def post(path, opts={})
|
80
|
-
response = token.post(("#{@api_version}/" + path), body: opts, headers: request_headers).response
|
80
|
+
response = token.post(("#{@api_version}/" + path), body: deep_keys_to_camel_case!(opts), headers: request_headers).response
|
81
81
|
object = MultiJson.load(response.body) unless response.status == 204
|
82
82
|
process_keys!(object, opts)
|
83
83
|
end
|
data/lib/fitbyte/helpers.rb
CHANGED
@@ -19,16 +19,19 @@ module Fitbyte
|
|
19
19
|
scope.is_a?(Array) ? scope.join(" ") : scope
|
20
20
|
end
|
21
21
|
|
22
|
-
# Borrowing from Rails
|
23
|
-
|
24
22
|
def deep_keys_to_snake_case!(object)
|
25
23
|
deep_transform_keys!(object) { |key| to_snake_case(key) }
|
26
24
|
end
|
27
25
|
|
26
|
+
def deep_keys_to_camel_case!(object)
|
27
|
+
deep_transform_keys!(object) { |key| to_camel_case(key, lower: true) }
|
28
|
+
end
|
29
|
+
|
28
30
|
def deep_symbolize_keys!(object)
|
29
31
|
deep_transform_keys!(object) { |key| key.to_sym rescue key }
|
30
32
|
end
|
31
33
|
|
34
|
+
# Inspired by ActiveSupport's implementation
|
32
35
|
def deep_transform_keys!(object, &block)
|
33
36
|
case object
|
34
37
|
when Hash
|
@@ -45,11 +48,19 @@ module Fitbyte
|
|
45
48
|
end
|
46
49
|
|
47
50
|
def to_snake_case(word)
|
48
|
-
|
49
|
-
return
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
string = word.to_s.dup
|
52
|
+
return string.downcase if string.match(/\A[A-Z]+\z/)
|
53
|
+
string.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
|
54
|
+
string.gsub!(/([a-z])([A-Z])/, '\1_\2')
|
55
|
+
string.downcase
|
56
|
+
end
|
57
|
+
|
58
|
+
def to_camel_case(word, opts={})
|
59
|
+
string = word.to_s
|
60
|
+
return string if string.match(/[A-Z]|[a-z]([A-Z0-9]*[a-z][a-z0-9]*[A-Z]|[a-z0-9]*[A-Z][A-Z0-9]*[a-z])[A-Za-z0-9]*/)
|
61
|
+
string = word.to_s.split("_").collect(&:capitalize).join
|
62
|
+
string.gsub!(/^\w{1}/) { |word| word.downcase } if opts[:lower]
|
63
|
+
return string
|
53
64
|
end
|
54
65
|
|
55
66
|
end
|
data/lib/fitbyte/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fitbyte
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zoran Pesic
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oauth2
|
@@ -121,5 +121,5 @@ rubyforge_project:
|
|
121
121
|
rubygems_version: 2.5.1
|
122
122
|
signing_key:
|
123
123
|
specification_version: 4
|
124
|
-
summary:
|
124
|
+
summary: A Ruby interface to the Fitbit API, using OAuth2.
|
125
125
|
test_files: []
|