class Heroix::Resource

Attributes

token[R]
type[R]
url[R]

Public Class Methods

new(params) click to toggle source
# File lib/heroix/resource.rb, line 10
def initialize(params)
  @url = params[:url]
  @token = params[:token]
  @type = params[:type]
end

Public Instance Methods

create(data) click to toggle source
# File lib/heroix/resource.rb, line 29
def create(data)
  request(:post, url, :data => data)
end
delete(id) click to toggle source
# File lib/heroix/resource.rb, line 37
def delete(id)
  request(:delete, url_for(id))
end
index(params = {}) click to toggle source
# File lib/heroix/resource.rb, line 21
def index(params = {})
  request(:get, url, :query => params)
end
show(id) click to toggle source
# File lib/heroix/resource.rb, line 25
def show(id)
  request(:get, url_for(id))
end
update(id, data) click to toggle source
# File lib/heroix/resource.rb, line 33
def update(id, data)
  request(:put, url_for(id), :data => data)
end
with_token(value) click to toggle source
# File lib/heroix/resource.rb, line 16
def with_token(value)
  @token = value
  self
end

Protected Instance Methods

url_for(path) click to toggle source
# File lib/heroix/resource.rb, line 43
def url_for(path)
  "#{url}/#{path}"
end

Private Instance Methods

handle(response) click to toggle source
# File lib/heroix/resource.rb, line 70
def handle(response)
  deprecation = response.headers[:x_deprecation]
  logger.warn "[heroix] DEPRECATION WARNING #{deprecation}" if deprecation

  parse_response response.to_str
end
logger() click to toggle source
# File lib/heroix/resource.rb, line 87
def logger
  Heroix.logger
end
parse_response(response) click to toggle source
# File lib/heroix/resource.rb, line 77
def parse_response(response)
  return {} if response.empty?
  JSON.parse(response)
end
perform_request(method, url, options) click to toggle source
# File lib/heroix/resource.rb, line 58
def perform_request(method, url, options)
  handle RestClient::Request.execute(
    :method => method,
    :url => url,
    :payload => prepare_data(options[:data]),
    :headers => {
      :authorization => "Token token=\"#{token}\"",
      :content_type => :json, :params => options[:query]
    }
  )
end
prepare_data(data) click to toggle source
# File lib/heroix/resource.rb, line 82
def prepare_data(data)
  data = { type => data } if data && type
  data.to_json
end
request(method, url, options = {}) click to toggle source
# File lib/heroix/resource.rb, line 49
def request(method, url, options = {})
  rest_logger = RestClient.log
  RestClient.log = logger

  perform_request(method, url, options)
ensure
  RestClient.log = rest_logger
end