backchat-client 0.1 → 0.2

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.
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 3 and
6
8
  (
@@ -12,7 +14,7 @@ api_key = ARGV.shift
12
14
  stream_slug = ARGV.shift
13
15
  channel = ARGV.shift
14
16
 
15
- bc = Backchat::Client.new(api_key)
17
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
16
18
  bc.logger.level = Logger::DEBUG
17
19
 
18
20
  begin
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 2 and
6
8
  (
@@ -14,7 +16,14 @@ type = ARGV.shift
14
16
 
15
17
  name = ARGV.shift
16
18
 
17
- bc = Backchat::Client.new(api_key)
19
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
18
20
  bc.logger.level = Logger::DEBUG
19
21
 
20
- puts bc.create_channel(type, name)
22
+ begin
23
+ puts bc.create_channel(type, name)
24
+ rescue BackchatClient::Error::ConflictError => ex
25
+ puts "channel already exists"
26
+ rescue BackchatClient::Error::UnprocessableError => ex
27
+ puts "invalid channel name"
28
+ p ex.errors
29
+ end
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 2 and
6
8
  (
@@ -12,7 +14,7 @@ api_key = ARGV.shift
12
14
 
13
15
  stream = ARGV.shift
14
16
 
15
- bc = Backchat::Client.new(api_key)
17
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
16
18
  bc.logger.level = Logger::DEBUG
17
19
 
18
20
  puts bc.create_stream(stream)
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 2 and
6
8
  (
@@ -12,7 +14,7 @@ api_key = ARGV.shift
12
14
 
13
15
  channel = ARGV.shift
14
16
 
15
- bc = Backchat::Client.new(api_key)
17
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
16
18
  bc.logger.level = Logger::DEBUG
17
19
 
18
20
  puts bc.destroy_channel(channel, true)
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 2 and
6
8
  (
@@ -12,7 +14,7 @@ api_key = ARGV.shift
12
14
 
13
15
  stream = ARGV.shift
14
16
 
15
- bc = Backchat::Client.new(api_key)
17
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
16
18
  bc.logger.level = Logger::DEBUG
17
19
 
18
20
  puts bc.destroy_stream(stream)
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 1 and
6
8
  (
@@ -10,7 +12,7 @@ ARGV.length < 1 and
10
12
 
11
13
  api_key = ARGV.shift
12
14
 
13
- bc = Backchat::Client.new(api_key)
15
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
14
16
 
15
17
  bc.logger.level = Logger::DEBUG
16
18
 
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 1 and
6
8
  (
@@ -10,7 +12,7 @@ ARGV.length < 1 and
10
12
 
11
13
  api_key = ARGV.shift
12
14
 
13
- bc = Backchat::Client.new(api_key)
15
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
14
16
  bc.logger.level=Logger::DEBUG
15
17
 
16
18
  begin
data/examples/setup.rb ADDED
@@ -0,0 +1 @@
1
+ BACKCHAT_ENDPOINT = "https://api.backchat.io/1"
data/examples/stream.rb CHANGED
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 1 and
6
8
  (
@@ -17,7 +19,7 @@ stream = if ARGV.length < 1
17
19
  end
18
20
 
19
21
  Backchat::Client.log_level=Logger::DEBUG
20
- bc = Backchat::Client.new(api_key)
22
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
21
23
 
22
24
  begin
23
25
  puts bc.find_stream(stream)
data/examples/user.rb CHANGED
@@ -1,6 +1,8 @@
1
- $:.unshift File.join(File.dirname(__FILE__),'..','lib')
1
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $:.unshift File.join(File.dirname(__FILE__), '.')
2
3
 
3
4
  require 'backchat-client'
5
+ require 'setup'
4
6
 
5
7
  ARGV.length < 1 and
6
8
  (
@@ -11,6 +13,7 @@ ARGV.length < 1 and
11
13
  api_key = ARGV.shift
12
14
 
13
15
 
14
- bc = Backchat::Client.new(api_key)
16
+ Backchat::Client.log_level = Logger::DEBUG
17
+ bc = Backchat::Client.new(api_key, BACKCHAT_ENDPOINT)
15
18
  puts bc.get_profile
16
19
 
@@ -2,11 +2,14 @@ require 'backchat_client/channel'
2
2
  require 'backchat_client/stream'
3
3
  require 'backchat_client/user'
4
4
  require 'backchat_client/backchat_logger'
5
+ require 'backchat_client/error'
5
6
  require 'addressable/uri'
6
7
 
7
8
  module Backchat
8
9
 
9
10
  include BackchatClient::BackchatLogger
11
+
12
+ autoload :ClientError, 'error/client_error'
10
13
 
11
14
  #
12
15
  # This class is the main entry point to use the backchat-client gem.
@@ -63,7 +66,6 @@ module Backchat
63
66
  #
64
67
  # Retrieves a specific channel or all the channels associated to the api_key
65
68
  # @return one or more channel data
66
- #
67
69
  def find_channel
68
70
  channels = channel.find
69
71
 
@@ -76,15 +78,15 @@ module Backchat
76
78
  end
77
79
  end
78
80
 
79
- #
80
81
  # Creates a specific channel
81
- # @param channel_type kind of channel: check Backchat documentation about supported channel types
82
- # @param id channel unique identifier (in user scope)
83
- #
84
- def create_channel(channel_type, id, bql=nil)
85
- _channel = channel.create(generate_channel_url(channel_type, id, bql))
82
+ # @param uri Full URI of the channel: <type>://<address>
83
+ # @param bql optional backchat filter
84
+ # @return response body returned by backchat
85
+ def create_channel(uri, bql=nil)
86
+ _channel = channel.create(generate_channel_url(uri, bql))
86
87
 
87
88
  if _channel.respond_to?("has_key?") and _channel.has_key?("data")
89
+ logger.debug("Channel created in Backchat #{_channel}")
88
90
  _channel["data"]
89
91
  else
90
92
  logger.error("Invalid data received while creating channel #{_channel}")
@@ -92,23 +94,20 @@ module Backchat
92
94
  end
93
95
  end
94
96
 
95
- #
96
97
  # Delete a channel
97
98
  # @param *name*
98
99
  # @param *force* true|false if channel should be deleted even if being used in a stream
99
- #
100
+ # @return true|false
100
101
  def destroy_channel(name, force = false)
101
102
  channel.destroy(name, force)
102
103
  end
103
104
 
104
105
  # Streams management
105
106
 
106
-
107
107
  #
108
108
  # Retrieves a specific stream
109
109
  # @param *name* (optional) stream name. If undefined, all the user streams are retrieved
110
110
  # @return stream data hash
111
- #
112
111
  def find_stream(name = nil)
113
112
  streams = stream.find(name)
114
113
 
@@ -125,28 +124,30 @@ module Backchat
125
124
  # @param *name* unique stream identifier
126
125
  # @param *description* (optional)
127
126
  # @param *filters* (optional) array of filters
128
- #
129
127
  def create_stream(name, description = nil, filters = [])
130
128
  description.nil? and description = "Stream created using backchat-client gem"
131
- _stream = stream.create(name, description, filters)
132
-
133
- if _stream.respond_to?("has_key?") and _stream.has_key?("data")
134
- _stream["data"]
135
- else
136
- logger.error("Invalid data received while creating stream: #{_stream}")
137
- raise "No data received from Backchat while creating stream"
129
+ begin
130
+ _stream = stream.create(name, description, filters)
131
+ if _stream.respond_to?("has_key?") and _stream.has_key?("data")
132
+ _stream["data"]
133
+ else
134
+ logger.error("Invalid data received while creating stream: #{_stream}")
135
+ raise "No data received from Backchat while creating stream"
136
+ end
137
+ rescue BackchatClient::Error::ClientError => ex
138
+ logger.error("There was an error creating the stream: #{ex.errors}")
139
+ nil
138
140
  end
139
141
  end
140
142
 
141
143
  #
142
144
  # This method updates the stream, assigning the new channels array to it.
143
145
  # In order to simplify, all the channels received will be automatically enabled.
144
- # @param stream_slug stream name
145
- # @param channels array of channels to be included in the stream
146
- # @param reset the channels are added (false) to the current channels or remove (true) the previous ones
147
- # @param filter valid Lucene syntax to filter the channels
148
- # @return true if the stream was successfully updated
149
- # @raise exception if stream is not found
146
+ # @param *stream_slug* stream name
147
+ # @param *channels* array of channels to be included in the stream
148
+ # @param *reset* the channels are added (false) to the current channels or remove (true) the previous ones
149
+ # @param *filter* valid Lucene syntax to filter the channels
150
+ # @return *boolean* true if the stream was successfully updated, false in case of failure
150
151
  #
151
152
  def set_channels(stream_slug, channels = [], reset = false, filter = nil)
152
153
  st = stream.find(stream_slug) or raise "stream does not exist"
@@ -167,8 +168,16 @@ module Backchat
167
168
  st["channel_filters"] |= channels
168
169
  end
169
170
 
170
- stream.update(stream_slug, st)
171
- true
171
+ begin
172
+ logger.debug("Updating stream channels to #{st}")
173
+ stream.update(stream_slug, st)
174
+ true
175
+ rescue Exception => ex
176
+ logger.error("Error while updating stream channels : #{ex.message}")
177
+ logger.error(ex)
178
+ false
179
+ end
180
+
172
181
  end
173
182
 
174
183
  #
@@ -181,8 +190,7 @@ module Backchat
181
190
  #
182
191
  # Helper that generates the channel url using Addressable help
183
192
  #
184
- def generate_channel_url(type, id, filter = nil)
185
- channel_uri = "#{type}://#{id}"
193
+ def generate_channel_url(channel_uri, filter = nil)
186
194
  if filter
187
195
  channel_uri += "?q=#{filter}"
188
196
  end
@@ -14,46 +14,45 @@ module BackchatClient
14
14
  # http uri to handle channels
15
15
  URI_PATH = "channels"
16
16
 
17
- #
18
17
  # @param *api_key* application identifier
19
18
  # @param *endpoint* Backchat endpoint
20
- #
21
19
  def initialize(api_key, endpoint)
22
20
  @api_key = api_key
23
21
  @endpoint = endpoint
24
22
  end
25
23
 
26
- #
27
24
  # This method POST a request to create a new channel on behalf of the
28
25
  # authenticated application
29
26
  # @param uri valid Backchat URI, i.e. twitter://username
30
27
  # @return response body
31
- #
32
28
  def create(uri)
33
- ActiveSupport::JSON.decode(post("index.json", {:channel => uri}))
29
+ begin
30
+ data = post("index.json", {:channel => uri})
31
+ ActiveSupport::JSON.decode(data)
32
+ rescue Error::ClientError => ex
33
+ logger.error ex.errors
34
+ raise ex
35
+ end
34
36
  end
35
37
 
36
- #
37
38
  # This method sends a GET request to retrieve all the user channels
38
39
  # There is no way to retrieve a specific channel
39
40
  # @return response body
40
- #
41
41
  def find
42
42
  ActiveSupport::JSON.decode(get("index.json"))
43
43
  end
44
44
 
45
- #
46
45
  # Delete an application channel
47
46
  # @param *name* valid channel URI
48
47
  # @param *force* delete even if it is being used by a stream
49
48
  # @return true|false
50
- #
51
49
  def destroy(name, force = false)
52
50
  begin
53
51
  ActiveSupport::JSON.decode(delete("", {:channel => name, :force => force}))
54
- return true
55
- rescue RestClient::ResourceNotFound
56
- return false
52
+ true
53
+ rescue Error::ClientError => ex
54
+ logger.error ex.errors
55
+ false
57
56
  end
58
57
  end
59
58
 
@@ -0,0 +1,4 @@
1
+ require 'backchat_client/error/general_error'
2
+ require 'backchat_client/error/client_error'
3
+ require 'backchat_client/error/server_error'
4
+
@@ -0,0 +1,14 @@
1
+ module BackchatClient::Error
2
+
3
+ class ClientError < GeneralError
4
+
5
+ end
6
+
7
+ class ConflictError < ClientError
8
+
9
+ end
10
+
11
+ class UnprocessableError < ClientError
12
+ end
13
+
14
+ end
@@ -0,0 +1,27 @@
1
+ module BackchatClient::Error
2
+
3
+ class GeneralError < StandardError
4
+ attr_reader :response
5
+
6
+ def initialize(response = nil)
7
+ @response = response
8
+ end
9
+
10
+ def to_s
11
+ response
12
+ end
13
+
14
+ def errors
15
+ @errors||=(
16
+ errors = nil
17
+ if !response.nil? and response.respond_to?(:body)
18
+ data = ActiveSupport::JSON.decode(response.body)
19
+ data.has_key?("errors") and errors = data["errors"].flatten
20
+ end
21
+ errors
22
+ )
23
+
24
+ end
25
+ end
26
+
27
+ end
@@ -0,0 +1,9 @@
1
+ module BackchatClient
2
+ module Error
3
+
4
+ class ServerError < GeneralError
5
+
6
+ end
7
+ end
8
+
9
+ end
@@ -1,4 +1,5 @@
1
1
  require 'rest_client'
2
+ require 'cgi'
2
3
  require 'backchat_client/backchat_logger'
3
4
 
4
5
  module BackchatClient
@@ -14,15 +15,61 @@ module BackchatClient
14
15
  module HttpClient
15
16
 
16
17
  include BackchatClient::BackchatLogger
18
+
19
+ # This method is used to handle the HTTP response
20
+ # *response* HTTP response
21
+ # *request* HTTP request
22
+ # *result*
23
+ def response_handler(response, request, result, &block)
24
+ case response.code
25
+ when 200..207
26
+ # If there is a :location header, return it
27
+ response.return!(request, result, &block)
28
+ when 301..307
29
+ response.follow_redirection(request, result, &block)
30
+ when 400
31
+ logger.warn("Bad request")
32
+ logger.warn("#{result.code} => #{result.message}")
33
+ raise BackchatClient::Error::ClientError.new(result)
34
+ when 401
35
+ logger.warn("Error while accessing Backchat. Authentication failed")
36
+ logger.warn("#{result.code} => #{result.message}")
37
+ raise BackchatClient::Error::ClientError.new(result)
38
+ when 404
39
+ logger.warn("Error while accessing Backchat. Resource not found")
40
+ logger.warn("#{result.code} => #{result.message}")
41
+ raise BackchatClient::Error::ClientError.new(result)
42
+ when 409
43
+ logger.warn("Error while accessing Backchat. Conflict")
44
+ logger.warn("#{result.code} => #{result.message}")
45
+ raise BackchatClient::Error::ConflictError.new(result)
46
+ when 422
47
+ logger.warn("Error while accessing Backchat. Unprocessable entity")
48
+ logger.warn("#{result.code} => #{result.message}")
49
+ raise BackchatClient::Error::UnprocessableError.new(result)
50
+ when 402..408,410..421,423..499
51
+ logger.warn("Error while accessing Backchat")
52
+ logger.warn("#{result.code} => #{result.message}")
53
+ response.return!(request, result, &block)
54
+ else
55
+ logger.warn("Error while accessing Backchat")
56
+ logger.warn("#{result.code} => #{result.message}")
57
+ response.return!(request, result, &block)
58
+ end
59
+ end
17
60
 
18
61
  # HTTP GET
19
62
  def get(path, params = {}, headers = {})
20
63
  headers.merge!({:Authorization => "Backchat #{@api_key}", :Accept => "application/json"})
21
64
 
22
65
  uri = set_path(path)
23
- uri = "#{uri}?".concat(params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&"))
66
+ uri = "#{uri}?".concat(params.collect { |k, v| "#{k}=#{CGI::escape(v.to_s)}" }.join("&"))
24
67
  debug("get request to uri #{uri}")
25
- RestClient.get(uri, headers)
68
+ RestClient.get(uri, headers) { |response, request, result, &block|
69
+ response_handler(response, request, result, &block)
70
+ }
71
+
72
+
26
73
  end
27
74
 
28
75
  # HTTP POST
@@ -32,7 +79,9 @@ module BackchatClient
32
79
  uri = set_path(path)
33
80
  debug("post request to uri #{uri}")
34
81
  debug("post body: <#{body}>")
35
- RestClient.post("#{uri}", body, headers)
82
+ RestClient.post("#{uri}", body, headers) { |response, request, result, &block|
83
+ response_handler(response, request, result, &block)
84
+ }
36
85
  end
37
86
 
38
87
  # HTTP PUT
@@ -42,25 +91,29 @@ module BackchatClient
42
91
  uri = set_path(path)
43
92
  debug("put request to uri #{uri}")
44
93
  debug("put body: <#{body}>")
45
- RestClient.put("#{uri}", body, headers)
94
+ RestClient.put("#{uri}", body, headers) { |response, request, result, &block|
95
+ response_handler(response, request, result, &block)
96
+ }
46
97
  end
47
98
 
48
99
  # HTTP DELETE
49
100
  def delete(path, params = {}, headers = {})
50
101
  headers.merge!({:Authorization => "Backchat #{@api_key}", :accept => :json})
51
102
  uri = set_path(path)
52
- uri = "#{uri}?".concat(params.collect { |k, v| "#{k}=#{v.to_s}" }.join("&"))
103
+ uri = "#{uri}?".concat(params.collect { |k, v| "#{k}=#{CGI::escape(v.to_s)}" }.join("&"))
53
104
  debug("delete request to uri #{uri}")
54
- RestClient.delete(uri, headers)
105
+ RestClient.delete(uri, headers) { |response, request, result, &block|
106
+ response_handler(response, request, result, &block)
107
+ }
55
108
  end
56
109
 
57
110
  private
58
111
 
59
112
  def set_path(path)
60
113
  if uri_path.nil?
61
- "#{@endpoint}/#{path}"
114
+ "#{@endpoint}/#{CGI::escape(path)}"
62
115
  else
63
- "#{@endpoint}/#{uri_path}/#{path}"
116
+ "#{@endpoint}/#{uri_path}/#{CGI::escape(path)}"
64
117
  end
65
118
  end
66
119
 
@@ -14,42 +14,40 @@ module BackchatClient
14
14
  # http uri to handle streams
15
15
  URI_PATH = "streams"
16
16
 
17
- #
18
17
  # @param *api_key* application identifier
19
18
  # @param *endpoint* Backchat endpoint
20
- #
21
19
  def initialize(api_key, endpoint)
22
20
  @api_key = api_key
23
21
  @endpoint = endpoint
24
22
  end
25
23
 
26
- #
27
24
  # This method POST a request to create a new stream on behalf of the
28
25
  # authenticated application
29
26
  # @param name
30
27
  # @param description
31
28
  # @param filters one or more valid channel URIs
32
29
  # @param filter_enabled one or more boolean values enabling|disabling the filters
33
- # @return response body
34
- #
30
+ # @return decoded response body
35
31
  def create(name, description, filters = [])
36
- ActiveSupport::JSON.decode(post("", {:name => name, :description => description, :channel_filters => filters}))
32
+ begin
33
+ data = post("", {:name => name, :description => description, :channel_filters => filters})
34
+ ActiveSupport::JSON.decode(data)
35
+ rescue Error::ClientError => ex
36
+ logger.error ActiveSupport::JSON.decode(ex.response.body)
37
+ raise ex
38
+ end
37
39
  end
38
40
 
39
- #
40
41
  # Retrieves a stream
41
42
  # @param *name* get a stream
42
43
  # @return stream data or nil if not found
43
- #
44
44
  def find(name)
45
- if name
46
- begin
47
- ActiveSupport::JSON.decode(get(name))
48
- rescue RestClient::ResourceNotFound
49
- return nil
50
- end
51
- else
52
- ActiveSupport::JSON.decode(get("index.json"))
45
+ name.nil? and name = "index.json"
46
+ begin
47
+ ActiveSupport::JSON.decode(get(name))
48
+ rescue Error::ClientError => ex
49
+ logger.error(ex.errors)
50
+ return nil
53
51
  end
54
52
  end
55
53
 
@@ -57,17 +55,16 @@ module BackchatClient
57
55
  ActiveSupport::JSON.decode(put(slug, params))
58
56
  end
59
57
 
60
- #
61
58
  # Delete a defined stream
62
59
  # @param *name* valid stream name
63
- # @param true|false if deleted or not
64
- #
60
+ # @return true|false if deleted or not
65
61
  def destroy(name)
66
62
  begin
67
- ActiveSupport::JSON.decode(delete(name))
68
- return true
69
- rescue RestClient::ResourceNotFound
70
- return false
63
+ delete(name)
64
+ true
65
+ rescue Error::ClientError => ex
66
+ logger.error ex.errors
67
+ false
71
68
  end
72
69
  end
73
70
 
@@ -44,7 +44,7 @@ module BackchatClient
44
44
  nil
45
45
  end
46
46
  end
47
- rescue RestClient::Unauthorized => ex
47
+ rescue BackchatClient::Error::ClientError => ex
48
48
  error "Invalid api_key #{@api_key}"
49
49
  nil # Invalid token
50
50
  rescue Exception => ex
@@ -1,3 +1,3 @@
1
1
  module BackchatClient
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -1,12 +1,9 @@
1
1
  require 'backchat-client'
2
+ require 'spec_helper'
2
3
  require 'webmock/rspec'
3
4
 
4
5
  describe Backchat::Client do
5
6
 
6
- before(:each) do
7
- Backchat::Client.log_level = Logger::DEBUG
8
- end
9
-
10
7
  describe "it initializes correctly the client" do
11
8
 
12
9
  it "gets the api key and the endpoint" do
@@ -23,22 +20,20 @@ describe Backchat::Client do
23
20
 
24
21
  describe "valid? method" do
25
22
  it "returns true with a valid token" do
26
- api_key = "valid-api-key"
27
23
  stub_request(:get, "https://api.backchat.io/1/index.json?").
28
- with(:headers => {'Accept'=>'application/json', 'Authorization' => "Backchat #{api_key}"}).
29
- to_return(:status => 200, :body => "{'data':{'channels':[],'email':'user@backchat.com','_id':'user_id','api_key':'#{api_key}','last_name':'lastname','first_name':'name','plan':'https://api.backchat.io/1/plans/free','streams':[],'login':'user'},'errors':[]}", :headers => {})
24
+ with(:headers => {'Accept'=>'application/json', 'Authorization' => "Backchat #{API_KEY}"}).
25
+ to_return(:status => 200, :body => "{'data':{'channels':[],'email':'user@backchat.com','_id':'user_id','api_key':'#{API_KEY}','last_name':'lastname','first_name':'name','plan':'https://api.backchat.io/1/plans/free','streams':[],'login':'user'},'errors':[]}", :headers => {})
30
26
 
31
- bc = Backchat::Client.new(api_key)
27
+ bc = Backchat::Client.new(API_KEY)
32
28
  bc.valid?.should eql(true)
33
29
  end
34
30
 
35
31
  it "returns false with a valid token" do
36
- api_key = "invalid-api-key"
37
32
  stub_request(:get, "https://api.backchat.io/1/index.json?").
38
- with(:headers => {'Accept'=>'application/json', 'Authorization' => "Backchat #{api_key}"}).
33
+ with(:headers => {'Accept'=>'application/json', 'Authorization' => "Backchat #{INVALID_API_KEY}"}).
39
34
  to_return(:status => 401, :body => "{'data':null,'errors':[['','Unauthorized']]}", :headers => {})
40
35
 
41
- bc = Backchat::Client.new(api_key)
36
+ bc = Backchat::Client.new(INVALID_API_KEY)
42
37
  bc.valid?.should eql(false)
43
38
  end
44
39
  end
data/spec/channel_spec.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'backchat-client'
2
+ require 'spec_helper'
2
3
  require 'webmock/rspec'
3
4
 
4
5
  describe BackchatClient::Channel do
5
- API_KEY = "valid-api-key"
6
6
 
7
7
  describe "when working with find method" do
8
8
 
@@ -31,7 +31,7 @@ describe BackchatClient::Channel do
31
31
 
32
32
  bc = Backchat::Client.new(API_KEY)
33
33
 
34
- channel = bc.create_channel("twitter", "juandebravo")
34
+ channel = bc.create_channel("twitter://juandebravo")
35
35
  channel.should be_a(Hash)
36
36
  channel.should have_key("uri")
37
37
  channel["uri"].should eql("twitter://juandebravo/#timeline")
@@ -0,0 +1,4 @@
1
+
2
+ Backchat::Client.logger = Logger.new("/dev/null")
3
+ API_KEY = "valid-api-key"
4
+ INVALID_API_KEY = "invalid-api-key"
data/spec/streams_spec.rb CHANGED
@@ -1,12 +1,8 @@
1
1
  require 'backchat-client'
2
+ require 'spec_helper'
2
3
  require 'webmock/rspec'
3
4
 
4
5
  describe BackchatClient::Stream do
5
- API_KEY = "valid_api_key"
6
-
7
- before(:each) do
8
- Backchat::Client.log_level = Logger::DEBUG
9
- end
10
6
 
11
7
  describe "when working with find method" do
12
8
 
data/spec/user_spec.rb CHANGED
@@ -1,16 +1,12 @@
1
1
  require 'backchat-client'
2
+ require 'spec_helper'
2
3
  require 'webmock/rspec'
3
4
 
4
5
  describe BackchatClient::User do
5
6
 
6
- before(:each) do
7
- Backchat::Client.log_level = Logger::DEBUG
8
- end
9
-
10
7
  describe "when working with find method" do
11
8
 
12
9
  it "gets all the user defined streams when no name is provided" do
13
- API_KEY = "valid_api_key"
14
10
  bc = Backchat::Client.new(API_KEY)
15
11
  stub_request(:get, "https://api.backchat.io/1/index.json?").
16
12
  with(:headers => {'Accept'=>'application/json', 'Authorization' => "Backchat #{API_KEY}"}).
@@ -23,7 +19,6 @@ describe BackchatClient::User do
23
19
  end
24
20
 
25
21
  it "deletes a user when valid api_key provided" do
26
- API_KEY = "valid_api_key"
27
22
  bc = Backchat::Client.new(API_KEY)
28
23
  stub_request(:delete, "https://api.backchat.io/1/?").
29
24
  with(:headers => {'Accept'=>'application/json', 'Authorization' => "Backchat #{API_KEY}"}).
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: backchat-client
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: "0.1"
5
+ version: "0.2"
6
6
  platform: ruby
7
7
  authors:
8
8
  - This gem provides an easy way to access Backchat provisioning API
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-26 00:00:00 +02:00
13
+ date: 2011-08-24 00:00:00 +03:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -92,17 +92,23 @@ files:
92
92
  - examples/delete_stream.rb
93
93
  - examples/delete_user.rb
94
94
  - examples/get_channels.rb
95
+ - examples/setup.rb
95
96
  - examples/stream.rb
96
97
  - examples/user.rb
97
98
  - lib/backchat-client.rb
98
99
  - lib/backchat_client/backchat_logger.rb
99
100
  - lib/backchat_client/channel.rb
101
+ - lib/backchat_client/error.rb
102
+ - lib/backchat_client/error/client_error.rb
103
+ - lib/backchat_client/error/general_error.rb
104
+ - lib/backchat_client/error/server_error.rb
100
105
  - lib/backchat_client/http_client.rb
101
106
  - lib/backchat_client/stream.rb
102
107
  - lib/backchat_client/user.rb
103
108
  - lib/backchat_client/version.rb
104
109
  - spec/backchat-client_spec.rb
105
110
  - spec/channel_spec.rb
111
+ - spec/spec_helper.rb
106
112
  - spec/streams_spec.rb
107
113
  - spec/user_spec.rb
108
114
  has_rdoc: true
@@ -136,5 +142,6 @@ summary: Gem to access Backchat provisioning API
136
142
  test_files:
137
143
  - spec/backchat-client_spec.rb
138
144
  - spec/channel_spec.rb
145
+ - spec/spec_helper.rb
139
146
  - spec/streams_spec.rb
140
147
  - spec/user_spec.rb