mirror-api 0.0.4 → 0.0.5

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.
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
- ![Travis-ci](https://travis-ci.org/ciberch/mirror-api.png)
2
- ![Code Climate](https://codeclimate.com/github/ciberch/mirror-api.png)
1
+ [![Travis-ci](https://travis-ci.org/ciberch/mirror-api.png)](https://travis-ci.org/ciberch/mirror-api)
2
+
3
+ [![Code Climate](https://codeclimate.com/github/ciberch/mirror-api.png)](https://codeclimate.com/github/ciberch/mirror-api)
3
4
 
4
5
  # Mirror::Api
5
6
 
@@ -22,20 +23,23 @@ Or install it yourself as:
22
23
  ## Usage
23
24
 
24
25
  ``` ruby
25
- api = Mirror::Api::Timeline.new(token)
26
+
27
+ require "mirror-api"
28
+
29
+ api = Mirror::Api::Client.new(token)
26
30
 
27
31
  # Getting all the timeline items
28
- items = api.list
32
+ items = api.timeline.list
29
33
 
30
34
  # Insert a simple text item - https://developers.google.com/glass/timeline#inserting_a_simple_timeline_item
31
- item1 = api.create({text: "Hello Word"})
35
+ item1 = api.timeline.create({text: "Hello Word"})
32
36
 
33
37
  # Inserting an item with reply actions - https://developers.google.com/glass/timeline#user_interaction_with_menu_items
34
- item2 = api.create({text: "Hello Word", menu_items:[{action: "REPLY"}]})
38
+ item2 = api.timeline.create({text: "Hello Word", menu_items:[{action: "REPLY"}]})
35
39
 
36
- item2 = api.update(item2.id, {text: "Hello Again Word", menu_items:[{action: "REPLY"}]})
40
+ item2 = api.timeline.update(item2.id, {text: "Hello Again Word", menu_items:[{action: "REPLY"}]})
37
41
 
38
- api.delete(item2.id)
42
+ api.timeline.delete(item2.id)
39
43
  ```
40
44
 
41
45
  ## See Also
@@ -9,7 +9,7 @@ module Mirror
9
9
 
10
10
  attr_accessor :last_error, :logger, :host, :last_exception, :throw_on_fail, :response, :data, :creds
11
11
 
12
- def initialize(credentials, throw_on_fail=false, host="https://www.googleapis.com", logger=nil)
12
+ def initialize(credentials, throw_on_fail=true, host="https://www.googleapis.com", logger=nil)
13
13
  @creds = credentials
14
14
  @last_exception = nil
15
15
  @throw_on_fail = throw_on_fail
@@ -0,0 +1,26 @@
1
+ require_relative "resource"
2
+
3
+ module Mirror
4
+ module Api
5
+ class Client
6
+
7
+ def initialize(credentials)
8
+ @credentials = if credentials.is_a?(String)
9
+ {:token => credentials}
10
+ elsif credentials.is_a?(Hash)
11
+ credentials
12
+ end
13
+
14
+ raise "Invalid credentials #{credentials.inspect}" unless @credentials
15
+ end
16
+
17
+ def timeline
18
+ @timeline ||= Resource.new(@credentials)
19
+ end
20
+
21
+ def subscriptions
22
+ @subscriptions ||= Resource.new(@credentials, Request::SUBSCRIPTIONS)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ require_relative "base"
2
+
3
+ module Mirror
4
+ module Api
5
+ HOST = "https://www.googleapis.com"
6
+
7
+ class Request < Mirror::Api::Base
8
+ TIMELINE = "timeline"
9
+ SUBSCRIPTIONS = "subscriptions"
10
+
11
+ def initialize(creds, options={})
12
+ @resource = options[:resource] || TIMELINE
13
+ @id = options[:id]
14
+ @params = options[:params]
15
+ @expected_response = options[:expected_response]
16
+ host = options[:host] || HOST
17
+ super(creds, options[:raise_errors], host, options[:logger])
18
+ end
19
+
20
+ def invoke_url
21
+ @invoke_url ||="#{self.host}/mirror/v1/#{@resource}/#{@id ? @id : ''}"
22
+ end
23
+
24
+ def params
25
+ @params ||={}
26
+ end
27
+
28
+ def ret_val
29
+ Hashie::Mash.new(@data)
30
+ end
31
+
32
+ def expected_response
33
+ @expected_response
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,45 @@
1
+ require_relative "request"
2
+
3
+ module Mirror
4
+ module Api
5
+ class Resource
6
+
7
+ def initialize(credentials, resource_name=Request::TIMELINE)
8
+ @credentials = credentials
9
+ @resource_name = resource_name
10
+
11
+ raise "Invalid credentials #{credentials}" unless @credentials
12
+ end
13
+
14
+ def list(params={})
15
+ Request.new(@credentials, make_options(params)).get
16
+ end
17
+
18
+ def create(params)
19
+ Request.new(@credentials, make_options(params, 201)).post
20
+ end
21
+
22
+ def get(id, params=nil)
23
+ Request.new(@credentials, item_options(id, params)).get
24
+ end
25
+
26
+ def update(id, params)
27
+ # This may become patch later
28
+ Request.new(@credentials, item_options(id, params)).put
29
+ end
30
+
31
+ def delete(id)
32
+ Request.new(@credentials, item_options(id)).delete
33
+ end
34
+
35
+ private
36
+ def make_options(params=nil, status=200)
37
+ {:resource => @resource_name, :params => params, :expected_response => status}
38
+ end
39
+
40
+ def item_options(id, params=nil, status=200)
41
+ {:resource => @resource_name, :id => id, :params => params, :expected_response => status}
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,5 +1,5 @@
1
1
  module Mirror
2
2
  module Api
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
data/mirror-api.gemspec CHANGED
@@ -24,4 +24,5 @@ Gem::Specification.new do |gem|
24
24
 
25
25
  gem.add_development_dependency "rspec"
26
26
  gem.add_development_dependency "webmock"
27
+ gem.add_development_dependency "simplecov"
27
28
  end
@@ -0,0 +1,65 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe Mirror::Api::Client do
4
+ before do
5
+ @token = "my-token"
6
+ @api = Mirror::Api::Client.new(@token)
7
+ end
8
+
9
+ describe "timeline" do
10
+ describe "create" do
11
+ describe "inserting" do
12
+
13
+ context "with valid params" do
14
+ before do
15
+ @msg = "Hello world"
16
+
17
+ stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline/").
18
+ with(body: {text: @msg},
19
+ headers: json_post_request_headers).
20
+ to_return(status: 201,
21
+ body: fixture("timeline_item.json", true),
22
+ headers: JSON.parse(fixture("timeline_item_response_headers.json", true)))
23
+ end
24
+
25
+ it "should insert plain text items", :focus => true do
26
+ item = @api.timeline.create({text: @msg})
27
+ item.should_not be_nil
28
+ item.created.should == "2012-09-25T23:28:43.192Z" # see fixture
29
+ item.text.should == @msg
30
+ end
31
+ end
32
+
33
+ context "with invalid params" do
34
+ before do
35
+ @msg = "Hello world"
36
+
37
+ # TODO: Verify error code is 422
38
+ stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline/").
39
+ with(body: {random: "123"},
40
+ headers: json_post_request_headers).
41
+ to_return(status: 422, body: {}.to_json,
42
+ headers: {})
43
+ end
44
+
45
+ it "should not insert the item" do
46
+
47
+ item = @api.timeline.create({random: "123"})
48
+ item.should be_nil
49
+ end
50
+ end
51
+ end
52
+
53
+ def json_post_request_headers
54
+ {
55
+ 'Accept'=>'application/json',
56
+ 'Accept-Encoding'=>'gzip, deflate',
57
+ 'Authorization'=>"Bearer #{@token}",
58
+ 'Content-Length'=>/\d+/,
59
+ 'Content-Type'=>'application/x-www-form-urlencoded',
60
+ 'User-Agent'=>'Ruby'
61
+ }
62
+ end
63
+ end
64
+ end
65
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,10 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
1
3
  require "rspec"
2
4
  require 'webmock/rspec'
3
5
  require 'webmock'
4
6
 
5
- require_relative "../lib/mirror-api/timeline.rb"
7
+ require_relative "../lib/mirror-api/client.rb"
6
8
 
7
9
  def fixture_path
8
10
  File.expand_path("../fixtures", __FILE__)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mirror-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-04-22 00:00:00.000000000 Z
13
+ date: 2013-04-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rake
@@ -108,6 +108,22 @@ dependencies:
108
108
  - - ! '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ! '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
111
127
  description: Wrapper for Google Glass Mirror API v1
112
128
  email:
113
129
  - monica@crushpath.com
@@ -125,15 +141,15 @@ files:
125
141
  - example/.gitkeep
126
142
  - lib/mirror-api.rb
127
143
  - lib/mirror-api/base.rb
128
- - lib/mirror-api/timeline.rb
129
- - lib/mirror-api/timeline_item_request.rb
130
- - lib/mirror-api/timeline_request.rb
144
+ - lib/mirror-api/client.rb
145
+ - lib/mirror-api/request.rb
146
+ - lib/mirror-api/resource.rb
131
147
  - lib/mirror-api/version.rb
132
148
  - mirror-api.gemspec
149
+ - spec/client_spec.rb
133
150
  - spec/fixtures/timeline_item.json
134
151
  - spec/fixtures/timeline_item_response_headers.json
135
152
  - spec/spec_helper.rb
136
- - spec/timeline_spec.rb
137
153
  homepage: https://github.com/ciberch/mirror-api
138
154
  licenses: []
139
155
  post_install_message:
@@ -159,7 +175,7 @@ signing_key:
159
175
  specification_version: 3
160
176
  summary: https://developers.google.com/glass/v1/reference/
161
177
  test_files:
178
+ - spec/client_spec.rb
162
179
  - spec/fixtures/timeline_item.json
163
180
  - spec/fixtures/timeline_item_response_headers.json
164
181
  - spec/spec_helper.rb
165
- - spec/timeline_spec.rb
@@ -1,40 +0,0 @@
1
- require_relative "timeline_request"
2
- require_relative "timeline_item_request"
3
-
4
- module Mirror
5
- module Api
6
- class Timeline
7
-
8
- def initialize(credentials)
9
- @credentials = if credentials.is_a?(String)
10
- {:token => credentials}
11
- elsif credentials.is_a?(Hash)
12
- credentials
13
- end
14
-
15
- raise "Invalid credentials #{credentials.inspect}" unless @credentials
16
- end
17
-
18
- def list(params={})
19
- TimelineRequest.new(params, 200, @credentials).get
20
- end
21
-
22
- def create(params)
23
- TimelineRequest.new(params, 201, @credentials).post
24
- end
25
-
26
- def get(id, params=nil)
27
- TimelineItemRequest.new(id, params, 200, @credentials).get
28
- end
29
-
30
- def update(id, params)
31
- # This may become patch later
32
- TimelineItemRequest.new(id, params, 200, @credentials).put
33
- end
34
-
35
- def delete(id)
36
- TimelineItemRequest.new(id, nil, 200, @credentials).delete
37
- end
38
- end
39
- end
40
- end
@@ -1,33 +0,0 @@
1
- require_relative "base"
2
-
3
- module Mirror
4
- module Api
5
- HOST = "https://www.googleapis.com"
6
-
7
- class TimelineItemRequest < Mirror::Api::Base
8
-
9
- def initialize(id, params, expected_response, creds, raise_errors=false, host=Mirror::Api::HOST, logger=nil)
10
- @id = id
11
- @params = params
12
- @expected_response = expected_response
13
- super(creds, raise_errors)
14
- end
15
-
16
- def invoke_url
17
- @invoke_url ||="#{self.host}/mirror/v1/timeline/#{@id}"
18
- end
19
-
20
- def params
21
- @params ||={}
22
- end
23
-
24
- def ret_val
25
- Hashie::Mash.new(@data)
26
- end
27
-
28
- def expected_response
29
- @expected_response
30
- end
31
- end
32
- end
33
- end
@@ -1,32 +0,0 @@
1
- require_relative "base"
2
-
3
- module Mirror
4
- module Api
5
- HOST = "https://www.googleapis.com"
6
-
7
- class TimelineRequest < Mirror::Api::Base
8
-
9
- def initialize(params, expected_response, creds, raise_errors=false, host=Mirror::Api::HOST, logger=nil)
10
- @params = params
11
- @expected_response = expected_response
12
- super(creds, raise_errors)
13
- end
14
-
15
- def invoke_url
16
- @invoke_url ||="#{self.host}/mirror/v1/timeline"
17
- end
18
-
19
- def params
20
- @params ||={}
21
- end
22
-
23
- def ret_val
24
- Hashie::Mash.new(@data)
25
- end
26
-
27
- def expected_response
28
- @expected_response
29
- end
30
- end
31
- end
32
- end
@@ -1,64 +0,0 @@
1
- require_relative "spec_helper"
2
-
3
- describe Mirror::Api::Timeline do
4
- before do
5
- @token = "my-token"
6
- @api = Mirror::Api::Timeline.new(@token)
7
- end
8
-
9
- describe "create" do
10
- describe "inserting" do
11
-
12
- context "with valid params" do
13
- before do
14
- @msg = "Hello world"
15
-
16
- stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline").
17
- with(body: {text: @msg},
18
- headers: json_post_request_headers).
19
- to_return(status: 201,
20
- body: fixture("timeline_item.json", true),
21
- headers: JSON.parse(fixture("timeline_item_response_headers.json", true)))
22
- end
23
-
24
- it "should insert plain text items" do
25
- item = @api.create({text: @msg})
26
- item.should_not be_nil
27
- item.created.should == "2012-09-25T23:28:43.192Z" # see fixture
28
- item.text.should == @msg
29
- end
30
- end
31
-
32
- context "with invalid params" do
33
- before do
34
- @msg = "Hello world"
35
-
36
- # TODO: Verify error code is 422
37
- stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline").
38
- with(body: {random: "123"},
39
- headers: json_post_request_headers).
40
- to_return(status: 422, body: {}.to_json,
41
- headers: {})
42
- end
43
-
44
- it "should not insert the item" do
45
-
46
- item = @api.create({random: "123"})
47
- item.should be_nil
48
- end
49
- end
50
- end
51
-
52
- def json_post_request_headers
53
- {
54
- 'Accept'=>'application/json',
55
- 'Accept-Encoding'=>'gzip, deflate',
56
- 'Authorization'=>"Bearer #{@token}",
57
- 'Content-Length'=>/\d+/,
58
- 'Content-Type'=>'application/x-www-form-urlencoded',
59
- 'User-Agent'=>'Ruby'
60
- }
61
- end
62
- end
63
-
64
- end