ihome 0.0.1

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
+ SHA1:
3
+ metadata.gz: d5a4fd072c567eba5f8dbb79eacf92163e043d67
4
+ data.tar.gz: 8e0672bf5eadbd93527ffb7d795613e49c72e4d4
5
+ SHA512:
6
+ metadata.gz: c4c6159eca26a4a49af746c04a432c32514624f74816890702dd4c61043455e434e7c6ec7ddb0073a655d7c0d9d1f3f291bc3675f97dfb99536f3d6afa7df3ac
7
+ data.tar.gz: a55afbc618d1acbde29c95a11415c963f696308c8a898a5d9a434f45113a78c14e72deace76171698928a29a9e529afca842f368db4158a1acc690e6159c683d
data/.git_ignore ADDED
@@ -0,0 +1 @@
1
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ihome.gemspec
4
+ gemspec
5
+
6
+ gem 'httparty'
data/Gemfile.lock ADDED
@@ -0,0 +1,51 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ihome (0.0.1)
5
+ oauth2
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.2.5)
11
+ faraday (0.9.1)
12
+ multipart-post (>= 1.2, < 3)
13
+ httparty (0.13.5)
14
+ json (~> 1.8)
15
+ multi_xml (>= 0.5.2)
16
+ json (1.8.3)
17
+ jwt (1.5.1)
18
+ multi_json (1.11.2)
19
+ multi_xml (0.5.5)
20
+ multipart-post (2.0.0)
21
+ oauth2 (1.0.0)
22
+ faraday (>= 0.8, < 0.10)
23
+ jwt (~> 1.0)
24
+ multi_json (~> 1.3)
25
+ multi_xml (~> 0.5)
26
+ rack (~> 1.2)
27
+ rack (1.6.4)
28
+ rake (10.4.2)
29
+ rspec (3.3.0)
30
+ rspec-core (~> 3.3.0)
31
+ rspec-expectations (~> 3.3.0)
32
+ rspec-mocks (~> 3.3.0)
33
+ rspec-core (3.3.2)
34
+ rspec-support (~> 3.3.0)
35
+ rspec-expectations (3.3.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.3.0)
38
+ rspec-mocks (3.3.2)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.3.0)
41
+ rspec-support (3.3.0)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler (~> 1.6)
48
+ httparty
49
+ ihome!
50
+ rake
51
+ rspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Shaun McGeever
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,29 @@
1
+ # iHome
2
+
3
+ Authorization Client for iHome integrations
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ihome'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ihome
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/ihome/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/ihome.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ihome/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ihome"
8
+ spec.version = Ihome::VERSION
9
+ spec.authors = ["Shaun McGeever"]
10
+ spec.email = ["shaun@winkapp.com"]
11
+ spec.summary = %q{Ruby Client for iHome API}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_dependency "oauth2"
26
+ end
data/lib/ihome.rb ADDED
@@ -0,0 +1,30 @@
1
+ require "ihome/client"
2
+ require "ihome/configuration"
3
+ require "ihome/version"
4
+
5
+ module Ihome
6
+
7
+ class ConfigurationMissingError < RuntimeError
8
+ def initialize(msg = "Please use Ihome.configure before trying to initialize a client")
9
+ super(msg)
10
+ end
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration
15
+ end
16
+
17
+ def self.configure(options = {})
18
+ @configuration = Configuration.new(options)
19
+ # Force refresh
20
+ @oauth2_client = nil
21
+ end
22
+
23
+ def self.authorization_url(state)
24
+ self.oauth2_client.auth_code.authorize_url(
25
+ response_type: 'code',
26
+ state: state,
27
+ )
28
+ end
29
+
30
+ end
@@ -0,0 +1,110 @@
1
+ require 'oauth2'
2
+ require 'ihome'
3
+ require 'ihome/configuration'
4
+ require 'json'
5
+ require 'httparty'
6
+
7
+ module Ihome
8
+ class Client
9
+ public
10
+ def initialize(opts)
11
+ if opts[:auth_code]
12
+ get_token!(opts[:auth_code])
13
+ else
14
+ authenticate!(opts[:access_token], opts[:refresh_token])
15
+ end
16
+ end
17
+
18
+ def get_token!(code)
19
+ @token = Ihome.oauth2_client.auth_code.get_token(code, {
20
+ redirect_uri: Ihome.configuration.redirect_uri,
21
+ })
22
+ end
23
+
24
+ def authenticate!(access_token, refresh_token = nil)
25
+ @token = OAuth2::AccessToken.new(Ihome.oauth2_client, access_token, refresh_token: refresh_token)
26
+ end
27
+
28
+ def refresh!
29
+ @token = @token.refresh!
30
+ end
31
+
32
+ def access_token
33
+ @token.token
34
+ end
35
+
36
+ def refresh_token
37
+ @token.refresh_token
38
+ end
39
+
40
+ def evrythng_api_key
41
+ # This will only be stored after exchanging an authcode.
42
+ # To get a new evrythng API key, reauthenticate with an authentication code
43
+ @token.params['evrythng_api_key']
44
+ end
45
+ end
46
+
47
+ class EvrythngClient
48
+ def initialize(opts)
49
+ @evrythng_api_key = opts[:evrythng_api_key]
50
+ end
51
+
52
+ def default_headers
53
+ {
54
+ 'authorization' => @evrythng_api_key,
55
+ 'content-type' => 'application/json',
56
+ 'accept' => 'application/json'
57
+ }
58
+ end
59
+
60
+ def get_devices
61
+ response = HTTParty.get("https://api.evrythng.com/thngs", {
62
+ :headers => default_headers
63
+ })
64
+ response.body
65
+ end
66
+
67
+ def send_powered_command(thng_id, state, source, outlet_number = 1)
68
+ body = {
69
+ 'type'=> '_ControlPowerState',
70
+ 'thng'=> thng_id,
71
+ 'custom_fields'=> {
72
+ 'outlet_number'=> outlet_number,
73
+ 'state'=> state
74
+ },
75
+ 'context'=> {
76
+ 'source'=> source
77
+ }
78
+ }.to_json
79
+
80
+ HTTParty.post("https://api.evrythng.com/actions/_ControlPowerState", {
81
+ headers: default_headers,
82
+ body: body
83
+ })
84
+ end
85
+
86
+ def update_name(thng_id, name)
87
+ body = {
88
+ "name"=> name
89
+ }.to_json
90
+
91
+ HTTParty.put("https://api.evrythng.com/thngs/#{thng_id}", {
92
+ headers: default_headers,
93
+ body: body
94
+ })
95
+ end
96
+ end
97
+
98
+ protected
99
+
100
+ def self.oauth2_client
101
+ @oauth2_client ||= begin
102
+ raise ConfigurationMissingError unless @configuration
103
+ OAuth2::Client.new(@configuration.client_id, @configuration.client_secret, {
104
+ site: @configuration.site,
105
+ authorize_url: @configuration.authorize_url,
106
+ token_url: @configuration.token_url,
107
+ })
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,16 @@
1
+ module Ihome
2
+ class Configuration
3
+ attr_accessor :site, :authorize_url, :token_url, :client_id, :client_secret, :redirect_uri, :api_prefix
4
+
5
+ def initialize(options)
6
+ self.site = options[:site] || 'http://ihome.ihomeaudio.biz'
7
+ self.authorize_url = options[:authorize_url] || '/api/oauth/authorize/'
8
+ self.token_url = options[:token_url] || '/api/oauth/token'
9
+ self.api_prefix = options[:api_prefix] || ''
10
+ #no default values, you need to set it !
11
+ self.client_id = options[:client_id]
12
+ self.client_secret = options[:client_secret]
13
+ self.redirect_uri = options[:redirect_uri]
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module Ihome
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ihome::Client do
4
+ before do
5
+ Ihome.configure(
6
+ client_id: 'CLIENT_ID',
7
+ client_secret: 'CLIENT_SECRET',
8
+ site: 'https://api.example.com',
9
+ redirect_uri: 'https://api.redirect.com'
10
+ )
11
+ end
12
+
13
+ # TODO: Add VCR to make an acceptable test
14
+ # it 'accepts an auth_code' do
15
+ # p = -> { Ihome::Client.new(auth_code: 'auth_code') }
16
+ # expect(p).not_to raise_error
17
+ # end
18
+
19
+ it 'accepts an access token with a refresh token' do
20
+ p = -> { Ihome::Client.new(access_token: 'my_access_token', refresh_token: 'my_refresh_token') }
21
+ expect(p).not_to raise_error
22
+ end
23
+
24
+
25
+ it 'can create evrythng client with api key' do
26
+ p = -> { Ihome::EvrythngClient.new(evrythng_api_key: 'my_evrythng_api_key') }
27
+ expect(p).not_to raise_error
28
+ end
29
+
30
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ihome do
4
+ before do
5
+ Ihome.configure(
6
+ client_id: 'CLIENT_ID',
7
+ client_secret: 'CLIENT_SECRET',
8
+ site: 'https://api.example.com',
9
+ redirect_uri: 'https://api.redirect.com'
10
+ )
11
+ end
12
+
13
+ it 'has a version number' do
14
+ expect(Ihome::VERSION).to_not be nil
15
+ end
16
+
17
+ it 'can be configured' do
18
+ Ihome.configuration.client_id.should eql('CLIENT_ID')
19
+ Ihome.configuration.client_secret.should eql('CLIENT_SECRET')
20
+ Ihome.configuration.site.should eql('https://api.example.com')
21
+ Ihome.configuration.redirect_uri.should eql('https://api.redirect.com')
22
+ end
23
+
24
+ it 'can return an authorization_url' do
25
+ Ihome.authorization_url('state').should == 'https://api.example.com/api/oauth/authorize/?client_id=CLIENT_ID&response_type=code&state=state'
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'ihome'
5
+ Ihome.configure({})
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ihome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shaun McGeever
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-08-18 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.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: oauth2
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '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'
69
+ description: ''
70
+ email:
71
+ - shaun@winkapp.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .git_ignore
77
+ - .rspec
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - ihome.gemspec
84
+ - lib/ihome.rb
85
+ - lib/ihome/client.rb
86
+ - lib/ihome/configuration.rb
87
+ - lib/ihome/version.rb
88
+ - spec/client_spec.rb
89
+ - spec/ihome_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.0.14
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Ruby Client for iHome API
115
+ test_files:
116
+ - spec/client_spec.rb
117
+ - spec/ihome_spec.rb
118
+ - spec/spec_helper.rb