Module: Printfection::API

Included in:
Printfection
Defined in:
lib/printfection/api.rb

Constant Summary collapse

ENDPOINT =
"api.printfection.com/v2/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_tokenObject



8
9
10
11
12
13
14
# File 'lib/printfection/api.rb', line 8

def api_token
  if @api_token.nil? || @api_token.strip.to_s.empty?
    raise Error, "Missing API Key."
  else
    return @api_token
  end
end

#loggerObject

Returns the value of attribute logger.



6
7
8
# File 'lib/printfection/api.rb', line 6

def logger
  @logger
end

Instance Method Details

#delete(uri) ⇒ Object



28
29
30
# File 'lib/printfection/api.rb', line 28

def delete(uri)
  request :delete, uri
end

#get(uri = "/", params = {}) ⇒ Object



16
17
18
# File 'lib/printfection/api.rb', line 16

def get(uri="/", params={})
  request :get, uri, params
end

#patch(uri, data = {}) ⇒ Object



24
25
26
# File 'lib/printfection/api.rb', line 24

def patch(uri, data={})
  request :patch, uri, data
end

#post(uri, data = {}) ⇒ Object



20
21
22
# File 'lib/printfection/api.rb', line 20

def post(uri, data={})
  request :post, uri, data
end

#request(verb, uri, params = {}) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/printfection/api.rb', line 32

def request(verb, uri, params={})
  perform_request do
    uri = Util.join_uri(ENDPOINT, uri)
    url = "https://#{api_token}:@#{uri}"

    unless logger.nil?
      logger.info "[Printfection] #{verb.upcase} #{url}"
    end

    response = case verb
    when :get;    RestClient.get url, :params => params, :accept => :json
    when :post;   RestClient.post url, params.to_json, :accept => :json, :content_type => :json
    when :patch;  RestClient.patch url, params.to_json, :accept => :json, :content_type => :json
    when :delete; RestClient.delete url
    end

    json = JSON.parse(response.body)

    if json["object"] == "list" and json.has_key? "data"
      json["data"]
    else
      json
    end
  end
end