Class: Storify::Client

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

Constant Summary collapse

EOC =

define end of content example

{'content' => {'stories' => [], 'elements' => [], 'users' => []}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Client

Returns a new instance of Client.

Yields:

  • (_self)

Yield Parameters:



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

def initialize(options = {})
  options.each do |k, v|
    send(:"#{k}=", v)
  end

  yield self if block_given?
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#tokenObject

Returns the value of attribute token.



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

def token
  @token
end

#usernameObject

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: options[:version], method: :auth)
  data = call(endpoint, :POST, params: {password: password})
  @token = data['content']['_token']

  self
end

#authenticatedObject



161
162
163
# File 'lib/storify/client.rb', line 161

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: options[:version],
                               protocol: options[:protocol],
                               method: :editslug,
                               params: params)

  data = call(endpoint, :POST, params: {slug: new_slug})
  data['content']['slug']
end


36
37
38
# File 'lib/storify/client.rb', line 36

def featured(pager: nil, options: {})
  story_list(:featured, pager, options: 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: options, use_auth: false)
end


40
41
42
# File 'lib/storify/client.rb', line 40

def popular(pager: nil, options: {})
  story_list(:popular, pager, options: 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: options[:version],
                               protocol: options[: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



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/storify/client.rb', line 138

def publish(story, options: {})
  puts story.inspect
  # 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.author.nil? || story.author.username.nil?)

  # extract author and slug
  slug = story.slug
  username = story.author.username
  endpoint = Storify::endpoint(version: options[:version],
                               protocol: options[:protocol],
                               method: :publish,
                               params: {':username' => username,
                                        ':slug' => slug})

  # attempt to publish
  json = story.to_json
  data = call(endpoint, :POST, params: {:data => 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: 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: 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: options[:version],
                               protocol: options[: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: options, params: {':topic' => topic})
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: options[:version],
                               protocol: options[: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: options, params: params)
end