urbit-api 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'urbit/bucket'
4
+
5
+ module Urbit
6
+ class Setting
7
+ attr_accessor :buckets
8
+ attr_reader :desk, :ship
9
+
10
+ def initialize(ship:, desk:, buckets:)
11
+ @ship = ship
12
+ @desk = desk
13
+ @buckets = Set.new
14
+ buckets.each {|k, v| @buckets << Bucket.new(setting: self, name: k, entries: v)}
15
+ end
16
+
17
+ def ==(another_group)
18
+ another_setting.desk == self.desk
19
+ end
20
+
21
+ def <=>(another_group)
22
+ self.desk <=> another_group.desk
23
+ end
24
+
25
+ def [](bucket:)
26
+ self.buckets.select {|b| bucket == b.name}.first
27
+ end
28
+
29
+ def add_bucket(name:, entries:)
30
+ msg = {
31
+ "put-bucket": {
32
+ "bucket-key": "#{name}",
33
+ "desk": "#{self.desk}",
34
+ "bucket": entries
35
+ }
36
+ }
37
+ self.ship.poke(app: 'settings-store', mark: 'settings-event', message: msg)
38
+ nil
39
+ end
40
+
41
+ def entries(bucket:)
42
+ self[bucket: bucket].entries
43
+ end
44
+
45
+ def remove_bucket(name:)
46
+ msg = {
47
+ "del-bucket": {
48
+ "bucket-key": "#{name}",
49
+ "desk": "#{self.desk}"
50
+ }
51
+ }
52
+ self.ship.poke(app: 'settings-store', mark: 'settings-event', message: msg)
53
+ nil
54
+ end
55
+
56
+ def to_h
57
+ {
58
+ desk: @desk,
59
+ buckets: self.buckets,
60
+ }
61
+ end
62
+
63
+ def to_s
64
+ "a Setting(#{self.to_h})"
65
+ end
66
+
67
+ def to_string
68
+ "desk: #{self.desk}\n buckets: #{self.buckets.collect {|b| b.to_string}}"
69
+ end
70
+ end
71
+
72
+ class Settings < Set
73
+ class << self
74
+ def load(ship:)
75
+ ship.subscribe(app: 'settings-store', path: '/all')
76
+ scry = ship.scry(app: "settings-store", path: "/all", mark: "json")
77
+ # scry = self.scry(app: "settings-store", path: "/desk/#{desk}", mark: "json")
78
+ s = Settings.new
79
+ if scry[:body]
80
+ body = JSON.parse scry[:body]
81
+ body["all"].each do |k, v| # At this level the keys are the desks and the values are the buckets
82
+ s << Setting.new(ship: ship, desk: k, buckets: v)
83
+ end
84
+ end
85
+ s
86
+ end
87
+ end
88
+
89
+ def initialize
90
+ @hash = {}
91
+ end
92
+
93
+ def [](desk:)
94
+ self.select {|s| desk == s.desk}.first
95
+ end
96
+
97
+ def list
98
+ self.each {|s| puts s.to_string}
99
+ nil
100
+ end
101
+ end
102
+
103
+ end
data/lib/urbit/ship.rb CHANGED
@@ -3,19 +3,21 @@ require 'faraday'
3
3
  require 'urbit/channel'
4
4
  require 'urbit/config'
5
5
  require 'urbit/graph'
6
- require 'urbit/setting'
6
+ require 'urbit/group_manager'
7
+ require 'urbit/settings'
7
8
 
8
9
  module Urbit
9
10
  class Ship
10
11
  attr_accessor :logged_in
11
- attr_reader :auth_cookie, :channels, :config
12
+ attr_reader :auth_cookie, :channels, :config, :group_mgr
12
13
 
13
14
  def initialize(config: Config.new)
14
15
  @auth_cookie = nil
15
16
  @channels = []
16
17
  @config = config
17
18
  @graphs = []
18
- @settings = []
19
+ @group_mgr = GroupManager.new ship: self
20
+ @settings = nil # Use lazy initialization here
19
21
  @logged_in = false
20
22
  end
21
23
 
@@ -62,12 +64,28 @@ module Urbit
62
64
  self.graphs.collect {|g| g.resource}
63
65
  end
64
66
 
67
+ #
68
+ # Answers the Group uniquely keyed by path:, if it exists
69
+ #
70
+ def group(path:)
71
+ @group_mgr.find_by_path(path)
72
+ end
73
+
74
+ #
75
+ # Answers the object managing the Groups on this ship.
76
+ # This object provides all the helper methods to list, join, leave, &c. a Group
77
+ #
78
+ def groups
79
+ @group_mgr
80
+ end
81
+
65
82
  def login
66
83
  return self if logged_in?
67
84
 
68
85
  ensure_connections_closed
69
86
  response = Faraday.post(login_url, "password=#{config.code}")
70
87
  parse_cookie(response)
88
+ @group_mgr.load
71
89
  self
72
90
  end
73
91
 
@@ -75,6 +93,24 @@ module Urbit
75
93
  config.name
76
94
  end
77
95
 
96
+ def open_channels
97
+ @channels.select {|c| c.open?}
98
+ end
99
+
100
+ def pat_p
101
+ config.name
102
+ end
103
+
104
+ #
105
+ # Poke an app with a message using a mark.
106
+ #
107
+ # Returns a Channel which has been created and opened and will begin
108
+ # to get back a stream of facts via its Receiver.
109
+ #
110
+ def poke(app:, mark:, message:)
111
+ (self.add_channel).poke(app: app, mark: mark, message: message)
112
+ end
113
+
78
114
  def remove_graph(desk: 'landscape', graph:)
79
115
  delete_json = %Q({
80
116
  "delete": {
@@ -92,66 +128,10 @@ module Urbit
92
128
  retcode
93
129
  end
94
130
 
95
- #
96
- # Answers the entries for the specified desk and bucket.
97
- #
98
- def setting(desk: 'landscape', bucket:)
99
- if (settings = self.settings(desk: desk))
100
- settings.each do |setting|
101
- if (entries = setting.entries(bucket: bucket))
102
- return entries
103
- end
104
- end
105
- end
106
- {}
107
- end
108
-
109
- #
110
- # Answers a collection of all the settings for this ship.
111
- # This collection is cached and will need to be invalidated to discover new settings.
112
- #
113
- def settings(desk: 'landscape', flush_cache: false)
114
- @settings = [] if flush_cache
115
- if @settings.empty?
116
- if self.logged_in?
117
- scry = self.scry(app: "settings-store", path: "/desk/#{desk}", mark: "json")
118
- if scry[:body]
119
- body = JSON.parse scry[:body]
120
- body["desk"].each do |k|
121
- @settings << Setting.new(ship: self, desk: desk, setting: k)
122
- end
123
- end
124
- end
125
- end
126
- @settings
127
- end
128
-
129
- def untilded_name
130
- name.gsub('~', '')
131
- end
132
-
133
- def pat_p
134
- config.name
135
- end
136
-
137
- def open_channels
138
- @channels.select {|c| c.open?}
139
- end
140
-
141
- #
142
- # Poke an app with a message using a mark.
143
- #
144
- # Returns a Channel which has been created and opened and will begin
145
- # to get back a stream of facts via its Receiver.
146
- #
147
- def poke(app:, mark:, message:)
148
- (self.add_channel).poke(app: app, mark: mark, message: message)
149
- end
150
-
151
131
  def scry(app:, path:, mark: 'json')
152
132
  self.login
153
133
  mark = ".#{mark}" unless mark.empty?
154
- scry_url = "#{self.config.api_base_url}/~/scry/#{app}#{path}#{mark}"
134
+ scry_url = "#{self.url}/~/scry/#{app}#{path}#{mark}"
155
135
 
156
136
  response = Faraday.get(scry_url) do |req|
157
137
  req.headers['Accept'] = 'application/json'
@@ -161,9 +141,20 @@ module Urbit
161
141
  {status: response.status, code: response.reason_phrase, body: response.body}
162
142
  end
163
143
 
144
+ #
145
+ # Answers the object managing the Settings on this ship.
146
+ # This object provides all the helper methods to list, update, and remove a Setting
147
+ #
148
+ def settings
149
+ if self.logged_in?
150
+ @settings = Settings.load(ship: self) if @settings.nil?
151
+ end
152
+ @settings
153
+ end
154
+
164
155
  def spider(desk: 'landscape', mark_in:, mark_out:, thread:, data:, args: [])
165
156
  self.login
166
- url = "#{self.config.api_base_url}/spider/#{desk}/#{mark_in}/#{thread}/#{mark_out}.json"
157
+ url = "#{self.url}/spider/#{desk}/#{mark_in}/#{thread}/#{mark_out}.json"
167
158
 
168
159
  # TODO: This is a huge hack due to the fact that certain spider operations are known to
169
160
  # not return when they should. Instead I just set the timeout low and catch the
@@ -211,6 +202,14 @@ module Urbit
211
202
  "a Ship(#{self.to_h})"
212
203
  end
213
204
 
205
+ def untilded_name
206
+ name.gsub('~', '')
207
+ end
208
+
209
+ def url
210
+ self.config.api_base_url
211
+ end
212
+
214
213
  private
215
214
 
216
215
  def add_channel
@@ -229,7 +228,7 @@ module Urbit
229
228
  end
230
229
 
231
230
  def login_url
232
- "#{config.api_base_url}/~/login"
231
+ "#{self.url}/~/login"
233
232
  end
234
233
 
235
234
  def parse_cookie(resp)
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Urbit
4
+ VERSION = "0.4.0"
5
+ end
@@ -1,5 +1,6 @@
1
- require_relative './config'
2
- require_relative './ship'
1
+ require "urbit/config"
2
+ require "urbit/ship"
3
+ require "urbit/version"
3
4
 
4
5
  # This is the main namespace for Urbit.
5
6
  #
data/urbit-api.gemspec CHANGED
@@ -1,8 +1,8 @@
1
- require_relative 'lib/urbit/api/version'
1
+ require_relative 'lib/urbit/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "urbit-api"
5
- spec.version = Urbit::Api::VERSION
5
+ spec.version = Urbit::VERSION
6
6
  spec.authors = ["Daryl Richter"]
7
7
  spec.email = ["daryl@ngzax.com"]
8
8
 
@@ -18,17 +18,16 @@ Gem::Specification.new do |spec|
18
18
  spec.metadata["changelog_uri"] = "https://github.com/Zaxonomy/urbit-ruby/CHANGELOG.md"
19
19
 
20
20
  # Specify which files should be added to the gem when it is released.
21
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
23
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
24
- end
21
+ spec.files = Dir.glob("lib{.rb,/**/*}", File::FNM_DOTMATCH).reject {|f| File.directory?(f) }
22
+ spec.files += %w[urbit-api.gemspec] # include the gemspec itself because warbler breaks w/o it
25
23
 
26
24
  spec.bindir = "exe"
27
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
26
  spec.require_paths = ["lib"]
29
27
 
30
- spec.add_dependency "faraday", "~> 1.3.0"
31
- spec.add_dependency "ld-eventsource", "~> 2.0.0"
28
+ spec.add_dependency "faraday", "~> 2.2.0"
29
+ spec.add_dependency "ld-eventsource", "~> 2.2.0"
30
+ spec.add_dependency "uri", "0.10.0" # Pinning this for now b/c 0.11 is broken. :(
32
31
 
33
32
  spec.add_development_dependency "pry", "~> 0.13"
34
33
  spec.add_development_dependency "rspec", "~> 3.10"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: urbit-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daryl Richter
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-02 00:00:00.000000000 Z
11
+ date: 2022-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,28 +16,42 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.3.0
19
+ version: 2.2.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.3.0
26
+ version: 2.2.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: ld-eventsource
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: 2.0.0
33
+ version: 2.2.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: 2.0.0
40
+ version: 2.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: uri
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: pry
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -73,45 +87,32 @@ executables: []
73
87
  extensions: []
74
88
  extra_rdoc_files: []
75
89
  files:
76
- - ".gitignore"
77
- - ".rspec"
78
- - ".ruby-version"
79
- - CHANGELOG.md
80
- - Gemfile
81
- - LICENSE.txt
82
- - README.gem.md
83
- - README.md
84
- - Rakefile
85
- - _config.yml
86
- - bin/console
87
- - bin/setup
88
- - bin/test
90
+ - lib/urbit.rb
89
91
  - lib/urbit/ack_message.rb
90
92
  - lib/urbit/api.rb
91
- - lib/urbit/api/version.rb
93
+ - lib/urbit/bucket.rb
92
94
  - lib/urbit/channel.rb
93
95
  - lib/urbit/chat_channel.rb
94
96
  - lib/urbit/close_message.rb
95
97
  - lib/urbit/config.rb
96
98
  - lib/urbit/fact.rb
99
+ - lib/urbit/fact/base_fact.rb
100
+ - lib/urbit/fact/graph_fact.rb
101
+ - lib/urbit/fact/group_fact.rb
102
+ - lib/urbit/fact/settings_fact.rb
97
103
  - lib/urbit/graph.rb
104
+ - lib/urbit/group.rb
105
+ - lib/urbit/group_manager.rb
106
+ - lib/urbit/group_parser.rb
98
107
  - lib/urbit/message.rb
99
108
  - lib/urbit/node.rb
100
109
  - lib/urbit/parser.rb
101
110
  - lib/urbit/poke_message.rb
102
111
  - lib/urbit/receiver.rb
103
- - lib/urbit/setting.rb
112
+ - lib/urbit/settings.rb
104
113
  - lib/urbit/ship.rb
105
114
  - lib/urbit/subscribe_message.rb
106
- - lib/urbit/urbit.rb
107
- - misc/graph-store_graph
108
- - misc/graph-store_keys
109
- - misc/graph-store_node
110
- - misc/graph-store_update
111
- - misc/graph-update_add-graph
112
- - misc/graph-update_add-nodes
113
- - misc/post
114
- - misc/settings-store.json
115
+ - lib/urbit/version.rb
115
116
  - urbit-api.gemspec
116
117
  homepage: https://www.ngzax.com
117
118
  licenses:
data/.gitignore DELETED
@@ -1,25 +0,0 @@
1
- _config-*.yml
2
- *.gem
3
- *.rbc
4
- /coverage/
5
- /pkg/
6
- /test/tmp/
7
- /test/version_tmp/
8
- /tmp/
9
-
10
- ## Documentation cache and generated files:
11
- /.yardoc/
12
- /_yardoc/
13
- /doc/
14
- /rdoc/
15
-
16
- ## Environment normalization:
17
- /.bundle/
18
- /vendor/bundle
19
- /lib/bundler/man/
20
-
21
- # for a library or gem, you might want to ignore these files since the code is
22
- # intended to run in multiple environments; otherwise, check them in:
23
- Gemfile.lock
24
- .ruby-version
25
- # .ruby-gemset
data/.rspec DELETED
@@ -1 +0,0 @@
1
- --require spec_helper
data/.ruby-version DELETED
@@ -1,2 +0,0 @@
1
- 2.7.4
2
-
data/CHANGELOG.md DELETED
@@ -1 +0,0 @@
1
- Nothing to see here.
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in urbit-api.gemspec
4
- gemspec
5
-
6
- gem "rake", "~> 12.0"
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2021 Daryl Richter
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
data/README.gem.md DELETED
@@ -1,4 +0,0 @@
1
-
2
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/urbit/ruby`. To experiment with that code, run `bin/console` for an interactive prompt.
3
-
4
- TODO: Delete this and the text above, and describe your gem