Class: Dev::BloomGrowth

Inherits:
Object show all
Defined in:
lib/firespring_dev_commands/bloom_growth.rb,
lib/firespring_dev_commands/bloom_growth/rock.rb,
lib/firespring_dev_commands/bloom_growth/seat.rb,
lib/firespring_dev_commands/bloom_growth/user.rb

Overview

Class for interacting with the Bloom Growth api

Defined Under Namespace

Classes: Config, Rock, Seat, User

Constant Summary collapse

CONFIG_FILE =

The config file to try to load credentials from

"#{Dir.home}/.env.bloom".freeze
BLOOM_USERNAME =

The text of the username variable key

'BLOOM_USERNAME'.freeze
BLOOM_PASSWORD =

The text of the password variable key

'BLOOM_PASSWORD'.freeze
BLOOM_TOKEN =

The text of the token variable key

'BLOOM_TOKEN'.freeze
BLOOM_URL =

The text of the url variable key

'BLOOM_URL'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username: self.class.config.username, password: self.class.config.password, url: self.class.config.url) ⇒ BloomGrowth

Initialize a new target process client using the given inputs



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 50

def initialize(username: self.class.config.username, password: self.class.config.password, url: self.class.config.url)
  raise 'username is required' if username.to_s.strip.empty?
  raise 'password is required' if password.to_s.strip.empty?
  raise 'url is required' if url.to_s.strip.empty?

  @username = username
  @password = password
  @url = url
  uri = URI.parse(@url)
  @client = Net::HTTP.new(uri.host, uri.port)
  @client.use_ssl = true
  @client.verify_mode = OpenSSL::SSL::VERIFY_PEER
  @client.set_debug_output(LOG) if self.class.config.http_debug
  @default_headers = {
    'authorization' => "Bearer #{token}",
    'content-type' => 'application/json',
    'accept' => 'application/json'
  }
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



47
48
49
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 47

def client
  @client
end

#default_headersObject

Returns the value of attribute default_headers.



47
48
49
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 47

def default_headers
  @default_headers
end

#passwordObject

Returns the value of attribute password.



47
48
49
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 47

def password
  @password
end

#tokenObject

Method for getting a bearer token for the bloom growth api. There are a couple of possible logic paths

- If a token has already been defined, use it
- If a token is found in the ENV, use it
- Otherwise, use the username and passowrd that has been configured to request a new token from bloom


74
75
76
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 74

def token
  @token
end

#urlObject

Returns the value of attribute url.



47
48
49
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 47

def url
  @url
end

#usernameObject

Returns the value of attribute username.



47
48
49
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 47

def username
  @username
end

Class Method Details

.config {|@config| ... } ⇒ Object Also known as: configure

Instantiates a new top level config object if one hasn’t already been created Yields that config object to any given block Returns the resulting config object

Yields:



37
38
39
40
41
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 37

def config
  @config ||= Config.new
  yield(@config) if block_given?
  @config
end

Instance Method Details

#get(path, query_string: nil, headers: default_headers) ⇒ Object

Perform a get request to the given path using the given query Call the given block (if present) with each piece of data Return all pieces of data



111
112
113
114
115
116
117
118
119
120
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 111

def get(path, query_string: nil, headers: default_headers, &)
  url = path
  url << "?#{URI.encode_www_form(query_string)}" unless query_string.to_s.strip.empty?

  response = client.request_get(url, headers)
  raise "Error querying #{url} [#{query_string}]: #{response.inspect}" unless response.response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body).each(&)
  nil
end

#post(path, data, headers: default_headers) ⇒ Object

Perform a post request to the given path using the gien data Return the parsed json body



124
125
126
127
128
129
130
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 124

def post(path, data, headers: default_headers)
  data = data.to_json unless data.is_a?(String)
  response = client.request_post(path, data, headers)
  raise "Error querying #{url}/#{path}: #{response.inspect}" unless response.response.is_a?(Net::HTTPSuccess)

  JSON.parse(response.body)
end

#visible_usersObject

Return all user objects visible to the logged in user



99
100
101
102
103
104
105
106
# File 'lib/firespring_dev_commands/bloom_growth.rb', line 99

def visible_users(&)
  [].tap do |ary|
    get('/api/v1/users/mineviewable') do |user_data|
      ary << User.new(user_data)
    end
    ary.each(&)
  end
end