Class: Storify::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/storify/client.rb

Overview

RestClient.log = ‘./restclient.log’

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, username) ⇒ Client

Returns a new instance of Client.



8
9
10
11
# File 'lib/storify/client.rb', line 8

def initialize(api_key, username)
  @api_key = api_key
  @username = username
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/storify/client.rb', line 6

def api_key
  @api_key
end

#tokenObject (readonly)

Returns the value of attribute token.



6
7
8
# File 'lib/storify/client.rb', line 6

def token
  @token
end

#usernameObject (readonly)

Returns the value of attribute username.



6
7
8
# File 'lib/storify/client.rb', line 6

def username
  @username
end

Instance Method Details

#add_pagination(page = 1, per_page = 20) ⇒ Object



65
66
67
68
69
70
# File 'lib/storify/client.rb', line 65

def add_pagination(page = 1, per_page = 20)
  params = {}
  params[:page] = page
  params[:per_page] = per_page
  params
end

#auth(password) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/storify/client.rb', line 13

def auth(password)
  endpoint = Storify::auth
  data = call(endpoint, :POST, {password: password})
  @token = data['content']['_token']

  self
end

#authenticatedObject



61
62
63
# File 'lib/storify/client.rb', line 61

def authenticated
  !@token.nil?
end

#story(slug, username = @username) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/storify/client.rb', line 40

def story(slug, username = @username)
  endpoint = Storify::story(username, slug)
  pager = add_pagination
  story = nil
  elements = []

  while data = call(endpoint, :GET, pager)
    story = Storify::Story.new(data['content']) if story.nil?
    break if data['content']['elements'].length == 0

    # create elements
    data['content']['elements'].each do |e|
      story.add_element(Storify::Element.new(e))
    end

    pager[:page] += 1
  end

  story
end

#userstories(username = @username) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/storify/client.rb', line 21

def userstories(username = @username)
  endpoint = Storify::userstories(username)
  pager = add_pagination
  stories = []

  while data = call(endpoint, :GET, pager)
    content = data['content']
    break if content['stories'].length == 0

    content['stories'].each do |s|
      stories << Storify::Story.new(s)
    end

    pager[:page] += 1
  end

  stories
end