cloud_connect 2.0.2 → 3.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -3
- data/Gemfile +2 -1
- data/Gemfile.lock +42 -31
- data/README.md +28 -83
- data/Rakefile +7 -35
- data/cloud_connect.gemspec +27 -28
- data/lib/cloud_connect/authentication.rb +15 -0
- data/lib/cloud_connect/client/assets.rb +102 -0
- data/lib/cloud_connect/client/channels.rb +83 -16
- data/lib/cloud_connect/client/configurations.rb +54 -0
- data/lib/cloud_connect/client/custom_methods.rb +20 -0
- data/lib/cloud_connect/client/fields.rb +88 -15
- data/lib/cloud_connect/client/messages.rb +60 -48
- data/lib/cloud_connect/client/tracks.rb +46 -0
- data/lib/cloud_connect/client.rb +39 -53
- data/lib/cloud_connect/configuration.rb +60 -0
- data/lib/cloud_connect/connection.rb +49 -0
- data/lib/cloud_connect/error.rb +27 -5
- data/lib/cloud_connect/notification/notification.rb +78 -0
- data/lib/cloud_connect/notification.rb +43 -0
- data/lib/cloud_connect/request.rb +70 -0
- data/lib/cloud_connect/version.rb +1 -1
- data/lib/cloud_connect.rb +18 -35
- data/lib/faraday/response/raise_cloud_connect_error.rb +44 -0
- metadata +93 -45
- data/.yardopts +0 -7
- data/HISTORY.md +0 -33
- data/lib/cloud_connect/client/trackings.rb +0 -22
- data/lib/cloud_connect/client/units.rb +0 -77
- data/lib/cloud_connect/client/users.rb +0 -36
- data/lib/ext/hash.rb +0 -5
- data/lib/ext/module.rb +0 -59
- data/lib/ext/object.rb +0 -57
- data/lib/faraday/cookie_auth.rb +0 -21
- data/lib/faraday/raise_http_4xx.rb +0 -41
- data/lib/faraday/raise_http_5xx.rb +0 -27
@@ -1,21 +1,94 @@
|
|
1
1
|
module CloudConnect
|
2
|
-
|
2
|
+
class Client
|
3
|
+
module Fields
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
5
|
+
# Get a field
|
6
|
+
#
|
7
|
+
# @param name [String] Name of the field
|
8
|
+
# @return [Field] The field you requested, if it exists
|
9
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0006-fields
|
10
|
+
# @example Get field 0123456789012345
|
11
|
+
# @client = CloudConnect::Client.new(:account => 'foo', :token => 'bar')
|
12
|
+
# @client.field("0123456789012345")
|
13
|
+
def field(name, options={})
|
14
|
+
enhance( get("fields/#{name}", options), with: FieldMethods )
|
15
|
+
end
|
16
|
+
|
17
|
+
# Search fields
|
18
|
+
#
|
19
|
+
# @param search_term [String] The term to search for
|
20
|
+
# @return [Array] A list of fields matching the search term
|
21
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0006-fields
|
22
|
+
# @example Search for 'TEMPERATURE' in the fields
|
23
|
+
# @client = CloudConnect::Client.new(:account => 'foor', :token => 'bar')
|
24
|
+
# @client.search_fields
|
25
|
+
def search_fields(search_term, options={})
|
26
|
+
enhance( get("fields?q=#{search_term}", options), with: FieldMethods )
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get fields
|
30
|
+
#
|
31
|
+
# @return [Array] A list of all fields
|
32
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0006-fields
|
33
|
+
# @example List all fields
|
34
|
+
# @client = CloudConnect::Client.new(:account => 'foo', :token => 'bar')
|
35
|
+
# @client.fields
|
36
|
+
def fields(options={})
|
37
|
+
enhance( get("fields", options), with: FieldMethods )
|
38
|
+
end
|
39
|
+
alias :list_fields :fields
|
40
|
+
|
41
|
+
# Create a field
|
42
|
+
#
|
43
|
+
# @param name [String] Name of the field
|
44
|
+
# @param options [Hash] A customizable set of options
|
45
|
+
# @options options [Integer] :field ID of the field
|
46
|
+
# @options options [String] :field_type Type of the field: <tt>unknown</tt>, <tt>boolean</tt>, <tt>integer</tt>, <tt>decimal</tt>, <tt>string</tt>, or <tt>base64</tt>
|
47
|
+
# @options options [String] :size Size of the field
|
48
|
+
# @options options [String] :ack
|
49
|
+
# @options options [String] :description Description of the field
|
50
|
+
# @return [Field] Your newly created field
|
51
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0006-fields#create_a_new_field
|
52
|
+
# @example Create a new Field
|
53
|
+
# @client = CloudConnect::CloudConnect.new(:account => 'foo', :token => 'bar')
|
54
|
+
# @client.create_field("CUSTOM_SENSOR_TEMP", :field_type => 'integer', :size => 1, :description => "Custom sensor temp in Celsius")
|
55
|
+
def create_field(name, options={})
|
56
|
+
enhance( post("fields", options.merge({:name => name})), with: FieldMethods )
|
57
|
+
end
|
58
|
+
|
59
|
+
# Update an field
|
60
|
+
#
|
61
|
+
# @param name [String] Name of the field
|
62
|
+
# @param options [Hash] A customizable set of options
|
63
|
+
# @options options [Integer] :field ID of the field
|
64
|
+
# @options options [String] :field_type Type of the field: <tt>unknown</tt>, <tt>boolean</tt>, <tt>integer</tt>, <tt>decimal</tt>, <tt>string</tt>, or <tt>base64</tt>
|
65
|
+
# @options options [String] :size Size of the field
|
66
|
+
# @options options [String] :ack
|
67
|
+
# @options options [String] :description Description of the field
|
68
|
+
# @return [Field] Your newly updated field
|
69
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0006-fields#update_a_field
|
70
|
+
# @example Update a Field
|
71
|
+
# @client = CloudConnect::CloudConnect.new(:account => 'foo', :token => 'bar')
|
72
|
+
# @client.update_field("CUSTOM_SENSOR_TEMP", :field_type => 'integer', :size => 1, :description => "Custom sensor temp in Celsius")
|
73
|
+
def update_field(name, options={})
|
74
|
+
enhance( put("/fields/#{name}", options.merge({:name => name})), with: FieldMethods )
|
75
|
+
end
|
76
|
+
|
77
|
+
# Delete a single field
|
78
|
+
#
|
79
|
+
# @param name [String] Name fo the field
|
80
|
+
# @return [Response] A response object with status
|
81
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0006-fields#delete_a_field
|
82
|
+
# @example Delete the CUSTOM_SENSOR_TEMP field
|
83
|
+
# @client = CloudConnect::CloudConnect.new(:account => 'foo', :token => 'bar')
|
84
|
+
# @client.delete_field("CUSTOM_SENSOR_TEMP")
|
85
|
+
def delete_field(name, options={})
|
86
|
+
delete("fields/#{name}", options, true)
|
87
|
+
end
|
88
|
+
|
89
|
+
module FieldMethods
|
90
|
+
extend CustomMethods
|
17
91
|
end
|
18
|
-
@fields = fields.sort_by(&:id)
|
19
92
|
end
|
20
93
|
end
|
21
94
|
end
|
@@ -1,56 +1,68 @@
|
|
1
1
|
module CloudConnect
|
2
|
-
|
2
|
+
class Client
|
3
|
+
module Messages
|
4
|
+
# Get a message
|
5
|
+
#
|
6
|
+
# @param id [String] ID of the message
|
7
|
+
# @return [Message] The message you requested, if it exists
|
8
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0005-messages
|
9
|
+
# @example Get message 380342888036303061
|
10
|
+
# @client = CloudConnect::Client.new(:account => 'foo', :token => 'bar')
|
11
|
+
# @client.message("380342888036303061")
|
12
|
+
def message(id, options={})
|
13
|
+
enhance( get("messages/#{id}", options), with: MessageMethods )
|
14
|
+
end
|
3
15
|
|
4
|
-
|
16
|
+
# Search messages
|
17
|
+
#
|
18
|
+
# @param search_term [String] The term to search for
|
19
|
+
# @return [Array] A list of messages matching the search term
|
20
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0005-messages
|
21
|
+
# @example Search for '123' in the messages
|
22
|
+
# @client = CloudConnect::Client.new(:account => 'foor', :token => 'bar')
|
23
|
+
# @client.search_messages
|
24
|
+
def search_messages(search_term, options={})
|
25
|
+
enhance( get("messages?q=#{search_term}", options), with: MessageMethods )
|
26
|
+
end
|
5
27
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
# Get messages from an asset
|
29
|
+
#
|
30
|
+
# @param imei [String] IMEI of the asset
|
31
|
+
# @return [Array] The messages you requested, if it exists
|
32
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0005-messages
|
33
|
+
# @example Get messages from the asset 351732050019192
|
34
|
+
# @client = CloudConnect::Client.new(:account => 'foo', :token => 'bar')
|
35
|
+
# @client.asset_messages("351732050019192")
|
36
|
+
def asset_messages(imei, options={})
|
37
|
+
enhance( get("assets/#{imei}/messages", options), with: MessageMethods )
|
38
|
+
end
|
39
|
+
|
40
|
+
# Get all messages
|
41
|
+
#
|
42
|
+
# @return [Array] A list of all messages
|
43
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0005-messages
|
44
|
+
# @example List all messages
|
45
|
+
# @client = CloudConnect::Client.new(:account => 'foo', :token => 'bar')
|
46
|
+
# @client.messages
|
47
|
+
def messages(options={})
|
48
|
+
enhance( get("messages", options), with: MessageMethods )
|
49
|
+
end
|
50
|
+
alias :list_messages :messages
|
51
|
+
|
52
|
+
# Send a message to the unit
|
53
|
+
#
|
54
|
+
# @param imei [String] IMEI of the asset
|
55
|
+
# @param channel [String] Channel for the message
|
56
|
+
# @param payload [String] Payload of the message to send
|
57
|
+
# @return [Message] The message
|
58
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0005-messages#push_a_message_to_an_asset
|
59
|
+
def send_message(imei, channel, payload, options={})
|
60
|
+
enhance( post("messages", options.merge({recipient: imei, asset: imei, channel: channel, b64_payload: Base64.encode64(payload)})), with: MessageMethods )
|
61
|
+
end
|
32
62
|
|
33
|
-
|
34
|
-
|
35
|
-
# @param [Integer] unit
|
36
|
-
# @param [Integer] channel
|
37
|
-
# @param [String] content
|
38
|
-
# @param [Hash] opts
|
39
|
-
# @return [Hashie::Mash] The message
|
40
|
-
# @see http://develop.g8teway.com/p/messages.html#sending_a_new_message
|
41
|
-
def send_message(unit, channel, content, opts = {})
|
42
|
-
# TODO: rename #message_create ?
|
43
|
-
opts.merge! :channelid => channel, :unitid => unit, :content => content
|
44
|
-
response = connection.post do |req|
|
45
|
-
if version == "v1.5"
|
46
|
-
req.url 'ws?messagesend'
|
47
|
-
req.body = "unit=#{unit}&channel=#{channel}&content=#{URI::encode(content)}"
|
48
|
-
else
|
49
|
-
req.url 'messages'
|
50
|
-
req.body = {:message => opts}
|
51
|
-
end
|
63
|
+
module MessageMethods
|
64
|
+
extend CustomMethods
|
52
65
|
end
|
53
|
-
response.body.values.first
|
54
66
|
end
|
55
67
|
end
|
56
68
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module CloudConnect
|
2
|
+
class Client
|
3
|
+
module Tracks
|
4
|
+
|
5
|
+
# Get a track
|
6
|
+
#
|
7
|
+
# @param id [String] ID of the track
|
8
|
+
# @return [Track] The track you requested, if it existsle ||
|
9
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0004-tracks
|
10
|
+
# @example Get track 0123456789012345
|
11
|
+
# @client = CloudConnect::Client.new(:account => 'foo', :token => 'bar')
|
12
|
+
# @client.track("0123456789012345")
|
13
|
+
def track(id, options={})
|
14
|
+
enhance( get("tracks/#{id}", options), with: TrackMethods )
|
15
|
+
end
|
16
|
+
|
17
|
+
# Search tracks
|
18
|
+
#
|
19
|
+
# @param search_term [String] The term to search for
|
20
|
+
# @return [Array] A list of tracks matching the search term
|
21
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0004-tracks
|
22
|
+
# @example Search for '123' in the tracks
|
23
|
+
# @client = CloudConnect::Client.new(:account => 'foor', :token => 'bar')
|
24
|
+
# @client.search_tracks
|
25
|
+
def search_tracks(search_term, options={})
|
26
|
+
enhance( get("tracks?q=#{search_term}", options), with: TrackMethods )
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get tracks
|
30
|
+
#
|
31
|
+
# @return [Array] A list of all tracks
|
32
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0004-messages
|
33
|
+
# @example List all tracks
|
34
|
+
# @client = CloudConnect::Client.new(:account => 'foo', :token => 'bar')
|
35
|
+
# @client.tracks
|
36
|
+
def tracks(options={})
|
37
|
+
enhance( get("tracks", options), with: TrackMethods )
|
38
|
+
end
|
39
|
+
|
40
|
+
alias :list_tracks :tracks
|
41
|
+
module TrackMethods
|
42
|
+
extend CustomMethods
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/cloud_connect/client.rb
CHANGED
@@ -1,66 +1,52 @@
|
|
1
|
+
require 'cloud_connect/authentication'
|
2
|
+
require 'cloud_connect/connection'
|
3
|
+
require 'cloud_connect/request'
|
4
|
+
|
5
|
+
require 'cloud_connect/client/custom_methods'
|
6
|
+
require 'cloud_connect/client/assets'
|
7
|
+
require 'cloud_connect/client/configurations'
|
8
|
+
require 'cloud_connect/client/fields'
|
9
|
+
require 'cloud_connect/client/channels'
|
10
|
+
require 'cloud_connect/client/tracks'
|
11
|
+
require 'cloud_connect/client/messages'
|
12
|
+
|
1
13
|
module CloudConnect
|
2
14
|
class Client
|
3
|
-
|
4
|
-
attr_writer :cookie
|
15
|
+
attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
|
5
16
|
|
6
17
|
def initialize(options={})
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
@env = options[:env] || CloudConnect.env || "prod"
|
11
|
-
@version = options[:version] || "v2"
|
12
|
-
end
|
13
|
-
|
14
|
-
# Raw HTTP connection, either Faraday::Connection
|
15
|
-
#
|
16
|
-
# @return [Faraday::Connection]
|
17
|
-
def connection
|
18
|
-
params = {}
|
19
|
-
params[:access_token] = @access_token if @access_token
|
20
|
-
@connection ||= Faraday::Connection.new(:url => api_url, :params => params, :headers => default_headers) do |builder|
|
21
|
-
builder.use Faraday::Request::CookieAuth, self
|
22
|
-
builder.adapter Faraday.default_adapter
|
23
|
-
builder.use Faraday::Response::RaiseHttp5xx
|
24
|
-
builder.use Faraday::Response::ParseJson
|
25
|
-
builder.use Faraday::Response::RaiseHttp4xx
|
26
|
-
builder.use Faraday::Response::Mashify
|
27
|
-
#builder.response :yajl # Faraday::Request::Yajl
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
# Provides the URL for accessing the API
|
32
|
-
#
|
33
|
-
# @return [String]
|
34
|
-
def api_url
|
35
|
-
if env == "preprod"
|
36
|
-
"http://srv/api/v2"
|
37
|
-
else
|
38
|
-
"http://#{env}.g8teway.com/api/#{version}"
|
18
|
+
options = CloudConnect.options.merge(options)
|
19
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
20
|
+
send("#{key}=", options[key])
|
39
21
|
end
|
40
22
|
end
|
41
23
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
24
|
+
def enhance(object, with_opts)
|
25
|
+
# @private
|
26
|
+
# Add custom methods to provided object(s).
|
27
|
+
# This method provides syntaxic suggar upon <tt>Module.apply_to(object)</tt>,
|
28
|
+
# allowing you to write <tt>enhance object, with: Module</tt>
|
29
|
+
#
|
30
|
+
# @param object [Object] An Object or Array of Objects to enhance.
|
31
|
+
# @param with_opts [Hash] Customizable set of options.
|
32
|
+
# @options with_opts [Module] :with Module to include.
|
33
|
+
# @return [Object] Enhanced initial object.
|
34
|
+
# @example Enhance an instance of Hashie::Mash with methods defined in the AssetMethods module
|
35
|
+
# enhance( Hashie::Mash.new, :with => AssetMethods)
|
36
|
+
object.tap do |obj|
|
37
|
+
with_opts[:with].apply_to(obj, client: self)
|
47
38
|
end
|
48
39
|
end
|
49
40
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
headers = {
|
54
|
-
:accept => 'application/json',
|
55
|
-
:user_agent => 'CloudClient Ruby gem'
|
56
|
-
}
|
57
|
-
end
|
41
|
+
include CloudConnect::Authentication
|
42
|
+
include CloudConnect::Connection
|
43
|
+
include CloudConnect::Request
|
58
44
|
|
59
|
-
include
|
60
|
-
include
|
61
|
-
include
|
62
|
-
include
|
63
|
-
include
|
64
|
-
include
|
45
|
+
include CloudConnect::Client::Assets
|
46
|
+
include CloudConnect::Client::Configurations
|
47
|
+
include CloudConnect::Client::Fields
|
48
|
+
include CloudConnect::Client::Channels
|
49
|
+
include CloudConnect::Client::Tracks
|
50
|
+
include CloudConnect::Client::Messages
|
65
51
|
end
|
66
52
|
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'cloud_connect/version'
|
3
|
+
|
4
|
+
module CloudConnect
|
5
|
+
module Configuration
|
6
|
+
VALID_OPTIONS_KEYS = [
|
7
|
+
:adapter,
|
8
|
+
:faraday_config_block,
|
9
|
+
:api_version,
|
10
|
+
:api_url,
|
11
|
+
:api_path,
|
12
|
+
:account,
|
13
|
+
:token,
|
14
|
+
:proxy,
|
15
|
+
:request_host,
|
16
|
+
:user_agent,
|
17
|
+
:auto_traversal,
|
18
|
+
:per_page,
|
19
|
+
].freeze
|
20
|
+
|
21
|
+
API_VERSION = 3
|
22
|
+
|
23
|
+
DEFAULT_ADAPTER = Faraday.default_adapter
|
24
|
+
DEFAULT_API_URL = "cloudconnect.io"
|
25
|
+
API_PATH = "/api/v#{API_VERSION}/"
|
26
|
+
DEFAULT_USER_AGENT = "CloudConnect Ruby Gem #{CloudConnect::VERSION}".freeze
|
27
|
+
DEFAULT_AUTO_TRAVERSAL = false
|
28
|
+
|
29
|
+
attr_accessor(*VALID_OPTIONS_KEYS)
|
30
|
+
|
31
|
+
def self.extended(base)
|
32
|
+
base.reset
|
33
|
+
end
|
34
|
+
|
35
|
+
def configure
|
36
|
+
yield self
|
37
|
+
end
|
38
|
+
|
39
|
+
def options
|
40
|
+
VALID_OPTIONS_KEYS.inject({}){|o,k| o.merge!(k => send(k)) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def faraday_config(&block)
|
44
|
+
@faraday_config_block = block
|
45
|
+
end
|
46
|
+
|
47
|
+
def reset
|
48
|
+
self.adapter = DEFAULT_ADAPTER
|
49
|
+
self.api_version = API_VERSION
|
50
|
+
self.api_url = DEFAULT_API_URL
|
51
|
+
self.api_path = API_PATH
|
52
|
+
self.account = nil
|
53
|
+
self.token = nil
|
54
|
+
self.proxy = nil
|
55
|
+
self.request_host = nil
|
56
|
+
self.user_agent = DEFAULT_USER_AGENT
|
57
|
+
self.auto_traversal = DEFAULT_AUTO_TRAVERSAL
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'faraday_middleware'
|
2
|
+
require 'faraday/response/raise_cloud_connect_error'
|
3
|
+
|
4
|
+
module CloudConnect
|
5
|
+
# @private
|
6
|
+
module Connection
|
7
|
+
private
|
8
|
+
|
9
|
+
def api_endpoint
|
10
|
+
File.join(@api_url, @api_path, "")
|
11
|
+
end
|
12
|
+
|
13
|
+
def url
|
14
|
+
url = "http://#{[account, api_endpoint].join('.')}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def connection(raw=false, force_urlencoded=false)
|
18
|
+
options = {
|
19
|
+
:ssl => { :verify => false },
|
20
|
+
:url => url,
|
21
|
+
}
|
22
|
+
|
23
|
+
options[:proxy] = proxy unless proxy.nil?
|
24
|
+
|
25
|
+
# TODO: Don't build on every request
|
26
|
+
connection = Faraday.new(options) do |builder|
|
27
|
+
if !force_urlencoded
|
28
|
+
builder.request :json
|
29
|
+
else
|
30
|
+
builder.request :url_encoded
|
31
|
+
end
|
32
|
+
|
33
|
+
builder.use Faraday::Response::RaiseCloudConnectError
|
34
|
+
|
35
|
+
unless raw
|
36
|
+
builder.use FaradayMiddleware::Mashify
|
37
|
+
builder.use FaradayMiddleware::ParseJson
|
38
|
+
end
|
39
|
+
|
40
|
+
faraday_config_block.call(builder) if faraday_config_block
|
41
|
+
|
42
|
+
builder.adapter *adapter
|
43
|
+
end
|
44
|
+
|
45
|
+
connection.basic_auth authentication[:token], 'X'
|
46
|
+
connection
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/cloud_connect/error.rb
CHANGED
@@ -1,12 +1,34 @@
|
|
1
1
|
module CloudConnect
|
2
|
+
# Custom error class for rescuing from all CloudConnect errors
|
2
3
|
class Error < StandardError; end
|
3
|
-
|
4
|
+
|
5
|
+
# Raised when CloudConnect returns a 400 HTTP status code
|
4
6
|
class BadRequest < Error; end
|
7
|
+
|
8
|
+
# Raised when CloudConnect returns a 401 HTTP status code
|
9
|
+
class Unauthorized < Error; end
|
10
|
+
|
11
|
+
# Raised when CloudConnect returns a 403 HTTP status code
|
5
12
|
class Forbidden < Error; end
|
6
|
-
|
7
|
-
|
13
|
+
|
14
|
+
# Raised when CloudConnect returns a 404 HTTP status code
|
8
15
|
class NotFound < Error; end
|
9
|
-
|
16
|
+
|
17
|
+
# Raised when CloudConnect returns a 406 HTTP status code
|
18
|
+
class NotAcceptable < Error; end
|
19
|
+
|
20
|
+
# Raised when CloudConnect returns a 422 HTTP status code
|
10
21
|
class UnprocessableEntity < Error; end
|
11
|
-
|
22
|
+
|
23
|
+
# Raised when CloudConnect returns a 500 HTTP status code
|
24
|
+
class InternalServerError < Error; end
|
25
|
+
|
26
|
+
# Raised when CloudConnect returns a 501 HTTP status code
|
27
|
+
class NotImplemented < Error; end
|
28
|
+
|
29
|
+
# Raised when CloudConnect returns a 502 HTTP status code
|
30
|
+
class BadGateway < Error; end
|
31
|
+
|
32
|
+
# Raised when CloudConnect returns a 503 HTTP status code
|
33
|
+
class ServiceUnavailable < Error; end
|
12
34
|
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'base64'
|
3
|
+
require 'faraday_middleware'
|
4
|
+
require 'faraday/response/raise_cloud_connect_error'
|
5
|
+
|
6
|
+
module CloudConnect
|
7
|
+
class Notification
|
8
|
+
module Notification
|
9
|
+
|
10
|
+
# Notification
|
11
|
+
#
|
12
|
+
# @param json [String] JSON of the POST request
|
13
|
+
# @return [Array] The fields from the json, deserialized and decoded
|
14
|
+
# @see http://wordsabout.it/mobile-devices/cloudconnect-user-documentation/cc-0013-notifications
|
15
|
+
# @example Notification
|
16
|
+
# @client = CloudConnect::Client.new(:account => 'foor', :token => 'bar')
|
17
|
+
# @client.notification(json)
|
18
|
+
def notification(json)
|
19
|
+
result = connection(false, false)
|
20
|
+
json = JSON.parse(json)
|
21
|
+
if json.class == [].class
|
22
|
+
mash = []
|
23
|
+
json.each do |obj|
|
24
|
+
mash << Hashie::Mash.new(obj)
|
25
|
+
end
|
26
|
+
else
|
27
|
+
mash = Hashie::Mash.new(json)
|
28
|
+
end
|
29
|
+
return parse_notification(mash)
|
30
|
+
end
|
31
|
+
|
32
|
+
alias :list_notification :notification
|
33
|
+
|
34
|
+
module NotificationMethods
|
35
|
+
extend CustomMethods
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def decode_b64(b64, type)
|
41
|
+
if type == "string"
|
42
|
+
return Base64.decode64(b64)
|
43
|
+
elsif type == "integer"
|
44
|
+
decoded_value = 0
|
45
|
+
bytes = Base64.decode64(b64).bytes
|
46
|
+
for byte in bytes
|
47
|
+
decoded_value <<= 8
|
48
|
+
decoded_value += byte
|
49
|
+
end
|
50
|
+
decoded_value
|
51
|
+
return decoded_value
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def decode_notification(mash)
|
56
|
+
if mash.meta.event == "message"
|
57
|
+
mash.payload.payload = decode_b64(mash.payload.b64_payload, "string")
|
58
|
+
elsif mash.meta.event == "track"
|
59
|
+
mash.payload.fields.each do |attr|
|
60
|
+
attr.last.b64_value = decode_b64(attr.last.b64_value, "integer")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
return mash
|
64
|
+
end
|
65
|
+
|
66
|
+
def parse_notification(mashes)
|
67
|
+
if mashes.class == [].class
|
68
|
+
mashes.each do |mash|
|
69
|
+
mash = parse_notification(mash)
|
70
|
+
end
|
71
|
+
return mashes
|
72
|
+
else
|
73
|
+
return decode_notification(mashes)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'cloud_connect/authentication'
|
2
|
+
require 'cloud_connect/connection'
|
3
|
+
require 'cloud_connect/request'
|
4
|
+
|
5
|
+
require 'cloud_connect/client/custom_methods'
|
6
|
+
require 'cloud_connect/notification/notification'
|
7
|
+
|
8
|
+
module CloudConnect
|
9
|
+
class Notification
|
10
|
+
attr_accessor(*Configuration::VALID_OPTIONS_KEYS)
|
11
|
+
|
12
|
+
def initialize(options={})
|
13
|
+
options = CloudConnect.options.merge(options)
|
14
|
+
Configuration::VALID_OPTIONS_KEYS.each do |key|
|
15
|
+
send("#{key}=", options[key])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def enhance(object, with_opts)
|
20
|
+
# @private
|
21
|
+
# Add custom methods to provided object(s).
|
22
|
+
# This method provides syntaxic suggar upon <tt>Module.apply_to(object)</tt>,
|
23
|
+
# allowing you to write <tt>enhance object, with: Module</tt>
|
24
|
+
#
|
25
|
+
# @param object [Object] An Object or Array of Objects to enhance.
|
26
|
+
# @param with_opts [Hash] Customizable set of options.
|
27
|
+
# @options with_opts [Module] :with Module to include.
|
28
|
+
# @return [Object] Enhanced initial object.
|
29
|
+
# @example Enhance an instance of Hashie::Mash with methods defined in the AssetMethods module
|
30
|
+
# enhance( Hashie::Mash.new, :with => AssetMethods)
|
31
|
+
object.tap do |obj|
|
32
|
+
with_opts[:with].apply_to(obj, client: self)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
include CloudConnect::Authentication
|
37
|
+
include CloudConnect::Connection
|
38
|
+
include CloudConnect::Request
|
39
|
+
|
40
|
+
include CloudConnect::Notification::Notification
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|