class Gumrider::Link

Attributes

currency[RW]
description[RW]
endpoint[RW]
id[RW]
name[RW]
price[RW]
short_url[RW]
token[RW]
url[RW]

Public Class Methods

new(id, token, endpoint) click to toggle source
# File lib/gumrider.rb, line 58
def initialize(id, token, endpoint)
  @token = token
  @endpoint = endpoint

  if id
    response = Crack::JSON.parse Http.with(:Authorization => 'Basic ' + token).get endpoint + '/links/' + id

    if response["success"]
      @name = response["link"]["name"]
      @url = response["link"]["url"]
      @price = response["link"]["price"] / 100
      @description = response["link"]["description"]
      @currency = response["link"]["currency"]
      @short_url = response["link"]["short_url"]

      @id = id
    end
  end
end

Public Instance Methods

delete() click to toggle source
# File lib/gumrider.rb, line 86
def delete
  response = Crack::JSON.parse Http.with(:Authorization => 'Basic ' + @token).delete @endpoint + '/links/' + @id

  !!response["success"]
end
save() click to toggle source
# File lib/gumrider.rb, line 78
def save
  if @id
    update
  else
    create
  end
end

Private Instance Methods

create() click to toggle source
# File lib/gumrider.rb, line 94
def create
  response = Crack::JSON.parse Http.with(:Authorization => 'Basic ' + @token).post @endpoint + '/links', :form => {
    :name => @name,
    :url => @url,
    :description => @description,
    :price => @price * 100,
    :currency => @currency
  }
  
  if response["success"]
    @id = response["link"]["id"]
    @short_url = response["link"]["short_url"]

    true
  else
    false
  end
end
update() click to toggle source
# File lib/gumrider.rb, line 113
def update
  response = Crack::JSON.parse Http.with(:Authorization => 'Basic ' + @token).put @endpoint + '/links/' + @id, :form => {
    :name => @name,
    :url => @url,
    :description => @description,
    :price => @price * 100,
    :currency => @currency
  }

  !!response["success"]
end