Class: Gumrider

Inherits:
Object
  • Object
show all
Defined in:
lib/gumrider.rb

Defined Under Namespace

Classes: Link

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email, password) ⇒ Gumrider

Returns a new instance of Gumrider.



9
10
11
12
13
# File 'lib/gumrider.rb', line 9

def initialize(email, password)
  @email = email
  @password = password
  @endpoint = 'https://gumroad.com/api/v1'
end

Instance Attribute Details

#endpointObject

Returns the value of attribute endpoint.



7
8
9
# File 'lib/gumrider.rb', line 7

def endpoint
  @endpoint
end

#tokenObject

Returns the value of attribute token.



7
8
9
# File 'lib/gumrider.rb', line 7

def token
  @token
end

Instance Method Details

#authenticateObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/gumrider.rb', line 15

def authenticate
  response = Crack::JSON.parse Http.post @endpoint + '/sessions', :form => { :email => @email, :password => @password }
  
  if response["success"]
    @token = Base64.encode64(response["token"] + ":")
    true
  else
    false
  end
end


26
27
28
# File 'lib/gumrider.rb', line 26

def link(id = false)
  Gumrider::Link.new id, @token, @endpoint
end


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gumrider.rb', line 30

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

  if response["success"]
    links = []

    response["links"].each do |item|
      link = Gumrider::Link.new(false, @token, @endpoint)
      link.name = item["name"]
      link.url = item["url"]
      link.price = item["price"] / 100
      link.description = item["description"]
      link.id = item["id"]
      link.currency = item["currency"]

      links.push link
    end

    links
  else
    []
  end
end