storify 0.0.5 → 0.0.6
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.
- checksums.yaml +4 -4
- data/lib/storify/client.rb +17 -9
- data/lib/storify.rb +22 -20
- data/spec/client_auth_spec.rb +20 -7
- data/spec/client_spec.rb +18 -4
- data/spec/storify_spec.rb +64 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 488d1fb73f0c02593590f124002f103b0ed96fdd
|
4
|
+
data.tar.gz: 008e7f1650cd761627297de605f2fcfb94760e39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b5615dd1ca034c3478c3e41aed0e1f5508ec946c32b3abf58c57325dd3479806104278a9b02880ef00bf1f8a8c3fc56222efbbc54ffb4111e05b5dbd951b93d
|
7
|
+
data.tar.gz: d9db9630e971f4dca1cacf937fc2757fdc564a649d53c6f5430264573836ad5c858fb68ebbe5fc8d7b0d35d94d7a011c4413e09e2cc1267df706962995988f7a
|
data/lib/storify/client.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'json'
|
2
2
|
require 'rest-client'
|
3
|
-
#RestClient.log = './restclient.log'
|
3
|
+
#RestClient.log = './restclient.log'
|
4
4
|
|
5
5
|
class Storify::Client
|
6
6
|
attr_reader :api_key, :username, :token
|
@@ -10,16 +10,20 @@ class Storify::Client
|
|
10
10
|
@username = username
|
11
11
|
end
|
12
12
|
|
13
|
-
def auth(password)
|
14
|
-
endpoint = Storify::auth
|
13
|
+
def auth(password, options: {})
|
14
|
+
endpoint = Storify::endpoint(version: options[:version], method: :auth)
|
15
15
|
data = call(endpoint, :POST, {password: password})
|
16
16
|
@token = data['content']['_token']
|
17
17
|
|
18
18
|
self
|
19
19
|
end
|
20
20
|
|
21
|
-
def userstories(username = @username)
|
22
|
-
endpoint = Storify::
|
21
|
+
def userstories(username = @username, options: {})
|
22
|
+
endpoint = Storify::endpoint(version: options[:version],
|
23
|
+
protocol: options[:protocol],
|
24
|
+
method: :userstories,
|
25
|
+
params: {':username' => username})
|
26
|
+
|
23
27
|
pager = add_pagination
|
24
28
|
stories = []
|
25
29
|
|
@@ -37,8 +41,12 @@ class Storify::Client
|
|
37
41
|
stories
|
38
42
|
end
|
39
43
|
|
40
|
-
def story(slug, username = @username)
|
41
|
-
|
44
|
+
def story(slug, username = @username, options: {})
|
45
|
+
params = {':username' => username, ':slug' => slug}
|
46
|
+
endpoint = Storify::endpoint(version: options[:version],
|
47
|
+
protocol: options[:protocol],
|
48
|
+
method: :userstory,
|
49
|
+
params: params)
|
42
50
|
pager = add_pagination
|
43
51
|
story = nil
|
44
52
|
elements = []
|
@@ -89,10 +97,10 @@ class Storify::Client
|
|
89
97
|
end
|
90
98
|
rescue => e
|
91
99
|
raw = e.response
|
92
|
-
|
100
|
+
|
93
101
|
data = JSON.parse(e.response)
|
94
102
|
error = data['error']
|
95
|
-
raise Storify::ApiError.new(data['code'], error['message'], error['type'])
|
103
|
+
raise Storify::ApiError.new(data['code'], error['message'], error['type'])
|
96
104
|
end
|
97
105
|
|
98
106
|
JSON.parse(raw)
|
data/lib/storify.rb
CHANGED
@@ -1,28 +1,30 @@
|
|
1
1
|
module Storify
|
2
|
-
|
2
|
+
PROTOCOLS = {
|
3
|
+
:secure => 'https://',
|
4
|
+
:insecure => 'http://'
|
5
|
+
}
|
3
6
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
7
|
+
ENDPOINTS = {
|
8
|
+
:v1 => {
|
9
|
+
:base => 'api.storify.com/v1',
|
10
|
+
:auth => '/auth',
|
11
|
+
:userstories => '/stories/:username',
|
12
|
+
:userstory => '/stories/:username/:slug'
|
13
|
+
}
|
14
|
+
}
|
11
15
|
|
12
|
-
|
13
|
-
|
14
|
-
|
16
|
+
# Set protocol and endpoint defaults
|
17
|
+
PROTOCOLS.default = PROTOCOLS[:secure]
|
18
|
+
ENDPOINTS.default = ENDPOINTS[:v1]
|
19
|
+
ENDPOINTS[:v1].default = ENDPOINTS[:v1][:base]
|
15
20
|
|
16
|
-
def self.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
stories << "/#{username}"
|
22
|
-
end
|
21
|
+
def self.endpoint(version: :v1, protocol: :secure, method: :base, params: {})
|
22
|
+
uri = (method == :auth) ? PROTOCOLS[:secure] : PROTOCOLS[protocol]
|
23
|
+
uri += ENDPOINTS[version][:base]
|
24
|
+
uri += ENDPOINTS[version][method]
|
25
|
+
params.each_pair {|k,v| uri = uri.gsub(k,v) }
|
23
26
|
|
24
|
-
|
25
|
-
userstories(username) << "/#{slug}"
|
27
|
+
uri
|
26
28
|
end
|
27
29
|
end
|
28
30
|
|
data/spec/client_auth_spec.rb
CHANGED
@@ -1,17 +1,30 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Storify::Client do
|
4
4
|
before(:each) do
|
5
5
|
@client = Storify::Client.new(@api_key, @username)
|
6
6
|
end
|
7
7
|
|
8
|
-
|
8
|
+
context "Authentication" do
|
9
|
+
it "should retrieve an auth token on success" do
|
10
|
+
@client.auth(get_password).token.should_not be_nil
|
11
|
+
@client.authenticated.should be_true
|
12
|
+
end
|
9
13
|
|
10
|
-
|
11
|
-
|
12
|
-
|
14
|
+
it "should raise an API error on failure" do
|
15
|
+
expect{@client.auth('invalid_password')}.to raise_error(Storify::ApiError)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should accept endpoint options (version)" do
|
19
|
+
options = {:version => :v1}
|
20
|
+
@client.auth(get_password, options: options)
|
21
|
+
@client.authenticated.should be_true
|
22
|
+
end
|
13
23
|
|
14
|
-
|
15
|
-
|
24
|
+
it "should ignore endpoint options (protocol, method)" do
|
25
|
+
options = {:method => :unknown, :protocol => :insecure}
|
26
|
+
@client.auth(get_password, options: options)
|
27
|
+
@client.authenticated.should be_true
|
28
|
+
end
|
16
29
|
end
|
17
30
|
end
|
data/spec/client_spec.rb
CHANGED
@@ -9,12 +9,26 @@ describe Storify::Client do
|
|
9
9
|
@story = STDIN.gets.chomp
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
context "GET /stories/:username" do
|
13
|
+
it "should get all stories for a specific user" do
|
14
|
+
@client.userstories(@username).length.should == 2
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should accept endpoint options (version, protocol)" do
|
18
|
+
options = {:version => :v1, :protocol => :insecure}
|
19
|
+
@client.userstories(@username, options: options).length.should == 2
|
20
|
+
end
|
14
21
|
end
|
15
22
|
|
16
|
-
|
17
|
-
|
23
|
+
context "GET /stories/:username/:slug" do
|
24
|
+
it "should get a specific story for a user (all pages)" do
|
25
|
+
@client.story(@story).elements.length.should == 3
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should accept endpoint options (version, protocol)" do
|
29
|
+
options = {:version => :v1, :protocol => :insecure}
|
30
|
+
@client.story(@story, options: options).elements.length.should == 3
|
31
|
+
end
|
18
32
|
end
|
19
33
|
|
20
34
|
it "should allow a story to be serialized as text" do
|
data/spec/storify_spec.rb
CHANGED
@@ -1,36 +1,79 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Storify do
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
context "API Protocols:" do
|
5
|
+
it "should support http" do
|
6
|
+
Storify::PROTOCOLS[:insecure].should == "http://"
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should support secure http" do
|
10
|
+
Storify::PROTOCOLS[:secure].should == "https://"
|
11
|
+
end
|
7
12
|
|
8
|
-
|
9
|
-
|
10
|
-
|
13
|
+
it "should default to secure http" do
|
14
|
+
Storify::PROTOCOLS[:unknown].should == "https://"
|
15
|
+
end
|
11
16
|
end
|
12
17
|
|
13
|
-
|
14
|
-
|
15
|
-
|
18
|
+
context "API Versions:" do
|
19
|
+
it "should support Storify API v1" do
|
20
|
+
Storify::ENDPOINTS[:v1].is_a?(Hash).should be_true
|
21
|
+
end
|
16
22
|
|
17
|
-
Storify
|
18
|
-
|
23
|
+
it "should fallback to Storify API v1" do
|
24
|
+
Storify::ENDPOINTS[:unknown].is_a?(Hash).should be_true
|
25
|
+
end
|
19
26
|
end
|
20
27
|
|
21
|
-
|
22
|
-
|
23
|
-
|
28
|
+
context "API v1 Endpoints Sub-Paths:" do
|
29
|
+
it "should support a base URL for a version" do
|
30
|
+
Storify::ENDPOINTS[:v1][:base].should == "api.storify.com/v1"
|
31
|
+
end
|
24
32
|
|
25
|
-
|
26
|
-
|
27
|
-
|
33
|
+
it "should default to the base URL" do
|
34
|
+
Storify::ENDPOINTS[:unknown][:unknown].should == "api.storify.com/v1"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should support the Authentication endpoint" do
|
38
|
+
Storify::ENDPOINTS[:v1][:auth].should == "/auth"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should support the User Stories endpoint" do
|
42
|
+
Storify::ENDPOINTS[:v1][:userstories].should == "/stories/:username"
|
43
|
+
end
|
28
44
|
|
29
|
-
|
30
|
-
|
45
|
+
it "should support the Single Story endpoint" do
|
46
|
+
Storify::ENDPOINTS[:v1][:userstory].should == "/stories/:username/:slug"
|
47
|
+
end
|
31
48
|
end
|
32
49
|
|
33
|
-
|
34
|
-
|
50
|
+
context "API Endpoint URI Builder:" do
|
51
|
+
it "should allow dynamic protocol selection" do
|
52
|
+
Storify::endpoint(protocol: :secure).include?('https').should be_true
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should allow dynamic version selection" do
|
56
|
+
Storify::endpoint(version: :v1).include?('v1').should be_true
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should allow dynamic endpoint selection" do
|
60
|
+
Storify::endpoint(method: :auth).include?('/auth').should be_true
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should allow dynamic parameter substitution" do
|
64
|
+
params = {':username' => 'rtejpar', ':slug' => 'this-is-my-story'}
|
65
|
+
uri = Storify::endpoint(params: params, method: :userstory)
|
66
|
+
['rtejpar', 'this-is-my-story'].each {|s| uri.include?(s).should be_true }
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should force secure protocol for Authentication" do
|
70
|
+
Storify::endpoint(protocol: :secure, method: :auth).include?('https').should be_true
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should build the final uri based on dynamic configuration" do
|
74
|
+
params = {':username' => 'rtejpar'}
|
75
|
+
uri = Storify::endpoint(protocol: :insecure, method: :userstories, params: params)
|
76
|
+
uri.should == 'http://api.storify.com/v1/stories/rtejpar'
|
77
|
+
end
|
35
78
|
end
|
36
79
|
end
|