Class: HashBlue::Client

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

Overview

This class is in charge of the HTTP channel with HashBlue API

Direct Known Subclasses

Account, Contact, Message

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.userObject

Returns the value of attribute user.



20
21
22
# File 'lib/hash_blue/client.rb', line 20

def user
  @user
end

Class Method Details

.clientObject



22
23
24
25
26
27
# File 'lib/hash_blue/client.rb', line 22

def client
  @rest ||= (uri = URI.parse("https://api.hashblue.com")
  rest = Net::HTTP.new(uri.host, uri.port)
  rest.use_ssl = true
  rest)
end

.delete(path) ⇒ Object

Send a HTTP DELETE request (should be used to delete available resources)



52
53
54
# File 'lib/hash_blue/client.rb', line 52

def delete(path)
  client.delete(path, {'Authorization' => "OAuth #{self.superclass.user}"})
end

.get(path) ⇒ Object

Send a HTTP GET request (should be used to retrieve available resources)



30
31
32
33
34
35
36
37
38
# File 'lib/hash_blue/client.rb', line 30

def get(path)
  # get oAuth token directly from the class or the subclass
  if user.nil?
    token = self.superclass.user
  else
    token = self.user
  end
  client.get(path, {'Authorization' => "OAuth #{token}"}).body
end

.post(path, body) ⇒ Object

Send a HTTP POST request (should be used to create new resources)



41
42
43
44
# File 'lib/hash_blue/client.rb', line 41

def post(path, body)
  client.post(path, body.to_json, {'Authorization' => "OAuth #{self.superclass.user}",
                                  'Content-Type' => "application/json"}).body
end

.put(path, body = nil) ⇒ Object

Send a HTTP PUT request (should be used to update available resources)



47
48
49
# File 'lib/hash_blue/client.rb', line 47

def put(path, body = nil)
  client.put(path, body, {'Authorization' => "OAuth #{self.superclass.user}"})
end