Class: Ossert::Fetch::SimpleClient
- Inherits:
-
Object
- Object
- Ossert::Fetch::SimpleClient
- Defined in:
- lib/ossert/fetch.rb
Overview
Public: Simple client for fetching HTTP API
Examples
client = SimpleClient.new("http://bestgems.org/api/v1/")
client.get("gems/#{project.rubygems_alias}/total_downloads.json")
# => Some JSON from api
Defined Under Namespace
Classes: NotFound, UnexpectedResponseError
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
readonly
Returns the value of attribute api_endpoint.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#get(path) ⇒ Object
Public: Get data via HTTP GET for given API path.
-
#initialize(api_endpoint, type = nil) ⇒ SimpleClient
constructor
Public: Instantiate client for fetching API for given api_endpoint and response type.
Constructor Details
#initialize(api_endpoint, type = nil) ⇒ SimpleClient
Public: Instantiate client for fetching API for given api_endpoint and response type
path - The String describes path of endpoint to access the data type - The String describes type of response data, e.g. ‘json’
Examples
client = SimpleClient.new("http://bestgems.org/api/v1/")
client.get("gems/#{project.rubygems_alias}/total_downloads.json")
# => Some JSON from api
Returns nothing.
96 97 98 99 100 |
# File 'lib/ossert/fetch.rb', line 96 def initialize(api_endpoint, type = nil) raise ArgumentError if !api_endpoint.start_with?('http') || !api_endpoint.end_with?('/') @api_endpoint = api_endpoint @type = type || 'json' end |
Instance Attribute Details
#api_endpoint ⇒ Object (readonly)
Returns the value of attribute api_endpoint.
82 83 84 |
# File 'lib/ossert/fetch.rb', line 82 def api_endpoint @api_endpoint end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
82 83 84 |
# File 'lib/ossert/fetch.rb', line 82 def type @type end |
Instance Method Details
#get(path) ⇒ Object
Public: Get data via HTTP GET for given API path
path - The String describes path of endpoint to access the data
Examples
client = SimpleClient.new("http://bestgems.org/api/v1/")
client.get("gems/#{project.rubygems_alias}/total_downloads.json")
# => Some JSON from api
Returns nothing.
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ossert/fetch.rb', line 116 def get(path) raise ArgumentError unless path.end_with? type response = agent.get("#{api_endpoint}#{path}") case response.status when 404 raise NotFound when 200 JSON.parse(response.body) else raise UnexpectedResponseError end end |