prismic.io 1.0.0.rc3 → 1.0.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 406a3849f94ecdfd3516db68fa2ee0d36ecf1c6a
4
- data.tar.gz: f6988c94934d3a900af92cf6973dbb3c8d46d135
3
+ metadata.gz: 2e41d8f8a5ceae95d4e2c3ee77967e1357b2162f
4
+ data.tar.gz: 162469c32efb11a20f358f21baf3b2e5d8dc1fbd
5
5
  SHA512:
6
- metadata.gz: 4f69e94c2b76f58b859ab8fac98c5a7a2ad09f2330fc05e1cf66daebf2d5913ac4d7f514d65f44833a4c10d8e98a3253bd0c6c7716e3bb9351a8218269dfc86b
7
- data.tar.gz: 2f09cbb004acd083ffb16ab02dc19ab2e4862d019b105456d658de1f066528c1151a9c41695573dc6d98f7e147bf7eecb4bb9b68d4b55b6939bab041d781aa1e
6
+ metadata.gz: 491f3c1ef1646963a35e95e8a4a5f0aea0f5ec0ed78a92a0ecfc2e7666b40afbd39a459abf2701f61aed920d649704a3ecbde5415666a2534366aa313f53e5d0
7
+ data.tar.gz: a91edff448c3a0762e1cb4b76b5700ae4bde77a8f04902fa87eb39ab451e01a9a464bea8d09e84ce371b758c7aca3863264262c8d2128aa20cb5ecb331a4f01f
@@ -1,5 +1,7 @@
1
1
  language: ruby
2
2
  rvm:
3
+ - 2.1.1
4
+ - 2.1.0
3
5
  - 2.0.0
4
6
  - 1.9.3
5
7
  - 1.9.2
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- prismic.io (1.0.0.rc3)
4
+ prismic.io (1.0.0.rc4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -55,6 +55,18 @@ module Prismic
55
55
  API.start(url, opts)
56
56
  end
57
57
 
58
+ def self.oauth_initiate_url(url, oauth_opts, api_opts=nil)
59
+ api_opts ||= {}
60
+ api_opts = {access_token: opts} if api_opts.is_a?(String)
61
+ API.oauth_initiate_url(url, oauth_opts, api_opts)
62
+ end
63
+
64
+ def self.oauth_check_token(url, oauth_opts, api_opts=nil)
65
+ api_opts ||= {}
66
+ api_opts = {access_token: api_opts} if api_opts.is_a?(String)
67
+ API.oauth_check_token(url, oauth_opts, api_opts)
68
+ end
69
+
58
70
  class ApiData
59
71
  attr_accessor :refs, :bookmarks, :types, :tags, :forms
60
72
  end
@@ -366,12 +378,25 @@ module Prismic
366
378
  # - body: returns the response's body (as String)
367
379
  def get(uri, data={}, headers={})
368
380
  uri = URI(uri) if uri.is_a?(String)
369
- uri.query = url_encode(data)
381
+ add_query(uri, data)
370
382
  http = Net::HTTP.new(uri.host, uri.port)
371
383
  http.use_ssl = uri.scheme =~ /https/i
372
384
  http.get(uri.request_uri, headers)
373
385
  end
374
386
 
387
+ # Performs a POST call and returns the result
388
+ #
389
+ # The result must respond to
390
+ # - code: returns the response's HTTP status code (as number or String)
391
+ # - body: returns the response's body (as String)
392
+ def post(uri, data={}, headers={})
393
+ uri = URI(uri) if uri.is_a?(String)
394
+ add_query(uri, data)
395
+ http = Net::HTTP.new(uri.host, uri.port)
396
+ http.use_ssl = uri.scheme =~ /https/i
397
+ http.post(uri.path, uri.query, headers)
398
+ end
399
+
375
400
  def url_encode(data)
376
401
  # Can't use URI.encode_www_form (doesn't support multi-values in 1.9.2)
377
402
  encode = ->(k, v){ "#{k}=#{CGI::escape(v)}" }
@@ -383,6 +408,14 @@ module Prismic
383
408
  end
384
409
  }.join("&")
385
410
  end
411
+
412
+ private
413
+
414
+ def add_query(uri, query)
415
+ query = url_encode(query)
416
+ query = "#{uri.query}&#{query}"if uri.query && !uri.query.empty?
417
+ uri.query = query
418
+ end
386
419
  end
387
420
  end
388
421
 
@@ -2,6 +2,8 @@
2
2
  module Prismic
3
3
  class API
4
4
  @@warned_create_search_form = false
5
+ @@warned_oauth_initiate_url = false
6
+ @@warned_oauth_check_token = false
5
7
  attr_reader :json, :access_token, :http_client
6
8
  attr_accessor :refs, :bookmarks, :forms, :tags, :types, :oauth
7
9
 
@@ -68,8 +70,24 @@ module Prismic
68
70
  data = {}
69
71
  data["access_token"] = access_token if access_token
70
72
  res = http_client.get(url, data, 'Accept' => 'application/json')
71
- raise PrismicWSConnectionError, res unless res.code.to_s == '200'
72
- res
73
+ case res.code
74
+ when '200'
75
+ res
76
+ when '401', '403'
77
+ begin
78
+ json = JSON.load(res.body)
79
+ raise PrismicWSAuthError.new(
80
+ json['error'],
81
+ json['oauth_initiate'],
82
+ json['oauth_token'],
83
+ http_client
84
+ )
85
+ rescue => e
86
+ raise PrismicWSConnectionError.new(res, e)
87
+ end
88
+ else
89
+ raise PrismicWSConnectionError, res
90
+ end
73
91
  end
74
92
 
75
93
  def self.start(url, opts={})
@@ -104,30 +122,48 @@ module Prismic
104
122
  }]
105
123
  api.tags = data['tags']
106
124
  api.types = data['types']
107
- api.oauth = OAuth.new(data['oauth_initiate'], data['oauth_token'])
125
+ api.oauth = OAuth.new(data['oauth_initiate'], data['oauth_token'], http_client)
108
126
  }
109
127
  end
110
128
 
129
+ def self.oauth_initiate_url(url, oauth_opts, api_opts={})
130
+ oauth =
131
+ begin
132
+ api = self.start(url, api_opts)
133
+ api.oauth
134
+ rescue PrismicWSAuthError => e
135
+ e.oauth
136
+ end
137
+ oauth.initiate_url(oauth_opts)
138
+ end
139
+
111
140
  def oauth_initiate_url(opts)
112
- oauth.initiate + "?" + {
113
- "client_id" => opts.fetch(:client_id),
114
- "redirect_uri" => opts.fetch(:redirect_uri),
115
- "scope" => opts.fetch(:scope),
116
- }.map{|kv| kv.map{|e| CGI.escape(e) }.join("=") }.join("&")
141
+ if !@@warned_oauth_initiate_url
142
+ warn "[DEPRECATION] Method `API#oauth_initiate_url` is deprecated. " +
143
+ "Please use `Prismic::API.oauth_initiate_url` instead."
144
+ @@warned_oauth_initiate_url = true
145
+ end
146
+ oauth.initiate_url(opts)
117
147
  end
118
148
 
119
- def oauth_check_token(params)
120
- uri = URI(oauth.token)
121
- res = Net::HTTP.post_form(uri, params)
122
- if res.code == '200'
149
+ def self.oauth_check_token(url, oauth_params, api_opts={})
150
+ oauth =
123
151
  begin
124
- JSON.parse(res.body)['access_token']
125
- rescue Exception => e
126
- raise PrismicWSConnectionError.new(res, e)
152
+ api = self.start(url, api_opts)
153
+ api.oauth
154
+ rescue PrismicWSAuthError => e
155
+ e.oauth
127
156
  end
128
- else
129
- raise PrismicWSConnectionError, res
157
+ oauth.check_token(oauth_params)
158
+ end
159
+
160
+ def oauth_check_token(params)
161
+ if !@@warned_oauth_check_token
162
+ warn "[DEPRECATION] Method `API#oauth_check_token` is deprecated. " +
163
+ "Please use `Prismic::API.oauth_check_token` instead."
164
+ @@warned_oauth_check_token = true
130
165
  end
166
+ oauth.check_token(params)
131
167
  end
132
168
 
133
169
  private
@@ -140,12 +176,42 @@ module Prismic
140
176
  end
141
177
  end
142
178
 
179
+ class PrismicWSAuthError < Error
180
+ attr_reader :error, :oauth
181
+ def initialize(error, oauth_initialize, oauth_token, http_client)
182
+ super("Can't connect to Prismic's API: #{error}")
183
+ @error = error
184
+ @oauth = OAuth.new(oauth_initialize, oauth_token, http_client)
185
+ end
186
+ end
187
+
143
188
  class OAuth
144
- attr_reader :initiate, :token
145
- def initialize(initiate, token)
189
+ attr_reader :initiate, :token, :http_client
190
+ def initialize(initiate, token, http_client)
191
+ @http_client = http_client
146
192
  @initiate = initiate
147
193
  @token = token
148
194
  end
195
+ def initiate_url(opts)
196
+ initiate + "?" + {
197
+ "client_id" => opts.fetch(:client_id),
198
+ "redirect_uri" => opts.fetch(:redirect_uri),
199
+ "scope" => opts.fetch(:scope),
200
+ }.map{|kv| kv.map{|e| CGI.escape(e) }.join("=") }.join("&")
201
+ end
202
+ def check_token(params)
203
+ res = http_client.post(token, params)
204
+ if res.code == '200'
205
+ begin
206
+ JSON.parse(res.body)['access_token']
207
+ rescue Exception => e
208
+ raise PrismicWSConnectionError.new(res, e)
209
+ end
210
+ else
211
+ raise PrismicWSConnectionError, res
212
+ end
213
+ end
149
214
  end
215
+
150
216
  end
151
217
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Prismic
3
3
 
4
- VERSION = "1.0.0.rc3"
4
+ VERSION = "1.0.0.rc4"
5
5
 
6
6
  end
@@ -0,0 +1,121 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ describe 'OAuth' do
5
+
6
+ OAUTH_INITIATE_OPTS = {
7
+ client_id: "client_id",
8
+ redirect_uri: "http://myapp.com/callback",
9
+ scope: "master+releases",
10
+ }.freeze
11
+
12
+ OAUTH_CHECKTOKEN_OPTS = {
13
+ grant_type: "authorization_code",
14
+ code: "thecode",
15
+ redirect_uri: "http://myapp.com/callback",
16
+ client_id: "client_id",
17
+ client_secret: "client_secret",
18
+ }
19
+
20
+ describe "Public API" do
21
+
22
+ PUBLIC_URL = "https://lesbonneschoses.prismic.io/api"
23
+
24
+ it "should accept unauthenticated requests" do
25
+ expect {
26
+ Prismic.api(PUBLIC_URL, nil)
27
+ }.not_to raise_error()
28
+ end
29
+
30
+ describe "oauth_initiate_url" do
31
+
32
+ it "should returns the URL" do
33
+ url = Prismic::API.oauth_initiate_url(PUBLIC_URL, OAUTH_INITIATE_OPTS)
34
+ expected_url = [
35
+ "https://lesbonneschoses.prismic.io/auth?client_id=client_id",
36
+ "redirect_uri=http%3A%2F%2Fmyapp.com%2Fcallback",
37
+ "scope=master%2Breleases",
38
+ ].join("&")
39
+ url.should == expected_url
40
+ end
41
+
42
+ end
43
+
44
+ describe "oauth_check_token" do
45
+
46
+ it "should returns the URL" do
47
+ expect {
48
+ Prismic.oauth_check_token(PUBLIC_URL, OAUTH_CHECKTOKEN_OPTS)
49
+ }.to raise_error(
50
+ # we don't want to get a PrismicWSAuthError error here
51
+ Prismic::API::PrismicWSConnectionError,
52
+ "Can't connect to Prismic's API: 400 Bad Request"
53
+ )
54
+ end
55
+
56
+ end
57
+
58
+ end
59
+
60
+ describe "Private API" do
61
+
62
+ PRIVATE_URL = "https://privaterepository.prismic.io/api"
63
+
64
+ it "should deny unauthenticated requests" do
65
+ expect {
66
+ Prismic.api(PRIVATE_URL, nil)
67
+ }.to raise_error(Prismic::API::PrismicWSAuthError)
68
+ end
69
+
70
+ def catch_wserror(url)
71
+ Prismic.api(url, nil)
72
+ rescue Prismic::API::PrismicWSAuthError => e
73
+ e
74
+ end
75
+
76
+ it "should provide the error" do
77
+ error = catch_wserror(PRIVATE_URL)
78
+ error.error.should == "Invalid access token"
79
+ end
80
+
81
+ it "should provide oauth entry points" do
82
+ error = catch_wserror(PRIVATE_URL)
83
+ error.oauth.initiate.should == "https://privaterepository.prismic.io/auth"
84
+ end
85
+
86
+ it "should provide oauth token" do
87
+ error = catch_wserror(PRIVATE_URL)
88
+ error.oauth.token.should == "https://privaterepository.prismic.io/auth/token"
89
+ end
90
+
91
+ describe "oauth_initiate_url" do
92
+
93
+ it "should returns the URL" do
94
+ url = Prismic.oauth_initiate_url(PRIVATE_URL, OAUTH_INITIATE_OPTS)
95
+ expected_url = [
96
+ "https://privaterepository.prismic.io/auth?client_id=client_id",
97
+ "redirect_uri=http%3A%2F%2Fmyapp.com%2Fcallback",
98
+ "scope=master%2Breleases",
99
+ ].join("&")
100
+ url.should == expected_url
101
+ end
102
+
103
+ end
104
+
105
+ describe "oauth_check_token" do
106
+
107
+ it "should returns the URL" do
108
+ expect {
109
+ Prismic::API.oauth_check_token(PRIVATE_URL, OAUTH_CHECKTOKEN_OPTS)
110
+ }.to raise_error(
111
+ # we don't want to get a PrismicWSAuthError error here
112
+ Prismic::API::PrismicWSConnectionError,
113
+ "Can't connect to Prismic's API: 400 Bad Request"
114
+ )
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+
121
+ end
@@ -24,7 +24,7 @@ describe 'Api' do
24
24
  'form3' => Prismic::Form.new(@api, 'form3', {}, nil, nil, nil, nil),
25
25
  'form4' => Prismic::Form.new(@api, 'form4', {}, nil, nil, nil, nil),
26
26
  }
27
- api.oauth = Prismic::API::OAuth.new(@oauth_initiate_url, "https://lesbonneschoses.prismic.io/auth/token")
27
+ api.oauth = Prismic::API::OAuth.new(@oauth_initiate_url, "https://lesbonneschoses.prismic.io/auth/token", Prismic::DefaultHTTPClient)
28
28
  }
29
29
  end
30
30
 
@@ -234,7 +234,7 @@ describe 'Api' do
234
234
  @scope = "none"
235
235
  end
236
236
  def oauth_initiate_url
237
- @api.oauth_initiate_url({
237
+ Prismic.oauth_initiate_url("https://lesbonneschoses.prismic.io/api", {
238
238
  client_id: @client_id,
239
239
  redirect_uri: @redirect_uri,
240
240
  scope: @scope,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prismic.io
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc3
4
+ version: 1.0.0.rc4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Étienne Vallette d'Osia
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-20 00:00:00.000000000 Z
12
+ date: 2014-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -102,6 +102,7 @@ files:
102
102
  - spec/fragments_spec.rb
103
103
  - spec/json_parsers_spec.rb
104
104
  - spec/lesbonneschoses_spec.rb
105
+ - spec/oauth_spec.rb
105
106
  - spec/prismic_spec.rb
106
107
  - spec/responses_mocks/api.json
107
108
  - spec/responses_mocks/document.json
@@ -140,6 +141,7 @@ test_files:
140
141
  - spec/fragments_spec.rb
141
142
  - spec/json_parsers_spec.rb
142
143
  - spec/lesbonneschoses_spec.rb
144
+ - spec/oauth_spec.rb
143
145
  - spec/prismic_spec.rb
144
146
  - spec/responses_mocks/api.json
145
147
  - spec/responses_mocks/document.json