Module: PrintReleaf::API

Extended by:
API
Included in:
PrintReleaf, API
Defined in:
lib/printreleaf/api.rb

Constant Summary collapse

ENDPOINT =
"api.printreleaf.com/v1/"
PROTOCOL =
"https"
USER_AGENT =
"PrintReleaf Ruby/#{PrintReleaf::VERSION}"
MAX_RETRY_COUNT =
2
RETRY_DELAY_BASE =

Base for exponential delay

1.5
NETWORK_EXCEPTIONS =
[
  SocketError,
  Errno::ECONNREFUSED,
  Errno::ECONNRESET,
  Errno::ETIMEDOUT,
  RestClient::RequestTimeout
]
API_EXCEPTIONS =
{
  400 => BadRequest,
  401 => Unauthorized,
  403 => Forbidden,
  404 => NotFound,
  429 => RateLimitExceeded,
  500 => ServerError
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_keyObject



34
35
36
37
38
39
40
# File 'lib/printreleaf/api.rb', line 34

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

#endpointObject



42
43
44
# File 'lib/printreleaf/api.rb', line 42

def endpoint
  @endpoint || ENDPOINT
end

#loggerObject

Returns the value of attribute logger.



32
33
34
# File 'lib/printreleaf/api.rb', line 32

def logger
  @logger
end

#protocolObject



46
47
48
# File 'lib/printreleaf/api.rb', line 46

def protocol
  @protocol || PROTOCOL
end

#user_agentObject



50
51
52
# File 'lib/printreleaf/api.rb', line 50

def user_agent
  @user_agent || USER_AGENT
end

Instance Method Details

#delete(uri) ⇒ Object



66
67
68
# File 'lib/printreleaf/api.rb', line 66

def delete(uri)
  request :delete, uri
end

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



54
55
56
# File 'lib/printreleaf/api.rb', line 54

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

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



62
63
64
# File 'lib/printreleaf/api.rb', line 62

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

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



58
59
60
# File 'lib/printreleaf/api.rb', line 58

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

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/printreleaf/api.rb', line 70

def request(verb, uri, params={})
  perform_request do
    uri = Util.join_uri(endpoint, uri)
    url = "#{protocol}://#{uri}"

    request_params = {
      method: verb,
      url: url,
      headers: {
        accept: :json,
        :Authorization => "Bearer #{api_key}",
        :user_agent => user_agent
      }
    }

    if verb == :get || verb == :delete
      request_params[:headers][:params] = params unless params.empty?
    else
      request_params[:payload] = params.to_json
      request_params[:headers][:content_type] = :json
    end

    unless logger.nil?
      logger.info "[PrintReleaf] #{verb.upcase} #{uri}"
    end

    response = RestClient::Request.execute(request_params)

    JSON.parse(response.body)
  end
end