mirror-api 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/lib/mirror-api.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require "mirror-api/version"
2
2
  require "mirror-api/base"
3
3
  require "mirror-api/client"
4
-
4
+ require "mirror-api/oauth"
@@ -43,7 +43,9 @@ module Mirror
43
43
 
44
44
  def invoke_url; end
45
45
  def params; end
46
- def send_error; end
46
+ def send_error
47
+ raise "Bad"
48
+ end
47
49
 
48
50
  def handle_http_response(response, request, result, &block)
49
51
  @request = request
@@ -0,0 +1,26 @@
1
+ module Mirror
2
+ module Api
3
+ class OAuth
4
+ attr_reader :client_id, :client_secret, :refresh_token
5
+ def initialize(client_id, client_secret, refresh_token)
6
+ @client_id = client_id
7
+ @client_secret = client_secret
8
+ @refresh_token = refresh_token
9
+ end
10
+
11
+ def get_access_token
12
+ req = Net::HTTP::Post.new("/o/oauth2/token")
13
+ req.set_form_data(client_id: self.client_id, client_secret: self.client_secret, refresh_token: self.refresh_token, grant_type: "refresh_token")
14
+ res = Net::HTTP.start("accounts.google.com", use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) { |http| http.request(req) }
15
+
16
+ begin
17
+ result = JSON.parse(res.body)
18
+ result["access_token"]
19
+ rescue
20
+ raise JSON.parse(res.body)['error']['message']
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -1,5 +1,5 @@
1
1
  module Mirror
2
2
  module Api
3
- VERSION = "0.0.8"
3
+ VERSION = "0.0.9"
4
4
  end
5
5
  end
@@ -0,0 +1,6 @@
1
+ {
2
+ "access_token": "BOOYAH",
3
+ "token_type": "Bearer",
4
+ "expires_in": 3600,
5
+ "id_token": "AAAAAHHHHHYYYEAAAAHLOOONGTOKEN"
6
+ }
@@ -0,0 +1,35 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe Mirror::Api::OAuth do
4
+ before do
5
+ @oauth = Mirror::Api::OAuth.new('client_id', 'client_secret', 'refresh_token')
6
+ end
7
+ describe "get_access_token" do
8
+ context "has valid params" do
9
+ before do
10
+ @body = {client_id: @oauth.client_id, client_secret: @oauth.client_secret, refresh_token: @oauth.refresh_token, grant_type: "refresh_token"}
11
+
12
+ stub_request(:post, "https://accounts.google.com/o/oauth2/token").
13
+ with(body: @body,
14
+ headers: json_post_request_headers(@body.to_json)).
15
+ to_return(status: 200,
16
+ body: fixture("oauth_response.json", true),
17
+ headers: {})
18
+ end
19
+ it "should return a valid access_token" do
20
+ access_token = @oauth.get_access_token
21
+ access_token.should == "BOOYAH"
22
+ end
23
+ end
24
+
25
+ end
26
+
27
+ def json_post_request_headers(body)
28
+ {
29
+ 'Accept'=>'*/*',
30
+ 'Content-Type'=>'application/x-www-form-urlencoded',
31
+ 'User-Agent'=>'Ruby'
32
+ }
33
+ end
34
+
35
+ end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,7 @@ require 'webmock'
6
6
  require 'pry'
7
7
 
8
8
  require_relative "../lib/mirror-api/client.rb"
9
+ require_relative "../lib/mirror-api/oauth.rb"
9
10
 
10
11
  def fixture_path
11
12
  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.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -206,6 +206,7 @@ files:
206
206
  - lib/mirror-api.rb
207
207
  - lib/mirror-api/base.rb
208
208
  - lib/mirror-api/client.rb
209
+ - lib/mirror-api/oauth.rb
209
210
  - lib/mirror-api/request.rb
210
211
  - lib/mirror-api/resource.rb
211
212
  - lib/mirror-api/version.rb
@@ -216,10 +217,12 @@ files:
216
217
  - spec/fixtures/files/fry.png
217
218
  - spec/fixtures/locations_item.json
218
219
  - spec/fixtures/locations_list.json
220
+ - spec/fixtures/oauth_response.json
219
221
  - spec/fixtures/timeline_item.json
220
222
  - spec/fixtures/timeline_item_attachments_item.json
221
223
  - spec/fixtures/timeline_item_attachments_list.json
222
224
  - spec/fixtures/timeline_item_response_headers.json
225
+ - spec/oauth_spec.rb
223
226
  - spec/spec_helper.rb
224
227
  homepage: https://github.com/ciberch/mirror-api
225
228
  licenses: []
@@ -252,8 +255,10 @@ test_files:
252
255
  - spec/fixtures/files/fry.png
253
256
  - spec/fixtures/locations_item.json
254
257
  - spec/fixtures/locations_list.json
258
+ - spec/fixtures/oauth_response.json
255
259
  - spec/fixtures/timeline_item.json
256
260
  - spec/fixtures/timeline_item_attachments_item.json
257
261
  - spec/fixtures/timeline_item_attachments_list.json
258
262
  - spec/fixtures/timeline_item_response_headers.json
263
+ - spec/oauth_spec.rb
259
264
  - spec/spec_helper.rb