Class: API

Inherits:
Object
  • Object
show all
Defined in:
lib/quintype/api.rb,
lib/quintype/api/tag.rb,
lib/quintype/api/url.rb,
lib/quintype/api/stack.rb,
lib/quintype/api/story.rb,
lib/quintype/api/author.rb,
lib/quintype/api/entity.rb,
lib/quintype/api/story/reading_time.rb

Defined Under Namespace

Classes: Author, Entity, Stack, Story, Tag, URL

Class Method Summary collapse

Class Method Details

.author_profile(author_id) ⇒ Object



151
152
153
# File 'lib/quintype/api.rb', line 151

def author_profile(author_id)
  _get("author/#{author_id}")
end

.authors(params) ⇒ Object



147
148
149
# File 'lib/quintype/api.rb', line 147

def authors(params)
  _get("authors", params)
end

.bulk_cached(params) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/quintype/api.rb', line 27

def bulk_cached(params)
  response_body = nil # Used in case of manticore auto following redirect. Ugly side effect

  location = @@bulk_cache.fetch(params) do |params|
    response = @@conn.post(@@api_base + "bulk-request", params) do |request|
      request.headers['Content-Type'] = 'application/json'
      request.body = params.to_json
    end

    if response.status == 303 && response.headers["Location"]
      response.headers["Location"]
    elsif response.status == 200 && response.headers["Content-Location"]
      response_body = keywordize(JSON.parse(response.body))
      log_info("The faraday adapter is configured to follow redirects by default. Using the Content-Location header")
      response.headers["Content-Location"]
    else
      raise "Did not recieve a location header, status #{response.status}"
    end
  end

  response_body || _get(location.sub(/^\/api\//, ""))
end

.bulk_post(params) ⇒ Object



23
24
25
# File 'lib/quintype/api.rb', line 23

def bulk_post(params)
  _post("bulk", params)
end

.check_email(email) ⇒ Object



179
180
181
# File 'lib/quintype/api.rb', line 179

def check_email(email)
  _get("member/check", { email: email })
end

.collection(slug, options) ⇒ Object

This is a hack because we can’t migrate entire APIs to use v1 [Varun - 14th December 2016]



110
111
112
# File 'lib/quintype/api.rb', line 110

def collection(slug, options)
  _get("v1/collections/" + slug, options)
end

.comments_and_likes(story_ids) ⇒ Object



87
88
89
90
91
# File 'lib/quintype/api.rb', line 87

def comments_and_likes(story_ids)
  if story_ids.present?
    _get("comments-and-votes/" + story_ids.join('|'))
  end
end

.configObject



50
51
52
# File 'lib/quintype/api.rb', line 50

def config
  _get("config")
end

.connObject



19
20
21
# File 'lib/quintype/api.rb', line 19

def conn
  @@conn
end

.contact_publisher(params) ⇒ Object



139
140
141
# File 'lib/quintype/api.rb', line 139

def contact_publisher(params)
  _post("emails/contact", params)
end

.entities(params) ⇒ Object



221
222
223
# File 'lib/quintype/api.rb', line 221

def entities(params)
  _get("v1/entities", params)
end

.establish_connection(host, conn = Faraday.new(url: host)) ⇒ Object



12
13
14
15
16
17
# File 'lib/quintype/api.rb', line 12

def establish_connection(host, conn = Faraday.new(url: host))
  @@host = host
  @@api_base = host + '/api/'
  @@bulk_cache ||= ActiveSupport::Cache::MemoryStore.new
  @@conn = conn
end

.find_entity(id) ⇒ Object



225
226
227
# File 'lib/quintype/api.rb', line 225

def find_entity(id)
  _get("v1/entities/#{id}")
end

.forgot_password(member) ⇒ Object



201
202
203
# File 'lib/quintype/api.rb', line 201

def forgot_password(member)
  _post("member/forgot-password", member)
end

.get_member_metadata(session_cookie) ⇒ Object



175
176
177
# File 'lib/quintype/api.rb', line 175

def (session_cookie)
  _get("member/metadata", {}, { auth_token: session_cookie})
end

.invite_users(emails, from_email, from_name) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/quintype/api.rb', line 131

def invite_users(emails, from_email, from_name)
  params = { emails: emails }
  params['from-email'] = from_email if from_email.present?
  params['from-name'] = from_name if from_name.present?

  _post("emails/invite", params)
end

.login(provider, data) ⇒ Object



191
192
193
194
195
# File 'lib/quintype/api.rb', line 191

def (provider, data)
  user, headers = _post_returning_headers("login/#{provider}", data)
  user['auth_token'] = headers['X-QT-AUTH']
  user['member'].merge(user.except('member'))
end

.login_member(auth) ⇒ Object



187
188
189
# File 'lib/quintype/api.rb', line 187

def (auth)
  _post("member/login", auth)
end

.logoutObject



197
198
199
# File 'lib/quintype/api.rb', line 197

def logout
  _get("logout")
end

.post_comment(story_content_id, text, parent_comment_id = nil, session_cookie) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/quintype/api.rb', line 122

def post_comment(story_content_id, text, parent_comment_id=nil, session_cookie)
  hash = {
    "story-content-id"  => story_content_id,
    "text"              => text
  }
  hash.merge!("parent-comment-id" => parent_comment_id.to_i) if parent_comment_id
  _post("comment", hash, session_cookie)
end

.preview_story(public_preview_key) ⇒ Object



217
218
219
# File 'lib/quintype/api.rb', line 217

def preview_story(public_preview_key)
  _get("v1/preview/story/#{public_preview_key}")
end


74
75
76
77
78
79
80
# File 'lib/quintype/api.rb', line 74

def related_stories(story_id, section, fields = [])
  _get("related-stories?", {
         "story-id" => story_id,
         section: section,
         fields: make_fields(fields)
       })
end

.reset_password(params) ⇒ Object



205
206
207
# File 'lib/quintype/api.rb', line 205

def reset_password(params)
  _post("member/password", params)
end

.save_member_metadata(metadata, session_cookie) ⇒ Object



171
172
173
# File 'lib/quintype/api.rb', line 171

def (, session_cookie)
  _post("member/metadata", { metadata:  }, session_cookie)
end

.search(options) ⇒ Object



155
156
157
# File 'lib/quintype/api.rb', line 155

def search(options)
  _get("search", options)
end

.search_story_collection(name, options) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/quintype/api.rb', line 101

def search_story_collection(name, options)
  _get("story-collection", {
         name: name,
         type: "search",
         fields: "author-name,hero-image-s3-key,hero-image-metadata,hero-image-caption,headline,slug,sections,metadata"
       }.merge(options))
end

.sectionsObject



54
55
56
# File 'lib/quintype/api.rb', line 54

def sections
  _get("sections")
end

.signup_member(member) ⇒ Object



183
184
185
# File 'lib/quintype/api.rb', line 183

def (member)
  _post("member", member)
end

.slugify(x) ⇒ Object



62
63
64
# File 'lib/quintype/api.rb', line 62

def slugify(x)
  _get("slugify/#{x}")
end

.stories(params, options = {}) ⇒ Object



82
83
84
85
# File 'lib/quintype/api.rb', line 82

def stories(params, options = {})
  url = options[:facets] ? "stories-with-facets" : "stories"
  _get(url, params)
end

.story(story_id) ⇒ Object



58
59
60
# File 'lib/quintype/api.rb', line 58

def story(story_id)
  _get("stories/#{story_id}")
end

.story_by_slug(slug, params = {}) ⇒ Object



70
71
72
# File 'lib/quintype/api.rb', line 70

def story_by_slug(slug, params = {})
  _get("stories-by-slug", params.merge({ slug: slug }))
end

.story_collection(options) ⇒ Object



114
115
116
# File 'lib/quintype/api.rb', line 114

def story_collection(options)
  _get("story-collection", options)
end

.story_collection_by_tag(options) ⇒ Object



118
119
120
# File 'lib/quintype/api.rb', line 118

def story_collection_by_tag(options)
  _get("story-collection/find-by-tag", options)
end

.sub_entity(entity_id, sub_entity_id) ⇒ Object



229
230
231
# File 'lib/quintype/api.rb', line 229

def sub_entity(entity_id, sub_entity_id)
  _get("v1/entities/#{entity_id}/#{sub_entity_id}")
end

.subscribe(member, profile, payment) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/quintype/api.rb', line 159

def subscribe(member, profile, payment)
  _post("subscribe", {
          member: member,
          profile: profile,
          payment: payment
        })
end

.tag_by_name(tag_name) ⇒ Object



66
67
68
# File 'lib/quintype/api.rb', line 66

def tag_by_name(tag_name)
  _get("tag/#{tag_name}")
end

.unsubscribe(options) ⇒ Object



167
168
169
# File 'lib/quintype/api.rb', line 167

def unsubscribe(options)
  _post("unsubscribe", { options: options })
end

.unsubscribe_publisher(params) ⇒ Object



143
144
145
# File 'lib/quintype/api.rb', line 143

def unsubscribe_publisher(params)
  _post("emails/unsubscribe", params)
end

.videosObject



93
94
95
96
97
98
99
# File 'lib/quintype/api.rb', line 93

def videos
  _get("stories-by-template", {
         template: "video",
         limit: 12,
         fields: "hero-image-s3-key,hero-image-metadata,hero-image-caption,headline,slug"
       })
end

.vote_on_story(data) ⇒ Object



209
210
211
# File 'lib/quintype/api.rb', line 209

def vote_on_story (data)
  _post("stories/#{data[:story_id]}/votes", data)
end

.votes_on_story(options = {}) ⇒ Object



213
214
215
# File 'lib/quintype/api.rb', line 213

def votes_on_story (options = {})
  _get("stories/#{story_id}/votes", options)
end