mirror-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mirror-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Monica Wilkinson
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,38 @@
1
+ # Mirror::Api
2
+
3
+ Mirror API client
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mirror-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mirror-api
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+
23
+ # Insert a simple text item - https://developers.google.com/glass/timeline#inserting_a_simple_timeline_item
24
+ timeline_api = Mirror::Api::Timeline.new({:token => access_token})
25
+ item = timeline_api.post({:text => "Hello Word"})
26
+
27
+ # Inserting an item with reply actions - https://developers.google.com/glass/timeline#user_interaction_with_menu_items
28
+ item2 = timeline_api.post({:text => "Hello Word", :menu_items => [{:action => "REPLY"}]})
29
+
30
+ ```
31
+
32
+ ## Contributing
33
+
34
+ 1. Fork it
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/example/.gitkeep ADDED
@@ -0,0 +1 @@
1
+
data/lib/mirror-api.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "mirror-api/version"
2
+ require "mirror-api/client"
3
+
@@ -0,0 +1,131 @@
1
+ module Mirror
2
+ module Api
3
+
4
+ class Base
5
+
6
+ attr_accessor :last_error, :logger, :host, :last_exception, :throw_on_fail, :response, :data, :creds
7
+
8
+ def initialize(credentials, throw_on_fail=false, host, logger=nil)
9
+ @creds = credentials
10
+ @last_exception = nil
11
+ @throw_on_fail = throw_on_fail
12
+ @host = host
13
+ @logger = logger
14
+ @last_error = nil
15
+ end
16
+
17
+ public
18
+ def post(json=false)
19
+ do_verb(:post, json)
20
+ end
21
+
22
+ def put(json=false)
23
+ do_verb(:put, json)
24
+ end
25
+
26
+ def delete
27
+ get_verb(:delete)
28
+ end
29
+
30
+ def get
31
+ get_verb
32
+ end
33
+
34
+ protected
35
+
36
+ def invoke_url; end
37
+ def params; end
38
+ def send_error; end
39
+
40
+ def handle_http_response(response, request, result, &block)
41
+ @request = request
42
+ case response.code
43
+ when 422
44
+ @logger.error("ERROR - Rejected #{request.inspect} to #{self.invoke_url} with params #{self.params}. Response is #{response.body}")
45
+ response
46
+ else
47
+ response.return!(request, result, &block)
48
+ end
49
+ end
50
+
51
+ def successful_response?
52
+ @response and @response.code == 200
53
+ end
54
+
55
+ def ret_val
56
+ @data
57
+ end
58
+
59
+ def headers
60
+ if @creds and @creds['token']
61
+ {
62
+ "Accept" => "application/json",
63
+ "Content-type" => "application/json",
64
+ "Authorization" => "Bearer #{@creds['token']}"
65
+ }
66
+ end
67
+ end
68
+
69
+ def handle_response
70
+ if successful_response?
71
+ ret_val
72
+ else
73
+ send_error
74
+ end
75
+ end
76
+
77
+ def handle_error(error_desc, msg, errors, validation_error=nil, params={})
78
+ @last_error = error_desc.dup
79
+ @last_error[:errors] = errors
80
+ @last_error[:validation_error] = validation_error if validation_error
81
+ msg += " with params #{params}" if params.keys.count > 0
82
+ @logger.warn(msg) if @logger
83
+ raise error_desc[:message] if throw_on_fail
84
+ end
85
+
86
+ def handle_exception(error_desc, msg, ex, params={})
87
+ @last_exception = ex
88
+ @last_error = error_desc
89
+ msg += " with params #{params}" if params.keys.count > 0
90
+ msg += " due to #{ex}.\n" + ex.backtrace.join("\n")
91
+ @logger.error(msg) if @logger
92
+
93
+ raise ex if throw_on_fail
94
+ return nil
95
+ end
96
+
97
+ def set_data
98
+ @data = JSON.parse(@response.body) if @response and @response.body
99
+ end
100
+
101
+ def handle_http_exception(verb, ex)
102
+ handle_exception("INTERNAL_ERROR", "Could not #{verb} to #{self.invoke_url}", ex, self.params)
103
+ end
104
+
105
+ def do_verb(verb=:post, json=false)
106
+ begin
107
+ data = json ? self.params.to_json : self.params
108
+ @response = RestClient.send(verb, self.invoke_url, data, self.headers) do |response, request, result, &block|
109
+ handle_http_response(response, request, result, &block)
110
+ end
111
+ set_data
112
+ handle_response
113
+ rescue => ex
114
+ return handle_http_exception(verb, ex)
115
+ end
116
+ end
117
+
118
+ def get_verb(verb=:get)
119
+ begin
120
+ @response = RestClient.send(verb, self.invoke_url, self.headers)
121
+ set_data
122
+ handle_response
123
+ rescue => ex
124
+ return handle_http_exception(verb, ex)
125
+ end
126
+ end
127
+ end
128
+
129
+
130
+ end
131
+ end
@@ -0,0 +1,18 @@
1
+ module Mirror
2
+ module Api
3
+ class Timeline < Mirror::Api::Base
4
+
5
+ def invoke_url
6
+ @invoke_url ||="#{self.host}/mirror/v1/timeline"
7
+ end
8
+
9
+ def params
10
+ @params
11
+ end
12
+
13
+ def successful_response?
14
+ @response and @response.code == 201
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,5 @@
1
+ module Mirror
2
+ module Api
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mirror-api/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "mirror-api"
8
+ gem.version = Mirror::Api::VERSION
9
+ gem.authors = ["Monica Wilkinson"]
10
+ gem.email = ["ciberch@gmail.com"]
11
+ gem.description = %q{Wrapper for Google Glass Mirror API v1}
12
+ gem.summary = %q{https://developers.google.com/glass/v1/reference/}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency "rest-client"
21
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mirror-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Monica Wilkinson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rest-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Wrapper for Google Glass Mirror API v1
31
+ email:
32
+ - ciberch@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - example/.gitkeep
43
+ - lib/mirror-api.rb
44
+ - lib/mirror-api/base.rb
45
+ - lib/mirror-api/timeline.rb
46
+ - lib/mirror-api/version.rb
47
+ - mirror-api.gemspec
48
+ homepage: ''
49
+ licenses: []
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: https://developers.google.com/glass/v1/reference/
72
+ test_files: []