human_api 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +75 -0
- data/Rakefile +1 -0
- data/human_api.gemspec +29 -0
- data/lib/config/initializers/core_ext.rb +16 -0
- data/lib/human_api/app.rb +38 -0
- data/lib/human_api/config.rb +48 -0
- data/lib/human_api/human.rb +92 -0
- data/lib/human_api/version.rb +3 -0
- data/lib/human_api.rb +25 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2a6cb0060e8a7c0d3ec9e4d79fdcdf4b5c101cdc
|
4
|
+
data.tar.gz: 6477af6b9a515d46c4ffb694d047b29af03a0257
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c98f1172de428f932d2b35764b69232407696782c1954d89e91c79f6eff43efe00c57b2e7d5ac6fa62bb6db18196b3ca5336b1a06163c9e164e2fd03285a58a3
|
7
|
+
data.tar.gz: 2cf4e4a6426cf4f6aebaaccb55755b771255dd664ab75e457b967b31d21a1194946f7016424539301663f5e14f50d97b1dc8f6cd816d753694aab5a41e52c17b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Pazienti.it
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# HumanApi
|
2
|
+
|
3
|
+
A Ruby client to [HumanApi](http://humanapi.co).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
# We're going to publish our gem soon...
|
10
|
+
gem 'human_api', :git => 'git://github.com/Pazienti/humanapi.git'
|
11
|
+
|
12
|
+
## Configuration
|
13
|
+
The gem is quite simple to configure. You can configure the gem using its home-made initializer:
|
14
|
+
|
15
|
+
HumanApi.config do |c|
|
16
|
+
c.app_id = "<YOUR_APP_ID>"
|
17
|
+
c.query_key = "<YOUR_QUERY_KEY>"
|
18
|
+
|
19
|
+
# This is the part where the magics happen
|
20
|
+
c.human_model = User # Tell me what is the model you want to use
|
21
|
+
c.token_method_name = :human_token # Tell me the method you use to retrieve the token (Inside the human_model)
|
22
|
+
end
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
Once you did the configuration, the usage of the gem is quite ridiculous:
|
26
|
+
|
27
|
+
# Somewhere in your model
|
28
|
+
u = User.first
|
29
|
+
u.human.profile #=> Will return the humanapi user's profile
|
30
|
+
u.human.query(:activities) #=> Will return everything you asked for
|
31
|
+
|
32
|
+
Just use the _human_ instance method from your User instance and that's it ;)
|
33
|
+
|
34
|
+
###The query method
|
35
|
+
The query method is meant to ask whatever you want whenever you want. Here are some permitted methods (according to humanapi) you can use to retrieve user's data:
|
36
|
+
|
37
|
+
profile
|
38
|
+
activities
|
39
|
+
blood_glucose
|
40
|
+
blood_pressure
|
41
|
+
body_fat
|
42
|
+
genetic_traits
|
43
|
+
heart_rate
|
44
|
+
height
|
45
|
+
locations
|
46
|
+
sleeps
|
47
|
+
weight
|
48
|
+
bmi
|
49
|
+
|
50
|
+
Mixin' up these methods with some options will give you what you want.
|
51
|
+
|
52
|
+
u.human.query(:activities, :summary => true) #=> will give you a summary of the activities
|
53
|
+
u.human.query(:sleeps, :date => "2014-01-01") #=> Will give you a single sleep measurement
|
54
|
+
|
55
|
+
# Getting KPIs (KPIs are just single values you get to retrieve a measurements average value)
|
56
|
+
u.human.query(:weight) #=> Will give you a single weight value (The avg I guess)
|
57
|
+
|
58
|
+
# Getting readings (If you begin with a single avg value and you wanna go deeper)
|
59
|
+
u.human.query(:weight, :readings => true)
|
60
|
+
|
61
|
+
Lastly, as a common rule, I've identified a pattern in humanapis.
|
62
|
+
- If the method name is plural, it will give you multiple measurements when calling it. In addition, you can ask for a :summary => true, for a group of value in a specific :date => "DATE" or for a single known measurement :id => "measurement_id"
|
63
|
+
- If the method name is singular, it will give you a single avg value for what you asked. In addition, you can ask for all :readings => true and for all readings => true in a specific :date=> "DATE".
|
64
|
+
- If I'm missing something just send me a PM or open an issue.
|
65
|
+
|
66
|
+
|
67
|
+
###The alternative
|
68
|
+
If you don't like that configuration, you can use a simple method right in your own user model:
|
69
|
+
|
70
|
+
humanizable :token_method
|
71
|
+
|
72
|
+
That's it! Then you can do something like _u.human.profile_ like I said before.
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
Feel free to contribute with your pull requests and forks. Get in touch with us at team@pazienti.it or open an issue.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/human_api.gemspec
ADDED
@@ -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 'human_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "human_api"
|
8
|
+
spec.version = HumanApi::VERSION
|
9
|
+
spec.authors = ["Alessio Santo"]
|
10
|
+
spec.email = ["alessio.santo@pazienti.it"]
|
11
|
+
spec.description = %q{API client for HumanAPI}
|
12
|
+
spec.summary = %q{API client for HumanAPI}
|
13
|
+
spec.homepage = "https://github.com/Pazienti/humanapi"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency "nestful", "~> 1.0.4"
|
23
|
+
spec.add_dependency "json", "~> 1.8.1"
|
24
|
+
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class String
|
2
|
+
def is_singular?
|
3
|
+
self.pluralize != self and self.singularize == self
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class ActiveRecord::Base
|
8
|
+
|
9
|
+
attr_accessor :human_var, :test
|
10
|
+
|
11
|
+
def self.humanizable(method)
|
12
|
+
define_method :human do
|
13
|
+
@human_var ||= HumanApi::Human.new(:access_token => self.send(method.to_sym))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
# THE MODULE
|
3
|
+
module HumanApi
|
4
|
+
# THE CLASS # TODO: Make it an instance of class HumanApi::App :(
|
5
|
+
class App < Nestful::Resource
|
6
|
+
|
7
|
+
# The host of the api
|
8
|
+
endpoint 'https://api.humanapi.co'
|
9
|
+
|
10
|
+
# The path of the api
|
11
|
+
path "/v1/apps/#{HumanApi.config.app_id}"
|
12
|
+
|
13
|
+
# This should be a private method
|
14
|
+
def self.authentication
|
15
|
+
Base64.encode64("#{HumanApi.config.query_key}:")
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get the humans of your app
|
19
|
+
def self.humans
|
20
|
+
get("users", {}, {:headers => {"Authorization" => "Basic #{authentication}"}})
|
21
|
+
end
|
22
|
+
|
23
|
+
# Create a new human
|
24
|
+
def self.create_human(id)
|
25
|
+
# Make a call to create the user
|
26
|
+
response = post("users", {:externalId => id}, {:headers => {"Authorization" => "Basic #{authentication}"}})
|
27
|
+
|
28
|
+
# If the response is true
|
29
|
+
if response.status >= 200 and response.status < 300 # Leave it for now
|
30
|
+
JSON.parse response.body
|
31
|
+
# Else tell me something went wrong
|
32
|
+
else
|
33
|
+
false # Nothing was created
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
# THE MODULE
|
3
|
+
module HumanApi
|
4
|
+
# THE CLASS
|
5
|
+
class Config
|
6
|
+
attr_accessor :app_id, :query_key, :human_model, :token_method_name, :hardcore
|
7
|
+
|
8
|
+
CHECK_THE_GUIDE = "Read the guide for more information. (https://github.com/Pazienti/humanapi)"
|
9
|
+
|
10
|
+
# Init some vars
|
11
|
+
def initialize
|
12
|
+
@hardcore = false
|
13
|
+
end
|
14
|
+
|
15
|
+
# Init some vars
|
16
|
+
def configure
|
17
|
+
rewrite_human_model
|
18
|
+
end
|
19
|
+
|
20
|
+
# Rewrite the human model
|
21
|
+
def rewrite_human_model
|
22
|
+
# Check if the human_model to use and the method name are present
|
23
|
+
if human_model.present? and token_method_name.present?
|
24
|
+
|
25
|
+
# Check if the method given exists in the given class
|
26
|
+
if human_model.instance_methods.include?(token_method_name)
|
27
|
+
|
28
|
+
# Rewrite the class adding a method
|
29
|
+
human_model.class_eval do
|
30
|
+
attr_accessor :human_var
|
31
|
+
|
32
|
+
# The method in the human_model class instance
|
33
|
+
def human
|
34
|
+
# Initialize with the access token
|
35
|
+
@human_var ||= HumanApi::Human.new(:access_token => self.send(HumanApi.config.token_method_name.to_sym))
|
36
|
+
end
|
37
|
+
end
|
38
|
+
else
|
39
|
+
raise "Could not find '#{token_method_name}' in #{human_model}. #{CHECK_THE_GUIDE}"
|
40
|
+
end
|
41
|
+
else
|
42
|
+
# unless hardcore
|
43
|
+
# raise "You must set a human_model and a token_method_name. #{CHECK_THE_GUIDE}"
|
44
|
+
# end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
|
2
|
+
# THE MODULE
|
3
|
+
module HumanApi
|
4
|
+
# THE CLASS
|
5
|
+
class Human < Nestful::Resource
|
6
|
+
|
7
|
+
attr_reader :token
|
8
|
+
|
9
|
+
# The host of the api
|
10
|
+
endpoint 'https://api.humanapi.co'
|
11
|
+
|
12
|
+
# The path of the api
|
13
|
+
path '/v1/human'
|
14
|
+
|
15
|
+
# The available methods for this api
|
16
|
+
AVAILABLE_METHODS = [
|
17
|
+
:profile,
|
18
|
+
:activities,
|
19
|
+
:blood_glucose,
|
20
|
+
:blood_pressure,
|
21
|
+
:body_fat,
|
22
|
+
:genetic_traits,
|
23
|
+
:heart_rate,
|
24
|
+
:height,
|
25
|
+
:locations,
|
26
|
+
:sleeps,
|
27
|
+
:weight,
|
28
|
+
:bmi
|
29
|
+
]
|
30
|
+
|
31
|
+
def initialize(options)
|
32
|
+
@token = options[:access_token]
|
33
|
+
super
|
34
|
+
end
|
35
|
+
|
36
|
+
# Profile =====================================
|
37
|
+
|
38
|
+
def summary
|
39
|
+
get('', :access_token => token)
|
40
|
+
end
|
41
|
+
|
42
|
+
def profile
|
43
|
+
query('profile')
|
44
|
+
end
|
45
|
+
|
46
|
+
# =============================================
|
47
|
+
|
48
|
+
def query(method, options = {})
|
49
|
+
|
50
|
+
# Is this method in the list?
|
51
|
+
if AVAILABLE_METHODS.include? method.to_sym
|
52
|
+
# From sym to string
|
53
|
+
method = method.to_s
|
54
|
+
|
55
|
+
# The base of the url
|
56
|
+
url = "#{method}"
|
57
|
+
|
58
|
+
# If it is a singular word prepare for readings
|
59
|
+
if method.is_singular?
|
60
|
+
if options[:readings] == true
|
61
|
+
url += "/readings"
|
62
|
+
end
|
63
|
+
else
|
64
|
+
if options[:summary] == true
|
65
|
+
url += "/summary"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# You passed a date
|
70
|
+
if options[:date].present?
|
71
|
+
# Make a request for a specific date
|
72
|
+
url += "/daily/#{options[:date]}"
|
73
|
+
# If you passed an id
|
74
|
+
elsif options[:id].present?
|
75
|
+
# Make a request for a single
|
76
|
+
url += "/#{options[:id]}"
|
77
|
+
end
|
78
|
+
|
79
|
+
# Make the request finally
|
80
|
+
result = get(url, :access_token => token)
|
81
|
+
|
82
|
+
# Converting to json the body string
|
83
|
+
JSON.parse(result.body)
|
84
|
+
else
|
85
|
+
# Tell the developer the method is not there
|
86
|
+
"The method '#{method}' does not exist!"
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
data/lib/human_api.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "json"
|
2
|
+
require "nestful"
|
3
|
+
require "human_api/version"
|
4
|
+
require "human_api/config"
|
5
|
+
require "config/initializers/core_ext"
|
6
|
+
|
7
|
+
module HumanApi
|
8
|
+
|
9
|
+
@config = HumanApi::Config.new
|
10
|
+
|
11
|
+
def self.config
|
12
|
+
|
13
|
+
if block_given?
|
14
|
+
yield(@config)
|
15
|
+
@config.configure
|
16
|
+
else
|
17
|
+
@config
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
autoload :Human, 'human_api/human'
|
23
|
+
autoload :App, 'human_api/app'
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: human_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alessio Santo
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-02-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nestful
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: json
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.1
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.8.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
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
|
+
description: API client for HumanAPI
|
70
|
+
email:
|
71
|
+
- alessio.santo@pazienti.it
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- human_api.gemspec
|
82
|
+
- lib/config/initializers/core_ext.rb
|
83
|
+
- lib/human_api.rb
|
84
|
+
- lib/human_api/app.rb
|
85
|
+
- lib/human_api/config.rb
|
86
|
+
- lib/human_api/human.rb
|
87
|
+
- lib/human_api/version.rb
|
88
|
+
homepage: https://github.com/Pazienti/humanapi
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.2.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: API client for HumanAPI
|
112
|
+
test_files: []
|