google-api-middle_man 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5afcff094f5049791c43c3a6c48ca55954b74948
4
+ data.tar.gz: c9adbbc80576746f427e7f40305dd257a21bb87c
5
+ SHA512:
6
+ metadata.gz: 68bb54e6e9e0616be530ebefc00f7fdf9a83b7c7328e1cd337333c2f909da80550451630fc22320371173de862b88320d6764e3b94d486bcda7e3f5c3e87b872
7
+ data.tar.gz: 9d3b175573f860bb175c09d91a0755845816e1962b6c83a2c4714a9891c1268e24a75830b79e814dffa84f3483a40f5c74c018325f1d13d4b8e71252a665430a
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ .ruby-version
19
+
data/.ruby-gemset ADDED
@@ -0,0 +1,2 @@
1
+ google-api-middle_man
2
+
@@ -0,0 +1,2 @@
1
+ ruby-2.0.0-p195
2
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in google-api-middle_man.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2013 Shaun Dern
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.
23
+
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # GoogleTravelAgent
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'google-api-middle_man'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install google-api-middle_man
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
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 new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'rubygems'
4
+ require 'bundler/gem_tasks'
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc 'Default: run unit tests.'
8
+ task :default => :spec
9
+
10
+ desc "Run all specs"
11
+ RSpec::Core::RakeTask.new do |t|
12
+ t.pattern = 'spec/**/*_spec.rb'
13
+ t.rspec_opts = '--color'
14
+ end
15
+
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'google-api-middle_man'
4
+
5
+ email = "google_service_account@email"
6
+ calendarId = "google_calendar_id"
7
+
8
+ google_config = {
9
+ application_name: "google_project_name",
10
+ key_location: 'client.p12',
11
+ google_service_email: email
12
+ }
13
+
14
+ travel_agent = GoogleAPIMiddleMan::Agent.new(google_config)
15
+ travel_agent.calendar_events(calendar_id)
16
+
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'google-api-middle_man/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "google-api-middle_man"
8
+ spec.version = GoogleTravelAgent::VERSION
9
+ spec.authors = ["Shaun Dern"]
10
+ spec.email = ["shaun@substantial.com"]
11
+ spec.description = %q{Simplify the Google API}
12
+ spec.summary = %q{Simplify the Google API}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency 'rspec'
24
+
25
+ spec.add_dependency "google-api-client", "~> 0.6.3"
26
+ end
27
+
@@ -0,0 +1,3 @@
1
+ module GoogleTravelAgent
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,64 @@
1
+ require "google-api-middle_man/version"
2
+
3
+ module GoogleAPIMiddleMan
4
+ class MissingConfigOptions < Exception; end
5
+
6
+ class Agent
7
+ require 'google/api_client'
8
+
9
+ attr_reader :client
10
+
11
+ def initialize(config)
12
+ [:application_name, :key_location, :google_service_email].each do |key|
13
+ unless config.has_key?(key)
14
+ raise MissingConfigOptions, "config is missing #{key}"
15
+ end
16
+ end
17
+
18
+ @application_name = config[:application_name]
19
+ @key_location = config[:key_location]
20
+ @google_service_email = config[:google_service_email]
21
+
22
+ @client = Google::APIClient.new(application_name: @application_name)
23
+ end
24
+
25
+ def calendar_events(calendar_id)
26
+ @client.authorization = service_account.authorize
27
+
28
+ options = {'calendarId' => calendar_id}
29
+ result = @client.execute(api_method: calendar_service.events.list, parameters: options)
30
+
31
+ result.data
32
+ end
33
+
34
+ private
35
+
36
+ def default_scope
37
+ 'https://www.googleapis.com/auth/prediction'
38
+ end
39
+
40
+ def calendar_scope
41
+ 'https://www.googleapis.com/auth/calendar.readonly'
42
+ end
43
+
44
+ def scopes
45
+ s = []
46
+ s << default_scope
47
+ s << calendar_scope
48
+ s
49
+ end
50
+
51
+ def api_key
52
+ @api_key ||= Google::APIClient::PKCS12.load_key(@key_location, 'notasecret')
53
+ end
54
+
55
+ def service_account
56
+ Google::APIClient::JWTAsserter.new(@google_service_email, scopes, api_key)
57
+ end
58
+
59
+ def calendar_service
60
+ @client.discovered_api('calendar', 'v3')
61
+ end
62
+ end
63
+ end
64
+
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe GoogleAPIMiddleMan::Agent do
4
+
5
+ let(:google_config) {
6
+ {
7
+ application_name: 'Foo App',
8
+ key_location: 'config/client.p12',
9
+ google_service_email: 'test@example.com'
10
+ }
11
+ }
12
+
13
+ before do
14
+ Google::APIClient.stub(:new) { double.as_null_object }
15
+ end
16
+
17
+ it "should require a configuration object" do
18
+ expect { GoogleAPIMiddleMan::Agent.new('foo') }.to raise_error
19
+ end
20
+
21
+ it "should require a complete config" do
22
+ incomplete_config = google_config
23
+ incomplete_config.delete(:key_location)
24
+ expect { GoogleAPIMiddleMan::Agent.new(incomplete_config) }.to raise_error GoogleAPIMiddleMan::MissingConfigOptions
25
+ end
26
+
27
+ it "should create a client with the correct application name" do
28
+ Google::APIClient.should_receive(:new).with(application_name: 'Foo App')
29
+ GoogleAPIMiddleMan::Agent.new(google_config)
30
+ end
31
+
32
+ describe "#calendar_events" do
33
+ let(:calendar_hash) { double }
34
+ let(:travel_agent) { GoogleAPIMiddleMan::Agent.new(google_config) }
35
+ let(:mock_service_account) { double(authorize: "authorization") }
36
+
37
+ before do
38
+ Google::APIClient.stub(:new) { double(:authorization= => :nil, execute: double(data: calendar_hash))}
39
+ travel_agent.stub(:api_key) { 'api_key' }
40
+ travel_agent.stub(:service_account) { mock_service_account }
41
+ travel_agent.stub(:calendar_service) { c = double
42
+ c.stub_chain(:events, :list) { "events_list_api" }
43
+ c
44
+ }
45
+ end
46
+
47
+ it "should require a calendar id" do
48
+ expect { travel_agent.calendar_events }.to raise_error ArgumentError
49
+ end
50
+
51
+ it "should set the client authorization" do
52
+ travel_agent.client.should_receive(:authorization=).with("authorization")
53
+ travel_agent.calendar_events('calendar_id')
54
+ end
55
+
56
+ it "should return hash of event info from Google" do
57
+ travel_agent.calendar_events('calendar_id').should == calendar_hash
58
+ end
59
+ end
60
+
61
+ describe "private methods" do
62
+ let(:travel_agent) { GoogleAPIMiddleMan::Agent.new(google_config) }
63
+
64
+ describe "scopes" do
65
+ let(:default_scope) { 'https://www.googleapis.com/auth/prediction' }
66
+ let(:calendar_scope) { 'https://www.googleapis.com/auth/calendar.readonly' }
67
+ let(:scopes) { [default_scope, calendar_scope]}
68
+
69
+ it "should have a default scope" do
70
+ travel_agent.send(:default_scope).should == default_scope
71
+ end
72
+
73
+ it "should have a have a calendar_scope" do
74
+ travel_agent.send(:calendar_scope).should == calendar_scope
75
+ end
76
+
77
+ it "should have a have a scopes" do
78
+ travel_agent.send(:scopes).should == scopes
79
+ end
80
+ end
81
+
82
+ describe "#api_key" do
83
+ before do
84
+ Google::APIClient::PKCS12.stub(:load_key) { 'some key' }
85
+ end
86
+ it "should load a valid api key" do
87
+ Google::APIClient::PKCS12.should_receive(:load_key).with(google_config[:key_location], 'notasecret')
88
+ travel_agent.send(:api_key).should == 'some key'
89
+ end
90
+
91
+ it "should memoize the key" do
92
+ travel_agent.send(:api_key)
93
+ Google::APIClient::PKCS12.should_not_receive(:load_key)
94
+ travel_agent.send(:api_key)
95
+ end
96
+ end
97
+
98
+ describe "#service_account" do
99
+ let(:mock_service) { double }
100
+
101
+ before do
102
+ travel_agent.stub(:api_key) { "api_key" }
103
+ travel_agent.stub(:scopes) { "scopes" }
104
+ Google::APIClient::JWTAsserter.stub(:new) { mock_service }
105
+ end
106
+
107
+ it "should return a new google service account" do
108
+ Google::APIClient::JWTAsserter.should_receive(:new).with(
109
+ google_config[:google_service_email],
110
+ "scopes",
111
+ "api_key"
112
+ )
113
+ travel_agent.send(:service_account).should == mock_service
114
+ end
115
+ end
116
+
117
+ describe "#calendar_service" do
118
+
119
+ let(:mock_calendar_service) { double }
120
+
121
+ before do
122
+ travel_agent.client.stub(:discovered_api) { mock_calendar_service }
123
+ end
124
+
125
+ it "should return a calendar service" do
126
+ travel_agent.send(:calendar_service).should == mock_calendar_service
127
+ end
128
+
129
+ it "should discover calendar endpoint" do
130
+ travel_agent.client.should_receive(:discovered_api).with('calendar', 'v3')
131
+ travel_agent.send(:calendar_service)
132
+ end
133
+ end
134
+ end
135
+
136
+ end
137
+
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ SPEC_ROOT = File.dirname(__FILE__)
5
+
6
+ RSpec.configure do |config|
7
+ config.mock_with :rspec
8
+ end
9
+
10
+ Dir[File.join(SPEC_ROOT, '../lib/**/*.rb')].each{ |file| require file }
11
+
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google-api-middle_man
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shaun Dern
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-19 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
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: google-api-client
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.6.3
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.6.3
69
+ description: Simplify the Google API
70
+ email:
71
+ - shaun@substantial.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .ruby-gemset
78
+ - .ruby-version.example
79
+ - Gemfile
80
+ - LICENSE
81
+ - README.md
82
+ - Rakefile
83
+ - examples/google_calendar_example.rb
84
+ - google-api-middle_man.gemspec
85
+ - lib/google-api-middle_man.rb
86
+ - lib/google-api-middle_man/version.rb
87
+ - spec/lib/google-api-middle_man_spec.rb
88
+ - spec/spec_helper.rb
89
+ homepage: ''
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.0.3
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Simplify the Google API
113
+ test_files:
114
+ - spec/lib/google-api-middle_man_spec.rb
115
+ - spec/spec_helper.rb