class Sharemore::SM

Attributes

conn[R]
key[R]

Public Class Methods

new(key) click to toggle source
# File lib/sharemore.rb, line 9
def initialize(key)
  @key = key
  @conn = connection
end

Public Instance Methods

article(article_id) click to toggle source
# File lib/sharemore.rb, line 14
def article(article_id)
  request(:get, get_article_uri(article_id) ) 
end
articles() click to toggle source
# File lib/sharemore.rb, line 18
def articles
  request(:get, articles_uri, access_token: key)
end
create(article) click to toggle source
# File lib/sharemore.rb, line 22
def create(article)#REFACTOR THIS INTO THE REQUEST
  response = conn.post do |req|
    req.url articles_uri, access_token: key
    req.headers['Content-Type'] = 'application/json'
    req.body = article.to_json
  end
  parse_response(response)
end
request(verb, url, args = {}) click to toggle source
# File lib/sharemore.rb, line 31
def request(verb, url, args = {})
  response = conn.send(verb) do |req| 
    req.url url, args
    req.headers['Content-Type'] = 'application/json'
  end
  parse_response(response)
end

Private Instance Methods

articles_uri() click to toggle source
# File lib/sharemore.rb, line 41
def articles_uri
  "articles"
end
connection() click to toggle source
# File lib/sharemore.rb, line 49
def connection
  conn = ::Faraday.new(:url => root_url) do |faraday|
    faraday.request  :url_encoded
    faraday.response :logger
    faraday.adapter  Faraday.default_adapter
  end
  conn
end
get_article_uri(article_id) click to toggle source
# File lib/sharemore.rb, line 45
def get_article_uri(article_id)
  "articles/#{article_id}"
end
parse_response(response) click to toggle source
# File lib/sharemore.rb, line 58
def parse_response(response)
  if response.status == 200
    JSON.parse(response.body)
  elsif response.status == 404
    nil
  else
    raise response.body
  end
end
root_url() click to toggle source
# File lib/sharemore.rb, line 68
def root_url
  "http://share-more.herokuapp.com/api/v1/"
end