Class: CWR

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/cwr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(captian_webhooks_base_uri = 'http://0.0.0.0:3000', producer_path = nil, consumer_path = nil, webhook_path = nil) ⇒ CWR

Returns a new instance of CWR.



115
116
117
118
119
120
121
122
123
# File 'lib/cwr.rb', line 115

def initialize(captian_webhooks_base_uri='http://0.0.0.0:3000',
               producer_path=nil,
               consumer_path=nil,
               webhook_path=nil)
  @PRODUCER_PATH = producer_path || "/producers"
  @CONSUMER_PATH = consumer_path || "/consumers"
  @WEBHOOK_PATH = webhook_path || "/webhooks"
  self.class.base_uri captian_webhooks_base_uri
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



112
113
114
# File 'lib/cwr.rb', line 112

def access_token
  @access_token
end

#emailObject Also known as: username

Returns the value of attribute email.



112
113
114
# File 'lib/cwr.rb', line 112

def email
  @email
end

#passwordObject

Returns the value of attribute password.



112
113
114
# File 'lib/cwr.rb', line 112

def password
  @password
end

#producerObject

Returns the value of attribute producer.



112
113
114
# File 'lib/cwr.rb', line 112

def producer
  @producer
end

Instance Method Details

#create_consumer(producer, name) ⇒ Object



138
139
140
141
142
143
144
145
# File 'lib/cwr.rb', line 138

def create_consumer(producer, name)
  consumer = { name: name, producer_id: producer.id }
  body = { consumer: consumer }
  resp = securely_post( @CONSUMER_PATH, body )
  location = resp.headers['location']
  consumer_path = location.split(self.class.base_uri)[-1]
  return Consumer.new( self, producer, name, consumer_path )
end

#create_mass_webhook(producer, consumers, post_data = nil) ⇒ Object



222
223
224
# File 'lib/cwr.rb', line 222

def create_mass_webhook(producer, consumers, post_data=nil)
  raise "NOT IMPLEMENTED"
end

#create_producer(name) ⇒ Object



129
130
131
132
133
134
135
136
# File 'lib/cwr.rb', line 129

def create_producer(name)
  body = { producer: { name: name } }
  resp = securely_post( @PRODUCER_PATH, body )
  
  location = resp.headers['location']
  producer_path = location.split(self.class.base_uri)[-1]
  return Producer.new( self, name, producer_path )
end

#create_webhook(consumer, post_uri, post_data = nil, post_headers = nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/cwr.rb', line 147

def create_webhook(consumer, post_uri, post_data=nil, post_headers=nil)
  post_data = post_data.to_json unless post_data.is_a? String or post_data.nil?
  post_headers = post_headers.to_json unless post_headers.is_a? String or post_headers.nil?

  webhook = { post_uri: post_uri,
    post_data: post_data,
    consumer_id: consumer.id }

  webhook[:post_headers] = post_headers if post_headers

  body = { webhook: webhook }
  resp = securely_post( @WEBHOOK_PATH, body )
  location = resp.headers['location']
  webhook_path = location.split(self.class.base_uri)[-1]
  Webhook.new(self, webhook_path)
end

#destroy_consumer(consumer) ⇒ Object



168
169
170
# File 'lib/cwr.rb', line 168

def destroy_consumer(consumer)
  resp = securely_delete(consumer.path)
end

#destroy_producer(producer) ⇒ Object



172
173
174
# File 'lib/cwr.rb', line 172

def destroy_producer(producer)
  resp = securely_delete(producer.path)
end

#list_consumers(producer, &block) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/cwr.rb', line 176

def list_consumers(producer, &block)
  block_given = !!block
  params = { producer_id: producer.id }
  resp = securely_get(@CONSUMER_PATH,
                      params)
  consumers = resp['consumers']
  collector = [] unless block_given
  consumers.map do |p|
    consumer = p['consumers']
    name = consumer['name']
    id = consumer['id']
    path = "#{@CONSUMER_PATH}/#{id}"
    
    consumer = Consumer.new( self, producer, name, path )

    if block_given
      yield consumer
    else
      collector << consumer
    end
  end
  return collector
end

#list_producers(&block) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/cwr.rb', line 200

def list_producers(&block)
  block_given = !!block
  params = { email: @email }
  resp = securely_get(@PRODUCER_PATH,
                      params)
  producers = resp['producers']
  collector = [] unless block_given
  producers.map do |producer|
    name = producer['name']
    id = producer['id']
    owner_id = producer['owner_id']
    path = "#{@PRODUCER_PATH}/#{id}"
    producer = Producer.new(self, name, path, owner_id)
    if block_given
      yield producer
    else
      collector << producer
    end
  end
  return collector
end

#list_webhooksObject



226
227
228
# File 'lib/cwr.rb', line 226

def list_webhooks
  return []
end

#update_webhook(webhook_path) ⇒ Object



164
165
166
# File 'lib/cwr.rb', line 164

def update_webhook(webhook_path)
  securely_get( webhook_path )
end

#username=(other) ⇒ Object



125
126
127
# File 'lib/cwr.rb', line 125

def username=(other)
  @email = other
end

#yeearrObject



230
231
232
233
# File 'lib/cwr.rb', line 230

def yeearr
  :jolly_roger
  "yeearr"
end