Class: Blockmason::Link::Session
- Inherits:
-
Object
- Object
- Blockmason::Link::Session
- Defined in:
- lib/blockmason/link/session.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#base_url ⇒ Object
Returns the value of attribute base_url.
-
#http ⇒ Object
Returns the value of attribute http.
-
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
Instance Method Summary collapse
- #get(path, inputs = {}) ⇒ Object
-
#initialize(access_token:, base_url:, http:, refresh_token:) ⇒ Session
constructor
A new instance of Session.
- #post(path, inputs = {}) ⇒ Object
- #refresh! ⇒ Object
Constructor Details
#initialize(access_token:, base_url:, http:, refresh_token:) ⇒ Session
Returns a new instance of Session.
10 11 12 13 14 15 |
# File 'lib/blockmason/link/session.rb', line 10 def initialize(access_token:, base_url:, http:, refresh_token:) @access_token = access_token @base_url = base_url @http = http @refresh_token = refresh_token end |
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
8 9 10 |
# File 'lib/blockmason/link/session.rb', line 8 def access_token @access_token end |
#base_url ⇒ Object
Returns the value of attribute base_url.
8 9 10 |
# File 'lib/blockmason/link/session.rb', line 8 def base_url @base_url end |
#http ⇒ Object
Returns the value of attribute http.
8 9 10 |
# File 'lib/blockmason/link/session.rb', line 8 def http @http end |
#refresh_token ⇒ Object
Returns the value of attribute refresh_token.
8 9 10 |
# File 'lib/blockmason/link/session.rb', line 8 def refresh_token @refresh_token end |
Instance Method Details
#get(path, inputs = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/blockmason/link/session.rb', line 17 def get(path, inputs = {}) query = URI.encode_www_form(inputs) query = "?#{query}" unless query.empty? url = "#{@base_url}/v1#{path}#{query}" request = Net::HTTP::Get.new(url) request['Authorization'] = "Bearer #{@access_token}" response = @http.request(request) outputs = JSON.parse(response.body) outputs end |
#post(path, inputs = {}) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/blockmason/link/session.rb', line 33 def post(path, inputs = {}) request = Net::HTTP::Post.new("#{@base_url}/v1#{path}") request['Content-Type'] = 'application/json' request['Authorization'] = "Bearer #{@access_token}" response = @http.request(request, inputs.to_json) outputs = JSON.parse(response.body) outputs end |
#refresh! ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/blockmason/link/session.rb', line 46 def refresh! request = Net::HTTP::Post.new("#{@base_url}/oauth2/token") request['Content-Type'] = 'application/json' response = @http.request(request, { grant_type: 'refresh_token', refresh_token: @refresh_token }.to_json) grant = JSON.parse(response.body) raise response.body if grant.has_key?('errors') self.new(access_token: grant['access_token'], base_url: @base_url, http: @http, refresh_token: grant['refresh_token']) end |