class Teespring::Client

Attributes

access_token[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/teespring/client.rb, line 8
def initialize(options = {})
  @app_id = options.delete(:app_id)
end

Public Instance Methods

authenticate(email, password) click to toggle source
# File lib/teespring/client.rb, line 20
def authenticate(email, password)
  @response = post '/v1/auth-tokens', email: email, password: password, app_id: @app_id

  @access_token = @response.body['token']
end
campaigns(search = nil, states = nil) click to toggle source
# File lib/teespring/client.rb, line 30
def campaigns(search = nil, states = nil)
  @response = get '/seller/v1/campaigns', { access_token: @access_token, search: search, states: states, page: 1, per_page: 100 }
end
connection() click to toggle source
# File lib/teespring/client.rb, line 12
def connection
  @connection ||= ::Faraday.new(url: endpoint)
end
get(url, params) click to toggle source
# File lib/teespring/client.rb, line 34
def get(url, params)
  request(:get, url, params)
end
me() click to toggle source
# File lib/teespring/client.rb, line 26
def me
  @response = get '/me', { access_token: @access_token }
end
post(url, params) click to toggle source
# File lib/teespring/client.rb, line 38
def post(url, params)
  request(:post, url, params)
end
reset() click to toggle source
# File lib/teespring/client.rb, line 16
def reset
  @connection = nil
end

Private Instance Methods

endpoint() click to toggle source
# File lib/teespring/client.rb, line 61
def endpoint
  "https://api.teespring.com"
end
request(method, url, params) click to toggle source
# File lib/teespring/client.rb, line 44
def request(method, url, params)
  response = connection.send(method, url, params)

  case response.status
  when 200..299
    Teespring::Response.new(response)
  when 301
    raise Teespring::Error.new('Not found', response)
  when 400
    message = JSON.parse(response.body)['error']['message'] rescue response.reason_phrase

    raise Teespring::Error.new(message, response)
  else
    raise Teespring::Error.new('Unknown error', response)
  end
end