Class: PagerJudy::API::Resource
- Inherits:
-
Object
- Object
- PagerJudy::API::Resource
- Defined in:
- lib/pager_judy/api/resource.rb
Overview
Represents an API endpoint.
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #dry_run? ⇒ Boolean
- #get(query = nil) ⇒ Object
-
#initialize(api_key:, uri:, logger: nil, dry_run: false) ⇒ Resource
constructor
A new instance of Resource.
- #post(data) ⇒ Object
- #put(data) ⇒ Object
- #subresource(path) ⇒ Object
Constructor Details
#initialize(api_key:, uri:, logger: nil, dry_run: false) ⇒ Resource
Returns a new instance of Resource.
13 14 15 16 17 18 19 |
# File 'lib/pager_judy/api/resource.rb', line 13 def initialize(api_key:, uri:, logger: nil, dry_run: false) @api_key = api_key @uri = URI(uri.to_s.chomp("/")) @type = @uri.to_s.split("/").last @logger = logger || Logger.new(nil) @dry_run = dry_run end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
21 22 23 |
# File 'lib/pager_judy/api/resource.rb', line 21 def api_key @api_key end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
24 25 26 |
# File 'lib/pager_judy/api/resource.rb', line 24 def logger @logger end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
23 24 25 |
# File 'lib/pager_judy/api/resource.rb', line 23 def type @type end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
22 23 24 |
# File 'lib/pager_judy/api/resource.rb', line 22 def uri @uri end |
Instance Method Details
#dry_run? ⇒ Boolean
26 27 28 |
# File 'lib/pager_judy/api/resource.rb', line 26 def dry_run? @dry_run end |
#get(query = nil) ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/pager_judy/api/resource.rb', line 34 def get(query = nil) request = new_request request.query = query if query debug("GET", request.url) response = HTTPI.get(request) raise HttpError.new(request, response) if response.error? MultiJson.load(response.body) end |
#post(data) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/pager_judy/api/resource.rb', line 43 def post(data) request = new_request request.body = MultiJson.dump(data) debug("POST", request.url, data) response = HTTPI.post(request) raise HttpError.new(request, response) if response.error? MultiJson.load(response.body) end |
#put(data) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'lib/pager_judy/api/resource.rb', line 52 def put(data) request = new_request request.body = MultiJson.dump(data) debug("PUT", request.url, data) response = HTTPI.put(request) raise HttpError.new(request, response) if response.error? MultiJson.load(response.body) end |