fitbyte 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 188fb792651e2f58e5b51a49dfb3bbe76e1041f5
4
- data.tar.gz: 157ba5a0dc5568fa3ebab5fa928c8247a48af471
3
+ metadata.gz: f683b1fe97c314e07e53a983be399fff5ea30787
4
+ data.tar.gz: 8d9701b9aa3f08a74a05f76a81dfcbe212387dfa
5
5
  SHA512:
6
- metadata.gz: fb907a686839076b47a7913a7aab8a1d55e02d3a081b7f758ad89eed182b9302ae6c8ffa99f7d0a96f142387b6769a9795ffaec05c915baaff2e24f019b18dbc
7
- data.tar.gz: f4537f148799ef45a6e028cbbf74d3db11f5d887230145b59938bae90eb6a5e956ec75df732267cecb56953b871f06ba28e6675c2ae791b0a606860abba86f92
6
+ metadata.gz: 2cb40bea88a84370e8af28cc981750274f67521bfba24b84380435679b58b6de03cc3ccc02f045441e9012c6240d2af850131f4f98b75d5e02fe3fe675d7cb83
7
+ data.tar.gz: e19b133f2d0d4ed7276ca7e116e0e2934739c08e2a5e7b849a24977fa24760b2c29ab7e911ed98503a6ef33999d25d300b83e0a3bd2229479d7aff676f21d4d2
@@ -1,3 +1,10 @@
1
+ 0.4.0
2
+ -----
3
+ - Remove FitStruct objects
4
+ - The response's hash keys can now be transformed using the following options:
5
+ - `snake_case` - if set to true, all keys are formatted to snake_case
6
+ - `symbolize_keys` - if set to true, all keys are converted to symbols
7
+
1
8
  0.3.0
2
9
  -----
3
10
  - API endpoint support for the following actions:
data/README.md CHANGED
@@ -5,8 +5,6 @@
5
5
 
6
6
  This gem allows interaction with [Fitbit's REST API](https://dev.fitbit.com/docs/basics/).
7
7
 
8
- **NOTE:** Fitbit's API is currently in beta, and is in active development. Breaking changes to certain endpoints may be introduced during early development of this gem, until Fitbit's API solidifies.
9
-
10
8
  ## Installation
11
9
 
12
10
  To install the latest release:
@@ -48,16 +46,14 @@ You're now authenticated and can make calls to Fitbit's API:
48
46
 
49
47
  ```ruby
50
48
  client.food_logs Date.today
51
- # => #<Fitbyte::FitStruct foods=[#<Fitbyte::FitStruct isFavorite=true, logDate="2015-06-26", logId=1820, loggedFood=#<Fitbyte::FitStruct accessLevel="PUBLIC", amount=132.57, brand="", calories=752, ...]
49
+ # => { "foods" => [{ "isFavorite" => true, "logDate" => "2015-06-26", "logId" => 1820, "loggedFood" => { "accessLevel" => "PUBLIC", "amount" => 132.57, "brand" => "", "calories" => 752, ...}] }
52
50
  ```
53
51
 
54
- If your JSON library allows, the default format for resulting data returns OpenStruct-based FitStruct objects, allowing for more convenient method-like attribute access.
55
-
56
- To return the original JSON, `raw: true` can be specified as an option:
52
+ To return the hash keys in snake_case format, the `snake_case: true` option can be specified:
57
53
 
58
54
  ```ruby
59
- client.food_logs Date.today, raw: true
60
- # => { :foods => [{ :isFavorite => true, :logDate => "2015-06-26", :logId => 1820, :loggedFood => { :accessLevel => "PUBLIC", :amount => 132.57, :brand => "", :calories => 752, ...}] }
55
+ client.food_logs Date.today, snake_case: true
56
+ # => { "foods" => [{ "is_favorite" => true, "log_date" => "2015-06-26", "log_id" => 1820, "logged_food" => { "access_level" => "PUBLIC", "amount" => 132.57, "brand" => "", "calories" => 752, ...}] }
61
57
  ```
62
58
 
63
59
  ### Options
@@ -76,7 +72,10 @@ When initializing a `Fitbyte::Client` instance, you're given access to a handful
76
72
  - `: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")
77
73
 
78
74
  ---
79
- - `:raw_response` - Setting this option to `true` returns response values on subsequent calls as parsed JSON; by default, response values use FitStruct objects, for convenient method-like attribute access (default: false | available: true, false)
75
+ - `:snake_case` - Transform returned object's keys to snake case format (default: false)
76
+
77
+ ---
78
+ - `:symbolize_keys` - Transform returned object's keys to symbols (default: false)
80
79
 
81
80
  ---
82
81
 
@@ -1,4 +1,3 @@
1
- require "fitbyte/fitstruct"
2
1
  require "fitbyte/helpers"
3
2
  require "fitbyte/activities"
4
3
  require "fitbyte/goals"
@@ -13,7 +12,7 @@ require "fitbyte/water"
13
12
 
14
13
  module Fitbyte
15
14
  class Client
16
- attr_accessor :api_version, :unit_system, :locale, :scope, :raw_response
15
+ attr_accessor :api_version, :unit_system, :locale, :scope, :snake_case, :symbolize_keys
17
16
 
18
17
  def initialize(opts)
19
18
  missing_args = [:client_id, :client_secret, :redirect_uri] - opts.keys
@@ -34,7 +33,8 @@ module Fitbyte
34
33
  @scope = format_scope(opts[:scope])
35
34
 
36
35
  @api_version = opts[:api_version]
37
- @raw_response = opts[:raw_response]
36
+ @snake_case = opts[:snake_case]
37
+ @symbolize_keys = opts[:symbolize_keys]
38
38
 
39
39
  @client = OAuth2::Client.new(@client_id, @client_secret, site: @site_url,
40
40
  authorize_url: @authorize_url, token_url: @token_url)
@@ -71,21 +71,27 @@ module Fitbyte
71
71
  end
72
72
 
73
73
  def get(path, opts={})
74
- raw = opts[:raw].nil? ? @raw_response : opts[:raw]
75
74
  response = token.get(("#{@api_version}/" + path), headers: request_headers).response
76
- MultiJson.load(response.body, symbolize_keys: true, object_class: (FitStruct unless raw)) unless response.status == 204
75
+ object = MultiJson.load(response.body) unless response.status == 204
76
+ process_keys!(object, opts)
77
77
  end
78
78
 
79
79
  def post(path, opts={})
80
- raw = opts[:raw].nil? ? @raw_response : opts[:raw]
81
80
  response = token.post(("#{@api_version}/" + path), body: opts, headers: request_headers).response
82
- MultiJson.load(response.body, symbolize_keys: true, object_class: (FitStruct unless raw)) unless response.status == 204
81
+ object = MultiJson.load(response.body) unless response.status == 204
82
+ process_keys!(object, opts)
83
83
  end
84
84
 
85
85
  def delete(path, opts={})
86
- raw = opts[:raw].nil? ? @raw_response : opts[:raw]
87
86
  response = token.delete(("#{@api_version}/" + path), headers: request_headers).response
88
- MultiJson.load(response.body, symbolize_keys: true, object_class: (FitStruct unless raw)) unless response.status == 204
87
+ object = MultiJson.load(response.body) unless response.status == 204
88
+ process_keys!(object, opts)
89
+ end
90
+
91
+ def process_keys!(object, opts={})
92
+ deep_keys_to_snake_case!(object) if (opts[:snake_case] || snake_case)
93
+ deep_symbolize_keys!(object) if (opts[:symbolize_keys] || symbolize_keys)
94
+ return object
89
95
  end
90
96
 
91
97
  def defaults
@@ -97,7 +103,8 @@ module Fitbyte
97
103
  unit_system: "en_US",
98
104
  locale: "en_US",
99
105
  api_version: "1",
100
- raw_response: false
106
+ snake_case: false,
107
+ symbolize_keys: false
101
108
  }
102
109
  end
103
110
  end
@@ -1,5 +1,6 @@
1
1
  module Fitbyte
2
2
  class Client
3
+
3
4
  def format_date(date)
4
5
  if [Date, Time, DateTime].include?(date.class)
5
6
  date.strftime("%Y-%m-%d")
@@ -17,5 +18,39 @@ module Fitbyte
17
18
  def format_scope(scope)
18
19
  scope.is_a?(Array) ? scope.join(" ") : scope
19
20
  end
21
+
22
+ # Borrowing from Rails
23
+
24
+ def deep_keys_to_snake_case!(object)
25
+ deep_transform_keys!(object) { |key| to_snake_case(key) }
26
+ end
27
+
28
+ def deep_symbolize_keys!(object)
29
+ deep_transform_keys!(object) { |key| key.to_sym rescue key }
30
+ end
31
+
32
+ def deep_transform_keys!(object, &block)
33
+ case object
34
+ when Hash
35
+ object.keys.each do |key|
36
+ value = object.delete(key)
37
+ object[yield(key)] = deep_transform_keys!(value) { |key| yield(key) }
38
+ end
39
+ object
40
+ when Array
41
+ object.map! { |e| deep_transform_keys!(e) { |key| yield(key) } }
42
+ else
43
+ object
44
+ end
45
+ end
46
+
47
+ def to_snake_case(word)
48
+ word = word.to_s.dup
49
+ return word.downcase if word.match(/\A[A-Z]+\z/)
50
+ word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
51
+ word.gsub!(/([a-z])([A-Z])/, '\1_\2')
52
+ word.downcase
53
+ end
54
+
20
55
  end
21
56
  end
@@ -1,4 +1,4 @@
1
1
  module Fitbyte
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  REPO_URL = "https://github.com/zokioki/fitbyte"
4
4
  end
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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zoran Pesic
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-12-07 00:00:00.000000000 Z
11
+ date: 2016-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oauth2
@@ -90,7 +90,6 @@ files:
90
90
  - lib/fitbyte/body.rb
91
91
  - lib/fitbyte/client.rb
92
92
  - lib/fitbyte/devices.rb
93
- - lib/fitbyte/fitstruct.rb
94
93
  - lib/fitbyte/food.rb
95
94
  - lib/fitbyte/friends.rb
96
95
  - lib/fitbyte/goals.rb
@@ -119,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
118
  version: '0'
120
119
  requirements: []
121
120
  rubyforge_project:
122
- rubygems_version: 2.4.5
121
+ rubygems_version: 2.5.1
123
122
  signing_key:
124
123
  specification_version: 4
125
124
  summary: This gem allows interaction with Fitbit's REST API.
@@ -1,3 +0,0 @@
1
- module Fitbyte
2
- class FitStruct < OpenStruct; end
3
- end