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 +1 -1
- data/lib/mirror-api/base.rb +3 -1
- data/lib/mirror-api/oauth.rb +26 -0
- data/lib/mirror-api/version.rb +1 -1
- data/spec/fixtures/oauth_response.json +6 -0
- data/spec/oauth_spec.rb +35 -0
- data/spec/spec_helper.rb +1 -0
- metadata +6 -1
data/lib/mirror-api.rb
CHANGED
data/lib/mirror-api/base.rb
CHANGED
@@ -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
|
data/lib/mirror-api/version.rb
CHANGED
data/spec/oauth_spec.rb
ADDED
@@ -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
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
|
+
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
|