Class: Storify::Client
- Inherits:
-
Object
- Object
- Storify::Client
- Defined in:
- lib/storify/client.rb
Constant Summary collapse
- EOC =
define end of content example
{'content' => {'stories' => [], 'elements' => [], 'users' => []}}
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#token ⇒ Object
Returns the value of attribute token.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #auth(password, options: {}) ⇒ Object
- #authenticated ⇒ Object
- #edit_slug(username = @username, old_slug, new_slug, options: {}) ⇒ Object
- #featured(pager: nil, options: {}) ⇒ Object
-
#initialize(options = {}) {|_self| ... } ⇒ Client
constructor
A new instance of Client.
- #latest(pager: nil, options: {}) ⇒ Object
- #popular(pager: nil, options: {}) ⇒ Object
- #profile(username = @usernmae, options: {}) ⇒ Object
- #publish(story, options: {}) ⇒ Object
- #search(criteria, pager: nil, options: {}) ⇒ Object
- #stories(pager: nil, options: {}) ⇒ Object
- #story(slug, username = @username, pager: nil, options: {}) ⇒ Object
- #topic(topic, pager: nil, options: {}) ⇒ Object
- #update_profile(user, options: {}) ⇒ Object
- #users(pager: nil, options: {}) ⇒ Object
- #userstories(username = @username, pager: nil, options: {}) ⇒ Object
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 |
# File 'lib/storify/client.rb', line 12 def initialize( = {}) .each do |k, v| send(:"#{k}=", v) end yield self if block_given? end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
10 11 12 |
# File 'lib/storify/client.rb', line 10 def api_key @api_key end |
#token ⇒ Object
Returns the value of attribute token.
10 11 12 |
# File 'lib/storify/client.rb', line 10 def token @token end |
#username ⇒ Object
Returns the value of attribute username.
10 11 12 |
# File 'lib/storify/client.rb', line 10 def username @username end |
Instance Method Details
#auth(password, options: {}) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/storify/client.rb', line 20 def auth(password, options: {}) endpoint = Storify::endpoint(version: [:version], method: :auth) data = call(endpoint, :POST, params: {password: password}) @token = data['content']['_token'] self end |
#authenticated ⇒ Object
176 177 178 |
# File 'lib/storify/client.rb', line 176 def authenticated !@token.nil? end |
#edit_slug(username = @username, old_slug, new_slug, options: {}) ⇒ Object
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/storify/client.rb', line 92 def edit_slug(username = @username, old_slug, new_slug, options: {}) params = {':username' => username, ':slug' => old_slug} endpoint = Storify::endpoint(version: [:version], protocol: [:protocol], method: :editslug, params: params) data = call(endpoint, :POST, params: {slug: new_slug}) data['content']['slug'] end |
#featured(pager: nil, options: {}) ⇒ Object
36 37 38 |
# File 'lib/storify/client.rb', line 36 def featured(pager: nil, options: {}) story_list(:featured, pager, options: , use_auth: false) end |
#latest(pager: nil, options: {}) ⇒ Object
32 33 34 |
# File 'lib/storify/client.rb', line 32 def latest(pager: nil, options: {}) story_list(:latest, pager, options: , use_auth: false) end |
#popular(pager: nil, options: {}) ⇒ Object
40 41 42 |
# File 'lib/storify/client.rb', line 40 def popular(pager: nil, options: {}) story_list(:popular, pager, options: , use_auth: false) end |
#profile(username = @usernmae, options: {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/storify/client.rb', line 126 def profile(username = @usernmae, options: {}) endpoint = Storify::endpoint(version: [:version], protocol: [:protocol], method: :userprofile, params: {':username' => username}) data = call(endpoint, :GET) json = JSON.generate(data['content']) User.new.extend(UserRepresentable).from_json(json) end |
#publish(story, options: {}) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/storify/client.rb', line 153 def publish(story, options: {}) # ensure we have a story w/slug and author raise "Not a Story" unless story.is_a?(Storify::Story) raise "No slug found" if story.slug.nil? raise "No author found" if (story..nil? || story..username.nil?) # extract author and slug slug = story.slug username = story..username endpoint = Storify::endpoint(version: [:version], protocol: [:protocol], method: :publish, params: {':username' => username, ':slug' => slug}) # attempt to publish json = story.to_json data = call(endpoint, :POST, params: {:story => json}) true end |
#search(criteria, pager: nil, options: {}) ⇒ Object
48 49 50 51 |
# File 'lib/storify/client.rb', line 48 def search(criteria, pager: nil, options: {}) u = {'q' => criteria} story_list(:search, pager, options: , use_auth: false, uparams: u) end |
#stories(pager: nil, options: {}) ⇒ Object
28 29 30 |
# File 'lib/storify/client.rb', line 28 def stories(pager: nil, options: {}) story_list(:stories, pager, options: , use_auth: false) end |
#story(slug, username = @username, pager: nil, options: {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/storify/client.rb', line 58 def story(slug, username = @username, pager: nil, options: {}) params = {':username' => username, ':slug' => slug} endpoint = Storify::endpoint(version: [:version], protocol: [:protocol], method: :userstory, params: params) pager = pager ||= Pager.new story = nil first = true elements = [] begin data = call(endpoint, :GET, paging: pager.to_hash) json = JSON.generate(data['content']) if story.nil? story = Story.new.extend(StoryRepresentable).from_json(json) else first = false end # create elements data['content']['elements'].each do |e| je = JSON.generate(e) story.elements << Element.new.extend(ElementRepresentable).from_json(je) end unless first pager.next end while pager.has_pages?(data['content']['elements']) story end |
#topic(topic, pager: nil, options: {}) ⇒ Object
44 45 46 |
# File 'lib/storify/client.rb', line 44 def topic(topic, pager: nil, options: {}) story_list(:topic, pager, options: , params: {':topic' => topic}) end |
#update_profile(user, options: {}) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/storify/client.rb', line 138 def update_profile(user, options: {}) raise "Not a User" unless user.is_a?(Storify::User) username = user.username endpoint = Storify::endpoint(version: [:version], protocol: [:protocol], method: :update_profile, params: {':username' => username}) json = user.to_json data = call(endpoint, :POST, params: {:user => json}) true end |
#users(pager: nil, options: {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/storify/client.rb', line 103 def users(pager: nil, options: {}) endpoint = Storify::endpoint(version: [:version], protocol: [:protocol], method: :users) pager = pager ||= Pager.new users = [] begin data = call(endpoint, :GET, paging: pager.to_hash, use_auth: false) content = data['content'] content['users'].each do |s| json = JSON.generate(s) users << User.new.extend(UserRepresentable).from_json(json) end pager.next end while pager.has_pages?(content['users']) users end |
#userstories(username = @username, pager: nil, options: {}) ⇒ Object
53 54 55 56 |
# File 'lib/storify/client.rb', line 53 def userstories(username = @username, pager: nil, options: {}) params = {':username' => username} story_list(:userstories, pager, options: , params: params) end |