class Putson::Client

Constants

URL

Attributes

id[RW]

Public Class Methods

new(id: nil) click to toggle source
# File lib/putson/client.rb, line 7
def initialize(id: nil)
  @id = id
end

Public Instance Methods

get(newid = nil) click to toggle source
# File lib/putson/client.rb, line 31
def get(newid = nil)
  @id = newid unless newid.nil?
  response = client.get do |req|
    req.url "/bins/#{id}"
  end
  response.body
end
post(data) click to toggle source
# File lib/putson/client.rb, line 11
def post(data)
  data = data.to_json unless data.is_a? String
  response = client.post do |req|
    req.url '/bins'
    req.headers['Content-Type'] = 'application/json'
    req.body = data
  end
  parse_post_response(response)
end
put(data) click to toggle source
# File lib/putson/client.rb, line 21
def put(data)
  data = data.to_json unless data.is_a? String
  response = client.put do |req|
    req.url "/bins/#{id}"
    req.headers['Content-Type'] = 'application/json'
    req.body = data
  end
  parse_post_response(response)
end
url() click to toggle source
# File lib/putson/client.rb, line 39
def url
  return if id.nil?
  "#{URL}bins/#{id}"
end

Private Instance Methods

client() click to toggle source
# File lib/putson/client.rb, line 52
def client
  @client ||= Faraday.new(URL)
end
parse_post_response(response) click to toggle source
# File lib/putson/client.rb, line 46
def parse_post_response(response)
  body = JSON.parse(response.body)
  @id ||= body.fetch('uri', nil).gsub("#{URL}bins/", '')
  url
end