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

Returns the value of attribute api_token.



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

def api_token
  @api_token
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



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

def delete(uri)
  request :delete, uri
end

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



8
9
10
# File 'lib/printfection/api.rb', line 8

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

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



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

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

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



12
13
14
# File 'lib/printfection/api.rb', line 12

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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/printfection/api.rb', line 24

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