class Reverb::Api::Client

Constants

HEADERS

Attributes

api_version[R]
basic_auth[R]
oauth_token[R]
reverb_token[R]
reverb_url[R]

Public Class Methods

new(reverb_token: nil, oauth_token: nil, url: "https://reverb.com", basic_auth: nil, headers:{}, api_version: nil) click to toggle source

url: Base URL of the Reverb API (e.g.: reverb.com) basic_auth: optional basic auth for hitting Reverb Sandbox URLs

# File lib/reverb/api/client.rb, line 15
def initialize(reverb_token: nil,
               oauth_token: nil,
               url: "https://reverb.com",
               basic_auth: nil,
               headers:{},
               api_version: nil)
  @reverb_token = reverb_token
  @oauth_token = oauth_token
  @reverb_url = url
  @basic_auth = basic_auth
  @api_version = api_version
  default_headers.merge!(headers)
end

Public Instance Methods

add_default_header(header) click to toggle source
# File lib/reverb/api/client.rb, line 33
def add_default_header(header)
  default_headers.merge!(header)
end
authenticate(email, password) click to toggle source
# File lib/reverb/api/client.rb, line 37
def authenticate(email, password)
  post("/api/auth/email", email: email, password: password)
end
create_listing(param) click to toggle source
# File lib/reverb/api/client.rb, line 41
def create_listing(param)
  post("/api/listings", param)
end
create_webhook(url:, topic:) click to toggle source
# File lib/reverb/api/client.rb, line 53
def create_webhook(url:, topic:)
  post("/api/webhooks/registrations", url: url, topic: topic)
end
default_headers() click to toggle source
# File lib/reverb/api/client.rb, line 29
def default_headers
  @_headers ||= HEADERS.dup
end
delete(path, params) click to toggle source
# File lib/reverb/api/client.rb, line 73
def delete(path, params)
  handle_response HTTParty.delete(url(path), with_defaults(params))
end
find_draft(sku) click to toggle source
# File lib/reverb/api/client.rb, line 49
def find_draft(sku)
  find_by_sku(sku, "draft")
end
find_listing_by_sku(sku) click to toggle source
# File lib/reverb/api/client.rb, line 45
def find_listing_by_sku(sku)
  find_by_sku(sku, "all")
end
get(path, params={}) click to toggle source
# File lib/reverb/api/client.rb, line 65
def get(path, params={})
  handle_response HTTParty.get(url(path), with_defaults(params))
end
post(path, params) click to toggle source
# File lib/reverb/api/client.rb, line 61
def post(path, params)
  handle_response HTTParty.post(url(path), with_defaults(params))
end
put(path, params) click to toggle source
# File lib/reverb/api/client.rb, line 69
def put(path, params)
  handle_response HTTParty.put(url(path), with_defaults(params))
end
webhooks() click to toggle source
# File lib/reverb/api/client.rb, line 57
def webhooks
  get("/api/webhooks/registrations")
end

Private Instance Methods

auth_headers() click to toggle source
# File lib/reverb/api/client.rb, line 138
def auth_headers
  if reverb_token
    {"X-Auth-Token" => reverb_token}
  elsif oauth_token
    {"Authorization" => "Bearer #{oauth_token}"}
  else
    {}
  end
end
authorized?(response) click to toggle source
# File lib/reverb/api/client.rb, line 113
def authorized?(response)
  !(response.code == 401 || response.code == 403)
end
default_options() click to toggle source
# File lib/reverb/api/client.rb, line 126
def default_options
  headers = default_headers.dup

  headers.merge!("Accept-Version" => api_version) if api_version
  headers.merge!(auth_headers)
  {
    basic_auth: basic_auth,
    headers: headers,
    verify: false
  }
end
find_by_sku(sku, state) click to toggle source
# File lib/reverb/api/client.rb, line 81
def find_by_sku(sku, state)
  listings = get("/api/my/listings?sku=#{CGI.escape(sku)}&state=#{state}")["listings"]
  listing_attributes =  listings && listings.first
  if listing_attributes
    Listing.new(client: self, listing_attributes: listing_attributes)
  else
    nil
  end
end
handle_response(response) click to toggle source
# File lib/reverb/api/client.rb, line 107
def handle_response(response)
  raise ServiceDown if response.code == 503
  raise Reverb::Api::NotAuthorizedError.new(response.parsed_response["message"]) unless authorized?(response)
  response
end
url(path) click to toggle source
# File lib/reverb/api/client.rb, line 99
def url(path)
  if URI.parse(path).hostname
    path
  else
    File.join(reverb_url, path)
  end
end
with_defaults(params={}) click to toggle source
# File lib/reverb/api/client.rb, line 91
def with_defaults(params={})
  unless params.empty?
    default_options.merge(body: params.to_json)
  else
    default_options
  end
end