storify 0.0.4 → 0.0.5
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 +51 -18
- data/lib/storify/element.rb +52 -0
- data/lib/storify/story.rb +39 -0
- data/lib/storify.rb +2 -0
- data/spec/client_spec.rb +13 -3
- metadata +35 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1079798438c428ba6b4ef587746d20cfa36ab175
|
4
|
+
data.tar.gz: ad39c541755df5151b1af5c24724709466590657
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9d4855d72e86d8415f82e2efeed524c6ee36500fe4fa1b92bd905332e67887fcf70dd5c43d76e36a6be0a0d45cdd64be7e23202563b34fdfc2113c5e3966350
|
7
|
+
data.tar.gz: a651172d7c64799ebc0f950ed471439fb3030f06f68037e283fda0a168f3a98f45027b4c760eb995839aab87e1c274442ebcb7988a0c63b62779ac08b4c90da6
|
data/lib/storify/client.rb
CHANGED
@@ -3,7 +3,7 @@ require 'rest-client'
|
|
3
3
|
#RestClient.log = './restclient.log'
|
4
4
|
|
5
5
|
class Storify::Client
|
6
|
-
attr_reader :api_key, :username, :token
|
6
|
+
attr_reader :api_key, :username, :token
|
7
7
|
|
8
8
|
def initialize(api_key, username)
|
9
9
|
@api_key = api_key
|
@@ -18,29 +18,62 @@ class Storify::Client
|
|
18
18
|
self
|
19
19
|
end
|
20
20
|
|
21
|
-
def
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
endpoint = Storify::story(@username, slug) unless slug.nil?
|
26
|
-
call(endpoint, :GET)
|
21
|
+
def userstories(username = @username)
|
22
|
+
endpoint = Storify::userstories(username)
|
23
|
+
pager = add_pagination
|
24
|
+
stories = []
|
27
25
|
|
28
|
-
|
26
|
+
while data = call(endpoint, :GET, pager)
|
27
|
+
content = data['content']
|
28
|
+
break if content['stories'].length == 0
|
29
|
+
|
30
|
+
content['stories'].each do |s|
|
31
|
+
stories << Storify::Story.new(s)
|
32
|
+
end
|
33
|
+
|
34
|
+
pager[:page] += 1
|
35
|
+
end
|
36
|
+
|
37
|
+
stories
|
38
|
+
end
|
39
|
+
|
40
|
+
def story(slug, username = @username)
|
41
|
+
endpoint = Storify::story(username, slug)
|
42
|
+
pager = add_pagination
|
43
|
+
story = nil
|
44
|
+
elements = []
|
45
|
+
|
46
|
+
while data = call(endpoint, :GET, pager)
|
47
|
+
story = Storify::Story.new(data['content']) if story.nil?
|
48
|
+
break if data['content']['elements'].length == 0
|
49
|
+
|
50
|
+
# create elements
|
51
|
+
data['content']['elements'].each do |e|
|
52
|
+
story.add_element(Storify::Element.new(e))
|
53
|
+
end
|
54
|
+
|
55
|
+
pager[:page] += 1
|
56
|
+
end
|
57
|
+
|
58
|
+
story
|
29
59
|
end
|
30
60
|
|
31
61
|
def authenticated
|
32
62
|
!@token.nil?
|
33
63
|
end
|
34
64
|
|
35
|
-
def
|
36
|
-
|
65
|
+
def add_pagination(page = 1, per_page = 20)
|
66
|
+
params = {}
|
67
|
+
params[:page] = page
|
68
|
+
params[:per_page] = per_page
|
69
|
+
params
|
37
70
|
end
|
38
71
|
|
39
72
|
|
40
|
-
private
|
41
|
-
|
73
|
+
private
|
74
|
+
|
42
75
|
def call(endpoint, verb, params = {}, opts = {})
|
43
|
-
|
76
|
+
raw = nil
|
44
77
|
|
45
78
|
begin
|
46
79
|
# inject auth params automatically (if available)
|
@@ -50,18 +83,18 @@ class Storify::Client
|
|
50
83
|
|
51
84
|
case verb
|
52
85
|
when :POST
|
53
|
-
|
54
|
-
|
55
|
-
|
86
|
+
raw = RestClient.post endpoint, params, {:accept => :json}
|
87
|
+
when :GET
|
88
|
+
raw = RestClient.get endpoint, {:params => params}
|
56
89
|
end
|
57
90
|
rescue => e
|
58
|
-
|
91
|
+
raw = e.response
|
59
92
|
|
60
93
|
data = JSON.parse(e.response)
|
61
94
|
error = data['error']
|
62
95
|
raise Storify::ApiError.new(data['code'], error['message'], error['type'])
|
63
96
|
end
|
64
97
|
|
65
|
-
|
98
|
+
JSON.parse(raw)
|
66
99
|
end
|
67
100
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
# todo: split logic into separate source types
|
5
|
+
class Storify::Element
|
6
|
+
attr_reader :type, :source, :link, :published, :author
|
7
|
+
attr_accessor :desc
|
8
|
+
|
9
|
+
def initialize(content)
|
10
|
+
@type = content['type']
|
11
|
+
@source = content['source']['name']
|
12
|
+
@link = content['permalink']
|
13
|
+
@published = DateTime.parse(content['posted_at'])
|
14
|
+
|
15
|
+
case @type
|
16
|
+
when 'image'
|
17
|
+
@desc = content['data'][@type]['caption']
|
18
|
+
|
19
|
+
if content['data'].has_key?('oembed')
|
20
|
+
@author = content['data']['oembed']['author_name']
|
21
|
+
else
|
22
|
+
@author = content['attribution']['name']
|
23
|
+
end
|
24
|
+
when 'text'
|
25
|
+
@desc = content['data']['text']
|
26
|
+
|
27
|
+
doc = Nokogiri::HTML(@desc)
|
28
|
+
@desc = doc.xpath("//text()").to_s
|
29
|
+
else
|
30
|
+
@desc = content['data'][@type]['description']
|
31
|
+
@author = content['source']['username']
|
32
|
+
@author = "@" + @author if @source.downcase == 'twitter'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
out = ''
|
38
|
+
|
39
|
+
case @source.downcase
|
40
|
+
when 'storify'
|
41
|
+
out << "\n#{@desc}\n"
|
42
|
+
out << ('-' * @desc.length) + "\n\n" if @desc.length < 50
|
43
|
+
when 'twitter'
|
44
|
+
out << "[#{@published.to_date.to_s}] #{@author}: #{@link}\n"
|
45
|
+
else
|
46
|
+
|
47
|
+
out << "#{@author} [#{@published.to_date.to_s}]: #{@link}\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
out
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class Storify::Story
|
4
|
+
attr_reader :slug, :title, :desc, :link, :published, :author, :elements
|
5
|
+
|
6
|
+
def initialize(content)
|
7
|
+
@slug = content['slug']
|
8
|
+
@title = content['title']
|
9
|
+
@desc = content['description']
|
10
|
+
@link = content['permalink']
|
11
|
+
@author = content['author']['username']
|
12
|
+
|
13
|
+
unless content['date']['published'].nil?
|
14
|
+
@published = DateTime.parse(content['date']['published'])
|
15
|
+
end
|
16
|
+
|
17
|
+
@elements = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_element(element)
|
21
|
+
@elements << element
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
published = @published.nil? ? 'unpublished' : @published.to_date.to_s
|
26
|
+
|
27
|
+
out = "\n#{@title}\n"
|
28
|
+
out << ('-' * @title.length.to_i) + "\n"
|
29
|
+
out << "Date: #{published}\n"
|
30
|
+
out << "Author: #{@author}\n"
|
31
|
+
out << "Link: #{@link}\n"
|
32
|
+
out << "\n#{@desc} \n"
|
33
|
+
|
34
|
+
# serialize elements
|
35
|
+
elements.each {|e| out << e.to_s }
|
36
|
+
|
37
|
+
out
|
38
|
+
end
|
39
|
+
end
|
data/lib/storify.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -4,12 +4,22 @@ describe Storify::Client do
|
|
4
4
|
before(:all) do
|
5
5
|
@client = Storify::Client.new(@api_key, @username)
|
6
6
|
@client.auth(get_password)
|
7
|
+
|
8
|
+
puts "Enter a Story Id for your Account:"
|
9
|
+
@story = STDIN.gets.chomp
|
7
10
|
end
|
8
11
|
|
9
|
-
it "should get
|
10
|
-
@client.
|
12
|
+
it "should get all stories for a specific user" do
|
13
|
+
@client.userstories(@username).length.should == 2
|
11
14
|
end
|
12
15
|
|
13
|
-
it "should get a specific "
|
16
|
+
it "should get a specific story for a user (all pages)" do
|
17
|
+
@client.story(@story).elements.length.should == 3
|
18
|
+
end
|
14
19
|
|
20
|
+
it "should allow a story to be serialized as text" do
|
21
|
+
story = @client.story('austin-startup-digest-for-december-9-2014', 'joshuabaer')
|
22
|
+
story.should_not eql ""
|
23
|
+
puts story.to_s
|
24
|
+
end
|
15
25
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: storify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rizwan Tejpar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-12-
|
11
|
+
date: 2013-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.7.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.7.7
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rest-client
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -25,19 +39,19 @@ dependencies:
|
|
25
39
|
- !ruby/object:Gem::Version
|
26
40
|
version: 1.6.7
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
42
|
+
name: nokogiri
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
30
44
|
requirements:
|
31
45
|
- - '>='
|
32
46
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1.
|
47
|
+
version: 1.6.0
|
34
48
|
type: :runtime
|
35
49
|
prerelease: false
|
36
50
|
version_requirements: !ruby/object:Gem::Requirement
|
37
51
|
requirements:
|
38
52
|
- - '>='
|
39
53
|
- !ruby/object:Gem::Version
|
40
|
-
version: 1.
|
54
|
+
version: 1.6.0
|
41
55
|
- !ruby/object:Gem::Dependency
|
42
56
|
name: rspec
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,6 +66,20 @@ dependencies:
|
|
52
66
|
- - '>='
|
53
67
|
- !ruby/object:Gem::Version
|
54
68
|
version: 2.14.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: io-console
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.4.2
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.4.2
|
55
83
|
description: Ruby Wrapper of Storify REST API -- work-in-progress
|
56
84
|
email: rtejpar@gmail.com
|
57
85
|
executables: []
|
@@ -60,6 +88,8 @@ extra_rdoc_files: []
|
|
60
88
|
files:
|
61
89
|
- lib/storify/apierror.rb
|
62
90
|
- lib/storify/client.rb
|
91
|
+
- lib/storify/element.rb
|
92
|
+
- lib/storify/story.rb
|
63
93
|
- lib/storify.rb
|
64
94
|
- spec/client_auth_spec.rb
|
65
95
|
- spec/client_spec.rb
|