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

#debug_modeObject

Returns the value of attribute debug_mode.



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

def debug_mode
  @debug_mode
end

Instance Method Details

#delete(uri) ⇒ Object



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

def delete(uri)
  request :delete, uri
end

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



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

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

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



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

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

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



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

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

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



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

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

    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