storify 0.0.4

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 054fc92b925bfaa5fe6f12055fb3edad0fc9edcf
4
+ data.tar.gz: 6c797051ce7a0808ee903c1df1ba6589c2789581
5
+ SHA512:
6
+ metadata.gz: 305a38acf3661181cc9ef7884d4b4a38f6fdd44b16a76835f5f1fe838ccb85d08138795191c08111be064512b69777d871bb695c3079ee9929347f752634db51
7
+ data.tar.gz: 0f16afc75eb29e7b1cf3f9e01e76b676d79393d67c47dd4d5d5db80a732b65017a8f8687e981d7cc3c9f8cb58ac4c8932ce96e3de8b6c9700eab4c54810690e9
@@ -0,0 +1,13 @@
1
+ class Storify::ApiError < StandardError
2
+ attr_reader :status, :type, :message
3
+
4
+ def initialize(status, message, type = nil)
5
+ @type = type
6
+ @status = status
7
+ @message = message
8
+ end
9
+
10
+ def to_s
11
+ "#{self.status} #{self.type} #{self.message}"
12
+ end
13
+ end
@@ -0,0 +1,67 @@
1
+ require 'json'
2
+ require 'rest-client'
3
+ #RestClient.log = './restclient.log'
4
+
5
+ class Storify::Client
6
+ attr_reader :api_key, :username, :token, :raw
7
+
8
+ def initialize(api_key, username)
9
+ @api_key = api_key
10
+ @username = username
11
+ end
12
+
13
+ def auth(password)
14
+ endpoint = Storify::auth
15
+ data = call(endpoint, :POST, {password: password})
16
+ @token = data['content']['_token']
17
+
18
+ self
19
+ end
20
+
21
+ def stories(slug: nil, world: false)
22
+ # filter from general to specific
23
+ endpoint = Storify::stories if world
24
+ endpoint = Storify::userstories(@username) unless world
25
+ endpoint = Storify::story(@username, slug) unless slug.nil?
26
+ call(endpoint, :GET)
27
+
28
+ self
29
+ end
30
+
31
+ def authenticated
32
+ !@token.nil?
33
+ end
34
+
35
+ def deserialize
36
+ JSON.parse(@raw)
37
+ end
38
+
39
+
40
+ private
41
+
42
+ def call(endpoint, verb, params = {}, opts = {})
43
+ @raw = nil
44
+
45
+ begin
46
+ # inject auth params automatically (if available)
47
+ params[:username] = @username
48
+ params[:api_key] = @api_key
49
+ params[:_token] = @token if authenticated
50
+
51
+ case verb
52
+ when :POST
53
+ @raw = RestClient.post endpoint, params, {:accept => :json}
54
+ else
55
+ @raw = RestClient.get endpoint, {:params => params}
56
+ end
57
+ rescue => e
58
+ @raw = e.response
59
+
60
+ data = JSON.parse(e.response)
61
+ error = data['error']
62
+ raise Storify::ApiError.new(data['code'], error['message'], error['type'])
63
+ end
64
+
65
+ deserialize
66
+ end
67
+ end
data/lib/storify.rb ADDED
@@ -0,0 +1,30 @@
1
+ module Storify
2
+ BASE_URL = 'https://api.storify.com'
3
+
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
11
+
12
+ def self.auth
13
+ versioned_api << "/auth"
14
+ end
15
+
16
+ def self.stories
17
+ versioned_api << "/stories"
18
+ end
19
+
20
+ def self.userstories(username)
21
+ stories << "/#{username}"
22
+ end
23
+
24
+ def self.story(username, slug)
25
+ userstories(username) << "/#{slug}"
26
+ end
27
+ end
28
+
29
+ require 'storify/client'
30
+ require 'storify/apierror'
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Storify::Client -- Authentication" do
4
+ before(:each) do
5
+ @client = Storify::Client.new(@api_key, @username)
6
+ end
7
+
8
+ it "should retrieve an auth token on success" do
9
+
10
+ @client.auth(get_password).token.should_not be_nil
11
+ @client.authenticated.should be_true
12
+ end
13
+
14
+ it "should raise an API error on failure" do
15
+ expect{@client.auth('invalid_password')}.to raise_error(Storify::ApiError)
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Storify::Client do
4
+ before(:all) do
5
+ @client = Storify::Client.new(@api_key, @username)
6
+ @client.auth(get_password)
7
+ end
8
+
9
+ it "should get the first page of a users stories" do
10
+ @client.stories(:world => false).deserialize.should_not be_nil
11
+ end
12
+
13
+ it "should get a specific "
14
+
15
+ end
@@ -0,0 +1,27 @@
1
+ require 'storify'
2
+ require 'io/console'
3
+
4
+ RSpec.configure do |config|
5
+ config.mock_with :rspec
6
+ config.add_setting :api_key, :default => ''
7
+ config.add_setting :api_usr, :default => ''
8
+
9
+ # load user-specific keys or fallback to defaults
10
+ config.before(:all) do
11
+ begin
12
+ require File.dirname(__FILE__) + '/.userkey.rb'
13
+ @api_key = API_KEY
14
+ @username = USERNAME
15
+ rescue LoadError
16
+ @api_key = RSpec.configuration.api_key
17
+ @username = RSpec.configuration.api_usr
18
+ end
19
+
20
+ end
21
+ end
22
+
23
+ def get_password
24
+ puts "Enter Storify Password:"
25
+ STDOUT.flush
26
+ STDIN.noecho(&:gets).chomp
27
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
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
7
+
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'
11
+ end
12
+
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'
16
+
17
+ Storify::versioned_api(:secure => false,
18
+ :version => 2).should eq 'http://api.storify.com/v2'
19
+ end
20
+
21
+ it "should retrieve the auth API endpoint" do
22
+ Storify::auth.should eq 'https://api.storify.com/v1/auth'
23
+ end
24
+
25
+ it "should retrieve the stories API endpoint" do
26
+ Storify::stories.should eq 'https://api.storify.com/v1/stories'
27
+ end
28
+
29
+ it "should retrieve the user-specific stories API endpoint" do
30
+ Storify::userstories(@username).should eq "https://api.storify.com/v1/stories/#{@username}"
31
+ end
32
+
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"
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: storify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Rizwan Tejpar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.7.7
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.7.7
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 2.14.1
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.14.1
55
+ description: Ruby Wrapper of Storify REST API -- work-in-progress
56
+ email: rtejpar@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/storify/apierror.rb
62
+ - lib/storify/client.rb
63
+ - lib/storify.rb
64
+ - spec/client_auth_spec.rb
65
+ - spec/client_spec.rb
66
+ - spec/spec_helper.rb
67
+ - spec/storify_spec.rb
68
+ homepage: https://github.com/natural-affinity/storify
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.1.11
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Storify API
92
+ test_files:
93
+ - spec/client_auth_spec.rb
94
+ - spec/client_spec.rb
95
+ - spec/spec_helper.rb
96
+ - spec/storify_spec.rb