blockmason-link 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 06545d74b63e712fd77f6995384e9564851bceae00d670517728487cc23d2ec7
4
+ data.tar.gz: e7e62470ffb36442822d02e5fa5f7f02e6c9a01dde2c5a1f6a3d91e34a9d6ebc
5
+ SHA512:
6
+ metadata.gz: a2925a410939cf4af6ef7f2d435baa0ac9b0d493864ab9325edd07c6535c035677024463f3baa9cff563a7a45cde2dbbb92e1357e64ee50e4fd604135fe2b4d7
7
+ data.tar.gz: '082d4651c2cf95303f6b2923d7bc08b2b98342324f25247d4c05a39569bc7ce4e6a91fa8922286e9ada12d42f7c1a7f3a02a86883bfcbdc8bbe76e80987cbe22'
data/lib/blockmason.rb ADDED
@@ -0,0 +1,4 @@
1
+ module Blockmason
2
+ end
3
+
4
+ require_relative './blockmason/link'
@@ -0,0 +1,10 @@
1
+ module Blockmason
2
+ module Link
3
+ end
4
+ end
5
+
6
+ require_relative './link/connection'
7
+ require_relative './link/managed_session'
8
+ require_relative './link/provider'
9
+ require_relative './link/session'
10
+ require_relative './link/project'
@@ -0,0 +1,38 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ require_relative './managed_session'
5
+ require_relative './session'
6
+
7
+ module Blockmason
8
+ module Link
9
+ class Connection
10
+ attr_accessor :base_url, :http
11
+
12
+ def initialize(base_url:, http:)
13
+ @base_url = base_url
14
+ @http = http
15
+ end
16
+
17
+ def authenticate!(client_id:, client_secret:)
18
+ request = Net::HTTP::Post.new("#{@base_url}/oauth2/token")
19
+
20
+ request['Content-Type'] = 'application/json'
21
+
22
+ response = @http.request(request, {
23
+ client_id: client_id,
24
+ client_secret: client_secret,
25
+ grant_type: 'client_credentials'
26
+ }.to_json)
27
+
28
+ grant = JSON.parse(response.body)
29
+
30
+ raise response.body if grant.has_key?('errors')
31
+
32
+ session = ::Blockmason::Link::Session.new(access_token: grant['access_token'], base_url: @base_url, http: @http, refresh_token: grant['refresh_token'])
33
+
34
+ ::Blockmason::Link::ManagedSession.new(session: session)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,39 @@
1
+ require_relative './session'
2
+
3
+ module Blockmason
4
+ module Link
5
+ class ManagedSession
6
+ attr_accessor :session
7
+
8
+ def initialize(session:)
9
+ @session = session
10
+ end
11
+
12
+ def post(path, inputs)
13
+ begin
14
+ @session.post(path, inputs)
15
+ rescue error
16
+ if error["errors"].any? { |it| it['detail'] =~ /Authentication failed/ }
17
+ @session = @session.refresh!
18
+ @session.post(path, inputs)
19
+ else
20
+ raise error
21
+ end
22
+ end
23
+ end
24
+
25
+ def get(path, inputs)
26
+ begin
27
+ @session.get(path, inputs)
28
+ rescue error
29
+ if error["errors"].any? { |it| it['detail'] =~ /Authentication failed/ }
30
+ @session = @session.refresh!
31
+ @session.get(path, inputs)
32
+ else
33
+ raise error
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,52 @@
1
+ require_relative './provider'
2
+
3
+ module Blockmason
4
+ module Link
5
+ class Project
6
+ attr_accessor :base_url, :client_id, :client_secret, :connection, :provider, :session
7
+
8
+ def initialize(base_url: ::Blockmason::Link::Provider.default_url, client_id:, client_secret:)
9
+ @base_url = base_url
10
+ @client_id = client_id
11
+ @client_secret = client_secret
12
+ end
13
+
14
+ def get(path, inputs = {})
15
+ session.get(path, inputs)
16
+ end
17
+
18
+ def post(path, inputs = {})
19
+ session.post(path, inputs)
20
+ end
21
+
22
+ protected
23
+ def connection
24
+ @connection ||= connection!
25
+ end
26
+
27
+ def provider
28
+ @provider ||= provider!
29
+ end
30
+
31
+ def session
32
+ @session ||= session!
33
+ end
34
+
35
+ def connection!
36
+ @connection = provider.connect!(@base_url)
37
+ @connection
38
+ end
39
+
40
+ def provider!
41
+ @provider = ::Blockmason::Link::Provider.new
42
+ @provider
43
+ end
44
+
45
+ def session!
46
+ @session = connection.authenticate!(client_id: @client_id, client_secret: @client_secret)
47
+ @session
48
+ end
49
+ end
50
+ end
51
+ end
52
+
@@ -0,0 +1,22 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ require_relative './connection'
5
+
6
+ module Blockmason
7
+ module Link
8
+ class Provider
9
+ def self.default_url
10
+ 'https://api.block.mason.link'
11
+ end
12
+
13
+ def connect!(base_url = ::Blockmason::Link::Provider.default_url)
14
+ uri = URI(base_url)
15
+
16
+ Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
17
+ ::Blockmason::Link::Connection.new(base_url: base_url, http: http)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,64 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+
5
+ module Blockmason
6
+ module Link
7
+ class Session
8
+ attr_accessor :access_token, :base_url, :http, :refresh_token
9
+
10
+ def initialize(access_token:, base_url:, http:, refresh_token:)
11
+ @access_token = access_token
12
+ @base_url = base_url
13
+ @http = http
14
+ @refresh_token = refresh_token
15
+ end
16
+
17
+ def get(path, inputs = {})
18
+ query = URI.encode_www_form(inputs)
19
+ query = "?#{query}" unless query.empty?
20
+
21
+ url = "#{@base_url}/v1#{path}#{query}"
22
+ request = Net::HTTP::Get.new(url)
23
+
24
+ request['Authorization'] = "Bearer #{@access_token}"
25
+
26
+ response = @http.request(request)
27
+
28
+ outputs = JSON.parse(response.body)
29
+
30
+ outputs
31
+ end
32
+
33
+ def post(path, inputs = {})
34
+ request = Net::HTTP::Post.new("#{@base_url}/v1#{path}")
35
+
36
+ request['Content-Type'] = 'application/json'
37
+ request['Authorization'] = "Bearer #{@access_token}"
38
+
39
+ response = @http.request(request, inputs.to_json)
40
+
41
+ outputs = JSON.parse(response.body)
42
+
43
+ outputs
44
+ end
45
+
46
+ def refresh!
47
+ request = Net::HTTP::Post.new("#{@base_url}/oauth2/token")
48
+
49
+ request['Content-Type'] = 'application/json'
50
+
51
+ response = @http.request(request, {
52
+ grant_type: 'refresh_token',
53
+ refresh_token: @refresh_token
54
+ }.to_json)
55
+
56
+ grant = JSON.parse(response.body)
57
+
58
+ raise response.body if grant.has_key?('errors')
59
+
60
+ self.new(access_token: grant['access_token'], base_url: @base_url, http: @http, refresh_token: grant['refresh_token'])
61
+ end
62
+ end
63
+ end
64
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blockmason-link
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Devin Canterberry
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Interact with your Blockmason Link projects with ease.
14
+ email: devin@blockmason.io
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/blockmason.rb
20
+ - lib/blockmason/link.rb
21
+ - lib/blockmason/link/connection.rb
22
+ - lib/blockmason/link/managed_session.rb
23
+ - lib/blockmason/link/project.rb
24
+ - lib/blockmason/link/provider.rb
25
+ - lib/blockmason/link/session.rb
26
+ homepage: https://blockmason.link
27
+ licenses:
28
+ - MIT
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubygems_version: 3.0.3
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Blockmason Link SDK
49
+ test_files: []