Class: Layer::HttpClient
- Inherits:
-
Object
- Object
- Layer::HttpClient
- Defined in:
- lib/layer/http_client.rb
Constant Summary collapse
- DEFAULT_HOST =
"https://api.layer.com"
Instance Attribute Summary collapse
-
#api_token ⇒ Object
readonly
Returns the value of attribute api_token.
-
#app_id ⇒ Object
readonly
Returns the value of attribute app_id.
Instance Method Summary collapse
- #base_url ⇒ Object
- #call(method, url, options = {}) ⇒ Object
- #connection ⇒ Object
- #default_layer_headers ⇒ Object
- #delete(url) ⇒ Object
- #get(url, options = {}) ⇒ Object
-
#initialize(app_id, api_token) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #layer_patch_header ⇒ Object
- #patch(url, options = {}) ⇒ Object
- #post(url, options = {}) ⇒ Object
- #run_request(method, url, options = {}) ⇒ Object
Constructor Details
#initialize(app_id, api_token) ⇒ HttpClient
Returns a new instance of HttpClient.
9 10 11 12 |
# File 'lib/layer/http_client.rb', line 9 def initialize(app_id, api_token) @app_id = app_id @api_token = api_token end |
Instance Attribute Details
#api_token ⇒ Object (readonly)
Returns the value of attribute api_token.
7 8 9 |
# File 'lib/layer/http_client.rb', line 7 def api_token @api_token end |
#app_id ⇒ Object (readonly)
Returns the value of attribute app_id.
7 8 9 |
# File 'lib/layer/http_client.rb', line 7 def app_id @app_id end |
Instance Method Details
#base_url ⇒ Object
68 69 70 |
# File 'lib/layer/http_client.rb', line 68 def base_url "#{DEFAULT_HOST}/apps/#{app_id}" end |
#call(method, url, options = {}) ⇒ Object
39 40 41 42 |
# File 'lib/layer/http_client.rb', line 39 def call(method, url, = {}) response = run_request(method, url, ) response.body.empty? ? nil : JSON.parse(response.body) end |
#connection ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/layer/http_client.rb', line 14 def connection @connection ||= Faraday.new(url: base_url) do |faraday| faraday.headers = default_layer_headers faraday.request :url_encoded faraday.adapter Faraday.default_adapter faraday.use Middleware::ApiErrors end end |
#default_layer_headers ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/layer/http_client.rb', line 56 def default_layer_headers { 'Accept' => 'application/vnd.layer+json; version=1.0', 'Authorization' => "Bearer #{api_token}", 'Content-Type' => 'application/json' } end |
#delete(url) ⇒ Object
35 36 37 |
# File 'lib/layer/http_client.rb', line 35 def delete(url) call(:delete, url) end |
#get(url, options = {}) ⇒ Object
23 24 25 |
# File 'lib/layer/http_client.rb', line 23 def get(url, = {}) call(:get, url, ) end |
#layer_patch_header ⇒ Object
64 65 66 |
# File 'lib/layer/http_client.rb', line 64 def layer_patch_header { 'Content-Type' => 'application/vnd.layer-patch+json' } end |
#patch(url, options = {}) ⇒ Object
31 32 33 |
# File 'lib/layer/http_client.rb', line 31 def patch(url, = {}) call(:patch, url, ) end |
#post(url, options = {}) ⇒ Object
27 28 29 |
# File 'lib/layer/http_client.rb', line 27 def post(url, = {}) call(:post, url, ) end |
#run_request(method, url, options = {}) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/layer/http_client.rb', line 44 def run_request(method, url, = {}) headers = [:headers] || {} headers["If-None-Match"] = SecureRandom.uuid connection.run_request( method, url, [:body], headers ) end |