v2_intuity 1.0.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.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +38 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/lib/requirements.rb +17 -0
  13. data/lib/v2_intuity/client.rb +118 -0
  14. data/lib/v2_intuity/endpoints/endpoint.rb +46 -0
  15. data/lib/v2_intuity/endpoints/marketplace.rb +22 -0
  16. data/lib/v2_intuity/endpoints/measurement.rb +10 -0
  17. data/lib/v2_intuity/endpoints/nutrition.rb +10 -0
  18. data/lib/v2_intuity/endpoints/sleep.rb +10 -0
  19. data/lib/v2_intuity/endpoints/stream.rb +24 -0
  20. data/lib/v2_intuity/endpoints/summary.rb +10 -0
  21. data/lib/v2_intuity/endpoints/user.rb +18 -0
  22. data/lib/v2_intuity/endpoints/workout.rb +10 -0
  23. data/lib/v2_intuity/filters/access_token.rb +10 -0
  24. data/lib/v2_intuity/filters/asc_created_at.rb +10 -0
  25. data/lib/v2_intuity/filters/asc_updated_at.rb +10 -0
  26. data/lib/v2_intuity/filters/date.rb +10 -0
  27. data/lib/v2_intuity/filters/dec_created_at.rb +10 -0
  28. data/lib/v2_intuity/filters/dec_updated_at.rb +10 -0
  29. data/lib/v2_intuity/filters/end_date.rb +10 -0
  30. data/lib/v2_intuity/filters/filter.rb +22 -0
  31. data/lib/v2_intuity/filters/format_redirect.rb +10 -0
  32. data/lib/v2_intuity/filters/resource_filters.rb +20 -0
  33. data/lib/v2_intuity/filters/sort.rb +13 -0
  34. data/lib/v2_intuity/filters/source.rb +10 -0
  35. data/lib/v2_intuity/filters/start_date.rb +10 -0
  36. data/lib/v2_intuity/headers/accept.rb +10 -0
  37. data/lib/v2_intuity/headers/content_type.rb +10 -0
  38. data/lib/v2_intuity/headers/header.rb +17 -0
  39. data/lib/v2_intuity/request.rb +12 -0
  40. data/lib/v2_intuity/request_error.rb +37 -0
  41. data/lib/v2_intuity/response.rb +60 -0
  42. data/lib/v2_intuity/streams/stream.rb +43 -0
  43. data/lib/v2_intuity/users/user.rb +71 -0
  44. data/lib/v2_intuity/version.rb +3 -0
  45. data/lib/v2_intuity.rb +13 -0
  46. data/v2_intuity.gemspec +29 -0
  47. metadata +146 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13104922bec089e414562de76b22ac3d7203beaf
4
+ data.tar.gz: 5aa9f094fb011b2476e7a3ef0c83f3acb52d3168
5
+ SHA512:
6
+ metadata.gz: b2997175def3a560cd3585fb49d032d34a5aa53f89a869d7bae4850eceb358f84341c41bea2606a4e31d22bb1d72071b831a139df80b9fa797f3d95b194bb137
7
+ data.tar.gz: 3937dd0f96c57d44804cdb683c61f60b46740ce9c175eeb528a6ef16d6e40d5a18dcccf18db32c6b4f328049dd8dd9d11a9deb4c3e6d6ba812c9861adb682321
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.14.6
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at rruiz@ncsu.edu. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in validic2.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 rruiz858
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,38 @@
1
+ # V2Intuity
2
+
3
+ This is a work in progress!
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'v2_intuity'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install v2_intuity
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Development
26
+
27
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+
31
+ ## Contributing
32
+
33
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/v2_intuity. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
34
+
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "v2_intuity"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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
@@ -0,0 +1,17 @@
1
+ require "v2_intuity/client"
2
+ require "v2_intuity/request_error"
3
+ require "v2_intuity/response"
4
+ require "v2_intuity/request"
5
+ require "v2_intuity/version"
6
+ require "v2_intuity/filters/filter"
7
+ require "v2_intuity/endpoints/endpoint"
8
+ require "v2_intuity/filters/sort"
9
+ require "v2_intuity/filters/date"
10
+ require "v2_intuity/headers/header"
11
+
12
+ ROOT = File.dirname(File.expand_path(File.dirname(__FILE__))).freeze
13
+
14
+ Dir["#{ROOT}/lib/v2_intuity/endpoints/*.rb"].each { |file| require file unless file =~ /endpoint.rb/}
15
+ Dir["#{ROOT}/lib/v2_intuity/filters/*.rb"].each { |file| require file unless file =~ /filter.rb|sort.rb|date.rb/ }
16
+ Dir["#{ROOT}/lib/v2_intuity/headers/*.rb"].each { |file| require file unless file =~ /header.rb/ }
17
+ Dir["#{ROOT}/lib/v2_intuity/users/*.rb"].each { |file| require file }
@@ -0,0 +1,118 @@
1
+ require 'httparty'
2
+ module V2Intuity
3
+ class Client
4
+ attr_accessor :token, :org_id, :accept,
5
+ :content_type, :base_uri,
6
+ :httparty_client
7
+
8
+ def initialize(attributes = {})
9
+ required_fields(attributes)
10
+ @accept = attributes.fetch(:accept, 'application/json')
11
+ @content_type = attributes.fetch(:content_type, 'application/json')
12
+ end
13
+
14
+ def send_request(request)
15
+ case request.method
16
+ when V2Intuity::Request::METHODS[:get], V2Intuity::Request::METHODS[:delete]
17
+ @httparty_client.send(request.method.to_sym, request.uri, query: request.options[:filters], headers: request.options[:headers]) # puts re.request.last_uri.to_s
18
+
19
+ when V2Intuity::Request::METHODS[:post], V2Intuity::Request::METHODS[:put]
20
+ @httparty_client.send(request.method.to_sym, request.uri, query: request.options[:filters],
21
+ body: request.options[:body].to_json,
22
+ headers: request.options[:headers])
23
+ end
24
+ end
25
+
26
+ def build_and_send_request(endpoint, options = {})
27
+ @request = build_request(endpoint, options)
28
+ send_request_generate_response(@request)
29
+ end
30
+
31
+ def build_request(endpoint, options = {})
32
+ http_party_options = {}
33
+ @filters = {}
34
+ @headers = {}
35
+
36
+ method = options[:method].to_s.nil? ? V2Intuity::Request::METHODS[:get] : options[:method]
37
+ id = options[:id].to_s.nil? ? nil : options[:id]
38
+
39
+ filters, headers = combine_filters_and_headers(options)
40
+
41
+ http_party_options[:filters] = filters
42
+ http_party_options[:headers] = headers
43
+ http_party_options[:body] = options[:body] unless method == V2Intuity::Request::METHODS[:get]
44
+ uri = endpoint.uri(org_id: org_id, id: id, base_uri: base_uri)
45
+
46
+ V2Intuity::Request.new(method: method, uri: uri, options: http_party_options)
47
+ end
48
+
49
+ def send_request_generate_response(request)
50
+ response = V2Intuity::Response.new(request: request)
51
+
52
+ self.httparty_client ||= HTTParty
53
+ response.define_response_and_json(send_request(request))
54
+
55
+ return response if response.response.success?
56
+
57
+ error = build_error(response)
58
+
59
+ response.add_errors(error)
60
+
61
+ response
62
+
63
+ rescue StandardError => e
64
+ response.add_errors(RequestError.new(code: 400, message: e.message))
65
+
66
+ response
67
+ end
68
+
69
+ private
70
+
71
+ def build_error(response)
72
+ json = response.json
73
+ if json
74
+ V2Intuity::RequestError.new(code: response.code, message: response.message, description: json['message'])
75
+ else
76
+ V2Intuity::RequestError.new(code: response.code, message: response.message)
77
+ end
78
+ end
79
+
80
+ def required_fields(attributes)
81
+ @token = attributes[:token]
82
+ raise 'Client requires token' unless @token
83
+
84
+ @org_id = attributes[:org_id]
85
+ raise 'Client requires org_id' unless @org_id
86
+
87
+ @base_uri = attributes[:base_uri]
88
+ raise 'Client requires base_uri' unless @base_uri
89
+ end
90
+
91
+ def default_filters
92
+ [V2Intuity::Filters::AccessToken.new(value: @token)]
93
+ end
94
+
95
+ def default_headers
96
+ [V2Intuity::Headers::Accept.new(value: @accept), V2Intuity::Headers::ContentType.new(value: @content_type)]
97
+ end
98
+
99
+ def combine_filters_and_headers(options)
100
+ total_filters = {}
101
+ total_headers = {}
102
+ filter_array = options[:filters].is_a?(Array) ? options[:filters] : []
103
+ header_array = options[:headers].is_a?(Array) ? options[:headers] : []
104
+
105
+ unique_filters = (default_filters + filter_array)
106
+ unique_filters.each do |filter|
107
+ total_filters.merge!(filter.to_hash)
108
+ end
109
+
110
+ unique_headers = (default_headers + header_array)
111
+ unique_headers.each do |header|
112
+ total_headers.merge!(header.to_hash)
113
+ end
114
+
115
+ [total_filters, total_headers]
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,46 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class Endpoint
4
+ attr_accessor :base_uri, :org_id, :id, :endpoint, :path, :token
5
+
6
+ ENDPOINTS = { measurements: 'measurements', sleeps: 'sleeps',
7
+ nutrition: 'nutrition', summaries: 'summaries',
8
+ workouts: 'workouts', users: 'users',
9
+ marketplace: 'marketplace', streams: 'streams' }.freeze
10
+
11
+ def initialize(options = {})
12
+ @endpoint = options[:endpoint]
13
+ @path = options.fetch(:path, 'organizations')
14
+ @id = options[:id]
15
+ add_constants(options)
16
+ end
17
+
18
+ def uri(options = {})
19
+ uri_options(options)
20
+ if @id.nil?
21
+ "#{@base_uri}/#{@path}/#{@org_id}/#{@endpoint}"
22
+ else
23
+ "#{@base_uri}/#{@path}/#{@org_id}/users/#{@id}/#{@endpoint}"
24
+ end
25
+ end
26
+
27
+ def uri_options(options = {})
28
+ @base_uri ||= options[:base_uri]
29
+ @org_id ||= options[:org_id]
30
+ @id ||= options[:id]
31
+ end
32
+
33
+ def sign_with
34
+ [:access_token]
35
+ end
36
+
37
+ private
38
+
39
+ def add_constants(options = {})
40
+ @base_uri = options[:base_uri]
41
+ @token = options[:token]
42
+ @org_id = options[:org_id]
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,22 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class Marketplace < Endpoint
4
+ REDIRECT_URI = 'https://syncmydevice.com/'.freeze
5
+
6
+ def initialize(options = {})
7
+ super
8
+ @endpoint = Endpoint::ENDPOINTS[:marketplace]
9
+ end
10
+
11
+ def uri(options = {})
12
+ return super if @base_uri != REDIRECT_URI
13
+
14
+ @base_uri
15
+ end
16
+
17
+ def define_redirect_uri(uri = REDIRECT_URI)
18
+ @base_uri = uri
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class Measurement < Endpoint
4
+ def initialize(options = {})
5
+ super
6
+ @endpoint = Endpoint::ENDPOINTS[:measurements]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class Nutrition < Endpoint
4
+ def initialize(options = {})
5
+ super
6
+ @endpoint = Endpoint::ENDPOINTS[:nutrition]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class Sleep < Endpoint
4
+ def initialize(options = {})
5
+ super
6
+ @endpoint = Endpoint::ENDPOINTS[:nutrition]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class Stream < Endpoint
4
+ def initialize(options = {})
5
+ @base_uri = options[:stream_uri]
6
+ @endpoint = Endpoint::ENDPOINTS[:streams]
7
+ end
8
+
9
+ def uri(options = {})
10
+ uri_options(options)
11
+ if @id.nil?
12
+ "#{@base_uri}/#{@endpoint}"
13
+ else
14
+ "#{@base_uri}/#{@endpoint}/#{@id}"
15
+ end
16
+ end
17
+
18
+ def uri_options(options = {})
19
+ @base_uri ||= options[:stream_uri]
20
+ @id ||= options[:id]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class Summary < Endpoint
4
+ def initialize(options = {})
5
+ super
6
+ @endpoint = Endpoint::ENDPOINTS[:summaries]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class User < Endpoint
4
+ def initialize(options = {})
5
+ super
6
+ @endpoint = Endpoint::ENDPOINTS[:users]
7
+ end
8
+
9
+ def uri(options = {})
10
+ uri_options(options)
11
+
12
+ return "#{@base_uri}/#{@path}/#{@org_id}/#{@endpoint}/#{@id}/" if @id
13
+
14
+ "#{@base_uri}/#{@path}/#{@org_id}/users" # used with creating users
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Endpoints
3
+ class Workout < Endpoint
4
+ def initialize(options = {})
5
+ super
6
+ @endpoint = Endpoint::ENDPOINTS[:weights]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class AccessToken < Filter
4
+ def initialize(options = {})
5
+ super
6
+ @name = Filter::FILTERS[:access_token]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class AscCreatedAt < Sort
4
+ def initialize(options = {})
5
+ super
6
+ @value = Filter::Sort::SORT[:asc_created_at]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class AscUpdatedAt < Sort
4
+ def initialize(options = {})
5
+ super
6
+ @value = Filter::Sort::SORT[:asc_updated_at]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class Date < Filter
4
+ def initialize(options = {})
5
+ super
6
+ @value = DateTime.iso8601(options[:value])
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class DecCreatedAt < Sort
4
+ def initialize(options = {})
5
+ super
6
+ @value = Filter::Sort::SORT[:dec_created_at]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class DecUpdatedAt < Sort
4
+ def initialize(options = {})
5
+ super
6
+ @value = Filter::Sort::SORT[:dec_updated_at]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class EndDate < Date
4
+ def initialize(options = {})
5
+ super
6
+ @name = Filter::FIRTERS[:end_date_filter]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,22 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class Filter
4
+ attr_accessor :name, :value
5
+
6
+ FILTERS = { access_token: 'token', source: 'source',
7
+ start_filter: 'start_date_filter',
8
+ end_filter: 'end_date_filter',
9
+ format_redirect: 'format',
10
+ sort: 'sort' }.freeze
11
+
12
+ def initialize(options = {})
13
+ @name = options[:name]
14
+ @value = options[:value]
15
+ end
16
+
17
+ def to_hash
18
+ { @name.to_sym => @value }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class FormatRedirect < Filter
4
+ def initialize(options = {})
5
+ super
6
+ @name = Filter::FILTERS[:format_redirect]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class ResourceFilter # Strean resource filters that indicate what kind of data to be returned by stream
4
+ attr_accessor :name, :value
5
+
6
+ FILTERS = { measurement: 'measurement', nutrition: 'nutrition',
7
+ sleep: 'sleep', summaries: 'summaries',
8
+ workout: 'workout' }.freeze
9
+
10
+ def initialize(options = {})
11
+ @name = options[:name]
12
+ @value = options[:value]
13
+ end
14
+
15
+ def to_hash
16
+ { @name.to_sym => @value }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,13 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class Sort < Filter
4
+ SORT = { dec_updated_at: '-updated_at', asc_updated_at: 'updated_at',
5
+ dec_created_at: '-created_at', asc_created_at: 'created_at' }.freeze
6
+
7
+ def initialize(options = {})
8
+ super
9
+ @name = Filter::FILTERS[:sort]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class Source < Filter
4
+ def initialize(options = {})
5
+ super
6
+ @name = Filter::FILTERS[:source]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Filters
3
+ class StartDate < Date
4
+ def initialize(options = {})
5
+ super
6
+ @name = Filter::FIRTERS[:start_date_filter]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Headers
3
+ class Accept < Header
4
+ def initialize(options = {})
5
+ super
6
+ @name = Header::HEADERS[:accept]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module V2Intuity
2
+ module Headers
3
+ class ContentType < Header
4
+ def initialize(options = {})
5
+ super
6
+ @name = Header::HEADERS[:content_type]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,17 @@
1
+ module V2Intuity
2
+ module Headers
3
+ class Header
4
+ attr_accessor :name, :value
5
+ HEADERS = { accept: 'accept', content_type: 'content_type' }.freeze
6
+
7
+ def initialize(options = {})
8
+ @name = options[:name]
9
+ @value = options[:value]
10
+ end
11
+
12
+ def to_hash
13
+ { @name.to_sym => @value }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module V2Intuity
2
+ class Request
3
+ METHODS = { get: 'get', put: 'put', post: 'post', delete: 'delete' }.freeze
4
+ attr_accessor :method, :uri, :options
5
+
6
+ def initialize(options = {})
7
+ @method = options[:method]
8
+ @uri = options[:uri]
9
+ @options = options[:options]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+ module V2Intuity
2
+ class RequestError
3
+ attr_accessor :code, :message, :description
4
+
5
+ CODES = { 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing',
6
+ 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information',
7
+ 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-status',
8
+ 208 => 'Already Reported', 300 => 'Multiple Choices', 301 => 'Moved Permanently',
9
+ 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy',
10
+ 306 => 'Switch Proxy', 307 => 'Temporary Redirect', 400 => 'Bad Request',
11
+ 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found',
12
+ 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required',
13
+ 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required',
14
+ 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large',
15
+ 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed',
16
+ 418 => 'I\'m a teapot', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency',
17
+ 425 => 'Unordered Collection', 426 => 'Upgrade Required', 428 => 'Precondition Required',
18
+ 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large',
19
+ 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway',
20
+ 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported',
21
+ 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 508 => 'Loop Detected',
22
+ 511 => 'Network Authentication Required', 1000 => 'Rate Limit Reached' }.freeze
23
+
24
+ def initialize(attributes = {})
25
+ @code = attributes[:code].to_i
26
+ @message = attributes.fetch(:message, CODES[@code])
27
+ @description = attributes[:description]
28
+ end
29
+
30
+ def as_json(_options = {})
31
+ {
32
+ description: @description,
33
+ message: @message
34
+ }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,60 @@
1
+ module V2Intuity
2
+ class Response
3
+ attr_accessor :request, :response, :errors
4
+
5
+ def initialize(attributes = {})
6
+ @request = attributes[:request]
7
+ @response = attributes[:response]
8
+ @errors = []
9
+ end
10
+
11
+ def define_response_and_json(response)
12
+ @response = response if @response.nil?
13
+ g_json if @json.nil?
14
+ end
15
+
16
+ def headers
17
+ !@response.to_s.nil? ? @response.headers : nil
18
+ end
19
+
20
+ def body
21
+ !@response.to_s.nil? ? @response.body : nil
22
+ end
23
+
24
+ def add_errors(error)
25
+ @errors.push(error)
26
+ end
27
+
28
+ def success?
29
+ return nil if @response.to_s.nil?
30
+
31
+ !@errors.to_s.nil? ? false : true
32
+ end
33
+
34
+ def g_json # store parsed response so we don't have to reparse
35
+ return @json if @json
36
+
37
+ json(parse_json(@response.body)) unless @response.to_s.nil?
38
+ end
39
+
40
+ def code
41
+ !@response.to_s.nil? ? @response.code : nil
42
+ end
43
+
44
+ def message
45
+ !@response.to_s.nil? ? @response.message : nil
46
+ end
47
+
48
+ private
49
+
50
+ def parse_json(body) # function that makes sure string
51
+ JSON.parse(body)
52
+ rescue
53
+ nil
54
+ end
55
+
56
+ def json(json)
57
+ @json = json
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,43 @@
1
+ module V2Intuity
2
+ module Streams
3
+ class Stream
4
+ attr_accessor :name, :start_date, :resource_filters, :stream_id
5
+
6
+ def initialize(options = {})
7
+ @name = options[:name]
8
+ @start_date = options.fetch(:start_date, Time.now.strftime('%Y-%d-%m'))
9
+ filters = options.fetch(:resource_filters, [])
10
+ @resource_filters = filters.is_a?(Array) ? filters : []
11
+ @stream_id = options[:stream_id]
12
+ end
13
+
14
+ def send_request(method, client = V2Intuity::Client.new)
15
+ endpoint, options = build_options(method)
16
+ client.build_and_send_request(endpoint, options)
17
+ end
18
+
19
+ def build_options(method, filters = [])
20
+ case method
21
+ when V2Intuity::Request::METHODS[:delete]
22
+ raise 'stream id is needed' unless @stream_id
23
+ options = { method: method, id: stream_id }
24
+ when V2Intuity::Request::METHODS[:get]
25
+ options = { method: method, id: stream_id }.compact # id can be blank when getting all streams
26
+ when V2Intuity::Request::METHODS[:post]
27
+ body = { name: name, start_date: start_date,
28
+ resource_filters: resource_filters } # supported resource_filters include measurement, nutrition, sleep, workout
29
+ options = { method: method, body: body.compact }
30
+ when V2Intuity::Request::METHODS[:put]
31
+ raise 'stream id is needed' unless @stream_id
32
+ body = { name: name }
33
+ options = { method: method, body: body.compact, id: stream_id }
34
+ else
35
+ raise 'incorrect method verb'
36
+ end
37
+ options[:filters] = filters if !filters.to_s.nil?
38
+
39
+ [V2Intuity::Endpoints::Stream.new, options]
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,71 @@
1
+ module V2Intuity
2
+ module Users
3
+ class User
4
+ attr_accessor :uid, :country_code, :time_zone,
5
+ :validic_token, :applications, :devices
6
+
7
+ def initialize(options = {})
8
+ @validic_token = options.fetch(:validic_token, nil)
9
+ @uid = options.fetch(:uid, nil)
10
+ @time_zone = options.fetch(:location, nil)
11
+ @country_code = options.fetch(:country_code, nil)
12
+ end
13
+
14
+ def send_request(method)
15
+ endpoint, options = build_options(method)
16
+ client.build_and_send_request(endpoint, options)
17
+ end
18
+
19
+ def build_options(method, filters = [])
20
+ raise 'uid is required' unless uid
21
+
22
+ case method
23
+ when V2Intuity::Request::METHODS[:delete], V2Intuity::Request::METHODS[:get]
24
+ options = { method: method, id: uid }
25
+ when V2Intuity::Request::METHODS[:post]
26
+ body = { id: uid, location: { timezone: time_zone, country_code: country_code }.compact }
27
+ options = { method: method, body: body.compact }
28
+ when V2Intuity::Request::METHODS[:put]
29
+ body = { location: { timezone: time_zone, country_code: country_code }.compact }
30
+ options = { method: method, body: body.compact, id: uid }
31
+ else
32
+ raise 'incorrect method verb'
33
+ end
34
+ options[:filters] = filters unless filters.to_s.nil?
35
+
36
+ [V2Intuity::Endpoints::User.new, options]
37
+ end
38
+
39
+ # method refreshes marketplace user token.
40
+ def refresh_marketplace(client)
41
+ raise 'uid is required' unless uid
42
+
43
+ options = { method: V2Intuity::Request::METHODS[:post], id: uid }
44
+ endpoint = V2Intuity::Endpoints::Marketplace.new
45
+
46
+ client.build_and_send_request(endpoint, options)
47
+ end
48
+
49
+ # method finds the marketplace connected and disconnected json response
50
+ def get_marketplace(client, format_redirect = 'json', redirect_uri = V2Intuity::Endpoints::Marketplace::REDIRECT_URI) # marketplace user defined token
51
+ raise 'validic token is required' unless validic_token
52
+
53
+ endpoint, options = build_options_marketplace(validic_token, format_redirect, redirect_uri)
54
+
55
+ client.build_and_send_request(endpoint, options)
56
+ end
57
+
58
+ def build_options_marketplace(token, format_redirect, redirect_uri) # marketplace user defined token
59
+ filters = []
60
+ filters << V2Intuity::Filters::AccessToken.new(value: token)
61
+ filters << V2Intuity::Filters::FormatRedirect.new(value: format_redirect)
62
+
63
+ options = { filters: filters, method: V2Intuity::Request::METHODS[:get] }
64
+ endpoint = V2Intuity::Endpoints::Marketplace.new
65
+ endpoint.define_redirect_uri(redirect_uri)
66
+
67
+ [endpoint, options]
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module V2Intuity
2
+ VERSION = '1.0.0'.freeze
3
+ end
data/lib/v2_intuity.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'requirements'
2
+
3
+ module V2Intuity
4
+ class << self
5
+ attr_accessor :base_uri, :token,
6
+ :stream_uri, :org_id
7
+
8
+ def configure
9
+ yield self
10
+ true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'v2_intuity/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "v2_intuity"
8
+ spec.version = V2Intuity::VERSION
9
+ spec.authors = ["rruiz858"]
10
+ spec.email = ["rruiz@ncsu.edu"]
11
+
12
+ spec.summary = "V2Intuity is a ruby wrapper to connecting to the new V2Intuity platform"
13
+ spec.description = "Wrapper allows one to manage V2Intuity's Rest API and even create/update/delete Streams. This is a work in progess and will not work, use other validic gem"
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ 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", "~> 1.14"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_dependency "httparty", "~> 0.14.0"
29
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: v2_intuity
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - rruiz858
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-15 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: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: httparty
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.14.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.14.0
69
+ description: Wrapper allows one to manage V2Intuity's Rest API and even create/update/delete
70
+ Streams. This is a work in progess and will not work, use other validic gem
71
+ email:
72
+ - rruiz@ncsu.edu
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/requirements.rb
88
+ - lib/v2_intuity.rb
89
+ - lib/v2_intuity/client.rb
90
+ - lib/v2_intuity/endpoints/endpoint.rb
91
+ - lib/v2_intuity/endpoints/marketplace.rb
92
+ - lib/v2_intuity/endpoints/measurement.rb
93
+ - lib/v2_intuity/endpoints/nutrition.rb
94
+ - lib/v2_intuity/endpoints/sleep.rb
95
+ - lib/v2_intuity/endpoints/stream.rb
96
+ - lib/v2_intuity/endpoints/summary.rb
97
+ - lib/v2_intuity/endpoints/user.rb
98
+ - lib/v2_intuity/endpoints/workout.rb
99
+ - lib/v2_intuity/filters/access_token.rb
100
+ - lib/v2_intuity/filters/asc_created_at.rb
101
+ - lib/v2_intuity/filters/asc_updated_at.rb
102
+ - lib/v2_intuity/filters/date.rb
103
+ - lib/v2_intuity/filters/dec_created_at.rb
104
+ - lib/v2_intuity/filters/dec_updated_at.rb
105
+ - lib/v2_intuity/filters/end_date.rb
106
+ - lib/v2_intuity/filters/filter.rb
107
+ - lib/v2_intuity/filters/format_redirect.rb
108
+ - lib/v2_intuity/filters/resource_filters.rb
109
+ - lib/v2_intuity/filters/sort.rb
110
+ - lib/v2_intuity/filters/source.rb
111
+ - lib/v2_intuity/filters/start_date.rb
112
+ - lib/v2_intuity/headers/accept.rb
113
+ - lib/v2_intuity/headers/content_type.rb
114
+ - lib/v2_intuity/headers/header.rb
115
+ - lib/v2_intuity/request.rb
116
+ - lib/v2_intuity/request_error.rb
117
+ - lib/v2_intuity/response.rb
118
+ - lib/v2_intuity/streams/stream.rb
119
+ - lib/v2_intuity/users/user.rb
120
+ - lib/v2_intuity/version.rb
121
+ - v2_intuity.gemspec
122
+ homepage: ''
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.6.11
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: V2Intuity is a ruby wrapper to connecting to the new V2Intuity platform
146
+ test_files: []