class API

Public Class Methods

author_profile(author_id) click to toggle source
# File lib/quintype/api.rb, line 175
def author_profile(author_id)
  _get("author/#{author_id}")
end
authors(params) click to toggle source
# File lib/quintype/api.rb, line 171
def authors(params)
  _get("authors", params)
end
bulk_cached(params) click to toggle source
# 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) click to toggle source
# File lib/quintype/api.rb, line 23
def bulk_post(params)
  _post("bulk", params)
end
bulk_v1_cached(params) click to toggle source
# File lib/quintype/api.rb, line 50
def bulk_v1_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/v1/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
check_email(email) click to toggle source
# File lib/quintype/api.rb, line 203
def check_email(email)
  _get("member/check", { email: email })
end
collection(slug, options) click to toggle source

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

# File lib/quintype/api.rb, line 134
def collection(slug, options)
  _get("v1/collections/" + slug, options)
end
comments_and_likes(story_ids) click to toggle source
# File lib/quintype/api.rb, line 111
def comments_and_likes(story_ids)
  if story_ids.present?
    _get("comments-and-votes/" + story_ids.join('|'))
  end
end
config() click to toggle source
# File lib/quintype/api.rb, line 74
def config
  _get("config")
end
conn() click to toggle source
# File lib/quintype/api.rb, line 19
def conn
  @@conn
end
contact_publisher(params) click to toggle source
# File lib/quintype/api.rb, line 163
def contact_publisher(params)
  _post("emails/contact", params)
end
entities(params) click to toggle source
# File lib/quintype/api.rb, line 245
def entities(params)
  _get("v1/entities", params)
end
establish_connection(host, conn = Faraday.new(url: host)) click to toggle source
# 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) click to toggle source
# File lib/quintype/api.rb, line 249
def find_entity(id)
  _get("v1/entities/#{id}")
end
forgot_password(member) click to toggle source
# File lib/quintype/api.rb, line 225
def forgot_password(member)
  _post("member/forgot-password", member)
end
get_member_metadata(session_cookie) click to toggle source
# File lib/quintype/api.rb, line 199
def get_member_metadata(session_cookie)
  _get("member/metadata", {}, { auth_token: session_cookie})
end
invite_users(emails, from_email, from_name) click to toggle source
# File lib/quintype/api.rb, line 155
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) click to toggle source
# File lib/quintype/api.rb, line 215
def login(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) click to toggle source
# File lib/quintype/api.rb, line 211
def login_member(auth)
  _post("member/login", auth)
end
logout() click to toggle source
# File lib/quintype/api.rb, line 221
def logout
  _get("logout")
end
post_comment(story_content_id, text, parent_comment_id=nil, session_cookie) click to toggle source
# File lib/quintype/api.rb, line 146
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) click to toggle source
# File lib/quintype/api.rb, line 241
def preview_story(public_preview_key)
  _get("v1/preview/story/#{public_preview_key}")
end
reset_password(params) click to toggle source
# File lib/quintype/api.rb, line 229
def reset_password(params)
  _post("member/password", params)
end
save_member_metadata(metadata, session_cookie) click to toggle source
# File lib/quintype/api.rb, line 195
def save_member_metadata(metadata, session_cookie)
  _post("member/metadata", { metadata: metadata }, session_cookie)
end
search_story_collection(name, options) click to toggle source
# File lib/quintype/api.rb, line 125
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
sections() click to toggle source
# File lib/quintype/api.rb, line 78
def sections
  _get("sections")
end
signup_member(member) click to toggle source
# File lib/quintype/api.rb, line 207
def signup_member(member)
  _post("member", member)
end
slugify(x) click to toggle source
# File lib/quintype/api.rb, line 86
def slugify(x)
  _get("slugify/#{x}")
end
stories(params, options = {}) click to toggle source
# File lib/quintype/api.rb, line 106
def stories(params, options = {})
  url = options[:facets] ? "stories-with-facets" : "stories"
  _get(url, params)
end
story(story_id) click to toggle source
# File lib/quintype/api.rb, line 82
def story(story_id)
  _get("stories/#{story_id}")
end
story_by_slug(slug, params = {}) click to toggle source
# File lib/quintype/api.rb, line 94
def story_by_slug(slug, params = {})
  _get("stories-by-slug", params.merge({ slug: slug }))
end
story_collection(options) click to toggle source
# File lib/quintype/api.rb, line 138
def story_collection(options)
  _get("story-collection", options)
end
story_collection_by_tag(options) click to toggle source
# File lib/quintype/api.rb, line 142
def story_collection_by_tag(options)
  _get("story-collection/find-by-tag", options)
end
sub_entity(entity_id, sub_entity_id) click to toggle source
# File lib/quintype/api.rb, line 253
def sub_entity(entity_id, sub_entity_id)
  _get("v1/entities/#{entity_id}/#{sub_entity_id}")
end
subscribe(member, profile, payment) click to toggle source
# File lib/quintype/api.rb, line 183
def subscribe(member, profile, payment)
  _post("subscribe", {
          member: member,
          profile: profile,
          payment: payment
        })
end
tag_by_name(tag_name) click to toggle source
# File lib/quintype/api.rb, line 90
def tag_by_name(tag_name)
  _get("tag/#{tag_name}")
end
unsubscribe(options) click to toggle source
# File lib/quintype/api.rb, line 191
def unsubscribe(options)
  _post("unsubscribe", { options: options })
end
unsubscribe_publisher(params) click to toggle source
# File lib/quintype/api.rb, line 167
def unsubscribe_publisher(params)
  _post("emails/unsubscribe", params)
end
videos() click to toggle source
# File lib/quintype/api.rb, line 117
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) click to toggle source
# File lib/quintype/api.rb, line 233
def vote_on_story (data)
  _post("stories/#{data[:story_id]}/votes", data)
end
votes_on_story(options = {}) click to toggle source
# File lib/quintype/api.rb, line 237
def votes_on_story (options = {})
  _get("stories/#{story_id}/votes", options)
end

Private Class Methods

_get(url_path, params={}, args={}) click to toggle source
# File lib/quintype/api.rb, line 285
def _get(url_path, params={}, args={})
  response = @@conn.get(@@api_base + url_path, params) do |request|
    request.headers['Content-Type'] = 'application/json'
    request.headers['X-QT-AUTH'] = args[:auth_token] if args[:auth_token]
  end

  return nil if response.status >= 400

  if response.body.present?
    body = JSON.parse(response.body)

    case body
    when Array
      body.map { |i| keywordize(i) }
    when Object
      keywordize body
    end
  end
end
_post(url_path, body, session_cookie=nil) click to toggle source
# File lib/quintype/api.rb, line 259
def _post(url_path, body, session_cookie=nil)
  body, headers = _post_returning_headers(url_path, body, session_cookie)

  body
end
_post_returning_headers(url_path, body, session_cookie=nil) click to toggle source
# File lib/quintype/api.rb, line 265
def _post_returning_headers(url_path, body, session_cookie=nil)
  response = @@conn.post(@@api_base + url_path) do |request|
    request.headers['Content-Type'] = 'application/json'
    request.headers['X-QT-AUTH'] = session_cookie if session_cookie

    request.body = body.to_json
  end

  if response.body.present?
    body = case body = JSON.parse(response.body)
           when Array
             body.map { |i| keywordize(i) }
           when Object
             keywordize body
           end

    [body, response.headers]
  end
end
keywordize(obj) click to toggle source
# File lib/quintype/api.rb, line 305
def keywordize(obj)
  obj.deep_transform_keys { |k| k.gsub('-', '_') }
end
log_info(*args) click to toggle source
# File lib/quintype/api.rb, line 313
def log_info(*args)
  return unless defined?(Rails)
  Rails.logger.info(*args)
end
make_fields(arr) click to toggle source
# File lib/quintype/api.rb, line 309
def make_fields(arr)
  arr.join(',') if arr.present?
end