storify 0.0.5 → 0.0.6

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: 1079798438c428ba6b4ef587746d20cfa36ab175
4
- data.tar.gz: ad39c541755df5151b1af5c24724709466590657
3
+ metadata.gz: 488d1fb73f0c02593590f124002f103b0ed96fdd
4
+ data.tar.gz: 008e7f1650cd761627297de605f2fcfb94760e39
5
5
  SHA512:
6
- metadata.gz: b9d4855d72e86d8415f82e2efeed524c6ee36500fe4fa1b92bd905332e67887fcf70dd5c43d76e36a6be0a0d45cdd64be7e23202563b34fdfc2113c5e3966350
7
- data.tar.gz: a651172d7c64799ebc0f950ed471439fb3030f06f68037e283fda0a168f3a98f45027b4c760eb995839aab87e1c274442ebcb7988a0c63b62779ac08b4c90da6
6
+ metadata.gz: 7b5615dd1ca034c3478c3e41aed0e1f5508ec946c32b3abf58c57325dd3479806104278a9b02880ef00bf1f8a8c3fc56222efbbc54ffb4111e05b5dbd951b93d
7
+ data.tar.gz: d9db9630e971f4dca1cacf937fc2757fdc564a649d53c6f5430264573836ad5c858fb68ebbe5fc8d7b0d35d94d7a011c4413e09e2cc1267df706962995988f7a
@@ -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::userstories(username)
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
- endpoint = Storify::story(username, slug)
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
- BASE_URL = 'https://api.storify.com'
2
+ PROTOCOLS = {
3
+ :secure => 'https://',
4
+ :insecure => 'http://'
5
+ }
3
6
 
4
- def self.api(secure = true)
5
- (secure ? 'https' : 'http') + '://api.storify.com'
6
- end
7
-
8
- def self.versioned_api(secure: true, version: 1)
9
- api(secure) << "/v#{version}"
10
- end
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
- def self.auth
13
- versioned_api << "/auth"
14
- end
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.stories
17
- versioned_api << "/stories"
18
- end
19
-
20
- def self.userstories(username)
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
- def self.story(username, slug)
25
- userstories(username) << "/#{slug}"
27
+ uri
26
28
  end
27
29
  end
28
30
 
@@ -1,17 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "Storify::Client -- Authentication" do
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
- it "should retrieve an auth token on success" do
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
- @client.auth(get_password).token.should_not be_nil
11
- @client.authenticated.should be_true
12
- end
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
- it "should raise an API error on failure" do
15
- expect{@client.auth('invalid_password')}.to raise_error(Storify::ApiError)
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
- it "should get all stories for a specific user" do
13
- @client.userstories(@username).length.should == 2
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
- it "should get a specific story for a user (all pages)" do
17
- @client.story(@story).elements.length.should == 3
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
- it "should default to un-versioned https API access" do
5
- Storify::api.should eq 'https://api.storify.com'
6
- end
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
- it "should allow explicit un-versioned https or http for API access" do
9
- Storify::api(false).should eq 'http://api.storify.com'
10
- Storify::api(true).should eq 'https://api.storify.com'
13
+ it "should default to secure http" do
14
+ Storify::PROTOCOLS[:unknown].should == "https://"
15
+ end
11
16
  end
12
17
 
13
- it "should allow dynamic versioned http or https API access" do
14
- Storify::versioned_api.should eq 'https://api.storify.com/v1'
15
- Storify::versioned_api(:version => 2).should eq 'https://api.storify.com/v2'
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::versioned_api(:secure => false,
18
- :version => 2).should eq 'http://api.storify.com/v2'
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
- it "should retrieve the auth API endpoint" do
22
- Storify::auth.should eq 'https://api.storify.com/v1/auth'
23
- end
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
- it "should retrieve the stories API endpoint" do
26
- Storify::stories.should eq 'https://api.storify.com/v1/stories'
27
- end
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
- it "should retrieve the user-specific stories API endpoint" do
30
- Storify::userstories(@username).should eq "https://api.storify.com/v1/stories/#{@username}"
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
- it "should retrieve a user-specific story API endpoint" do
34
- Storify::story(@username, "mystory").should eq "https://api.storify.com/v1/stories/#{@username}/mystory"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rizwan Tejpar