cloud_connect 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ .yardoc
5
+ .DS_Store
6
+ doc/*
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ 'lib/cloud_connect.rb' 'lib/cloud_connect/**/*.rb'
2
+ --no-private
3
+ --protected
4
+ --tag format:"Supported formats"
5
+ --tag authenticated:"Requires Authentication"
6
+ --tag rate_limited:"Rate Limited"
7
+ --markup markdown
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :gemcutter
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cloud_connect (0.0.1)
5
+ faraday (~> 0.5.0)
6
+ faraday_middleware (~> 0.1.6)
7
+ hashie (~> 0.4.0)
8
+ json
9
+ multi_json (~> 0.0.0)
10
+
11
+ GEM
12
+ remote: http://rubygems.org/
13
+ specs:
14
+ addressable (2.2.2)
15
+ bluecloth (2.0.11)
16
+ diff-lcs (1.1.2)
17
+ faraday (0.5.1)
18
+ addressable (~> 2.2.2)
19
+ multipart-post (~> 1.0.1)
20
+ rack (~> 1.2.1)
21
+ faraday_middleware (0.1.7)
22
+ faraday (~> 0.5.1)
23
+ hashie (0.4.0)
24
+ json (1.5.1)
25
+ multi_json (0.0.4)
26
+ multipart-post (1.0.1)
27
+ rack (1.2.1)
28
+ rake (0.8.7)
29
+ rspec (2.0.1)
30
+ rspec-core (~> 2.0.1)
31
+ rspec-expectations (~> 2.0.1)
32
+ rspec-mocks (~> 2.0.1)
33
+ rspec-core (2.0.1)
34
+ rspec-expectations (2.0.1)
35
+ diff-lcs (>= 1.1.2)
36
+ rspec-mocks (2.0.1)
37
+ rspec-core (~> 2.0.1)
38
+ rspec-expectations (~> 2.0.1)
39
+ yard (0.6.1)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ bluecloth (>= 2.0)
46
+ bundler (>= 1.0.0)
47
+ cloud_connect!
48
+ faraday (~> 0.5.0)
49
+ faraday_middleware (~> 0.1.6)
50
+ hashie (~> 0.4.0)
51
+ json
52
+ multi_json (~> 0.0.0)
53
+ rake (~> 0.8)
54
+ rspec
55
+ yard (~> 0.6)
data/HISTORY.md ADDED
@@ -0,0 +1,12 @@
1
+ 0.0.1 - February 16, 2011
2
+ -------------------------
3
+ * Initial release
4
+
5
+ Thanks to the following people for making this possible
6
+ -------------------------------------------------------
7
+ - John Nunemaker ([@jnunemaker](http://twitter.com/#!/jnunemaker))
8
+ for the twitter that inspired this one.
9
+ - Rick Olson ([@technoweenie](https://github.com/technoweenie))
10
+ for the faraday gem
11
+ - Wynn Netherland ([@pengwynn](http://github.com/pengwynn))
12
+ for the faraday_middleware gem
data/README.md ADDED
@@ -0,0 +1,97 @@
1
+ The Coud Connect Ruby Gem
2
+ ==========================
3
+
4
+ A Ruby wrapper for the [Cloud Connect API](http://develop.g8teway.com).
5
+
6
+ Installation
7
+ ------------
8
+ gem install cloud_connect
9
+
10
+ Usage
11
+ -----
12
+
13
+ require 'rubygems'
14
+ require 'cloud_connect'
15
+
16
+ ### Instantiate a client
17
+
18
+ cloud_connect = CloudConnect::Client.new(:username => 'user', :password => 'password', :account => 'test', :env => 'sandbox')
19
+
20
+ ### Or configure once
21
+
22
+ CloudConnect.configure do |config|
23
+ config.username = 'user'
24
+ config.password = 'password'
25
+ config.account = 'test'
26
+ config.env = 'sandbox'
27
+ end
28
+ cloud_connect = CloudConnect.client.new
29
+
30
+ ### Login
31
+
32
+ cloud_connect.login
33
+
34
+ ### Examples
35
+
36
+ cloud_connect.units
37
+ => [<#Hashie::Mash id=2 lat=nil lng=nil time=nil>, <#Hashie::Mash id=3 lat=4884481 lng=226392 time="2009-07-08T10:23:13Z">]
38
+
39
+ Details for the current user
40
+ cloud_connect.user
41
+
42
+ Details for another user
43
+
44
+ cloud_connect.user('other_user')
45
+ cloud_connect.user(1)
46
+
47
+ Send a message to a unit
48
+
49
+ cloud_connect.send_message(1, 11, "Hello World!")
50
+
51
+ Get a unit's last known position
52
+
53
+ unit = cloud_connect.unit(3)
54
+ puts "#{unit.location.join(', ')} @ #{unit.time}"
55
+
56
+ We recommand you install the [yajl-ruby](http://github.com/brianmario/yajl-ruby) Gem
57
+ for improved performance over the default pure ruby JSON library.
58
+
59
+ Contributing
60
+ ------------
61
+ In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project.
62
+
63
+ Here are some ways *you* can contribute:
64
+
65
+ * by using alpha, beta, and prerelease versions
66
+ * by reporting bugs
67
+ * by suggesting new features
68
+ * by writing or editing documentation
69
+ * by writing specifications
70
+ * by writing code (**no patch is too small**: fix typos, add comments, clean up inconsistent whitespace)
71
+ * by refactoring code
72
+ * by closing [issues](http://github.com/mobiledevices/cloud_connect/issues)
73
+ * by reviewing patches
74
+
75
+ All contributors will be added to the [HISTORY](https://github.com/mobiledevices/cloud_connect/blob/master/HISTORY.md)
76
+ file and will receive the respect and gratitude of the community.
77
+
78
+ Submitting an Issue
79
+ -------------------
80
+ We use the [GitHub issue tracker](http://github.com/mobiledevices/cloud_connect/issues) to track bugs and
81
+ features. Before submitting a bug report or feature request, check to make sure it hasn't already
82
+ been submitted. You can indicate support for an existing issuse by voting it up. When submitting a
83
+ bug report, please include a [Gist](http://gist.github.com/) that includes a stack trace and any
84
+ details that may be necessary to reproduce the bug, including your gem version, Ruby version, and
85
+ operating system. Ideally, a bug report should include a pull request with failing specs.
86
+
87
+ Submitting a Pull Request
88
+ -------------------------
89
+ 1. Fork the project.
90
+ 2. Create a topic branch.
91
+ 3. Implement your feature or bug fix.
92
+ 4. Add documentation for your feature or bug fix.
93
+ 5. Run <tt>bundle exec rake doc:yard</tt>. If your changes are not 100% documented, go back to step 4.
94
+ 6. Add specs for your feature or bug fix.
95
+ 7. Run <tt>bundle exec rake spec</tt>. If your changes are not 100% covered, go back to step 6.
96
+ 8. Commit and push your changes.
97
+ 9. Submit a pull request. Please do not include changes to the gemspec, version, or history file. (If you want to create your own version for some reason, please do so in a separate commit.)
data/Rakefile ADDED
@@ -0,0 +1,47 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ namespace :doc do
5
+ require 'yard'
6
+ YARD::Rake::YardocTask.new do |task|
7
+ task.files = ['lib/cloud_connect.rb', 'lib/cloud_connect/**/*.rb']
8
+ task.options = [
9
+ '--protected',
10
+ '--output-dir', 'doc/yard',
11
+ '--tag', 'format:Supported formats',
12
+ '--tag', 'authenticated:Requires Authentication',
13
+ '--tag', 'rate_limited:Rate Limited',
14
+ '--markup', 'markdown',
15
+ ]
16
+ end
17
+ end
18
+
19
+ task :irb do
20
+ $:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
21
+ require 'irb'
22
+ require 'bundler/setup'
23
+ require 'cloud_connect'
24
+ module IRB # :nodoc:
25
+ def self.start_session(binding)
26
+ unless @__initialized
27
+ args = ARGV
28
+ ARGV.replace(ARGV.dup)
29
+ IRB.setup(nil)
30
+ ARGV.replace(args)
31
+ @__initialized = true
32
+ end
33
+
34
+ ws = WorkSpace.new(binding)
35
+ irb = Irb.new(ws)
36
+
37
+ @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
38
+ @CONF[:MAIN_CONTEXT] = irb.context
39
+
40
+ catch(:IRB_EXIT) do
41
+ irb.eval_input
42
+ end
43
+ end
44
+
45
+ IRB.start_session(binding)
46
+ end
47
+ end
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/cloud_connect/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "cloud_connect"
6
+ s.version = CloudConnect::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Jean-Paul Bonnetouche"]
9
+ s.email = ["jean-paul.bonnetouche@mobile-devices.fr"]
10
+ s.homepage = "http://rubygems.org/gems/cloud_connect"
11
+ s.summary = "Wrapper for Cloud Connect"
12
+ s.description = "Ruby Wrapper for the Mobile Devices Cloud Connect API"
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+
16
+ s.add_runtime_dependency "json"
17
+ s.add_runtime_dependency "multi_json", "~> 0.0.0"
18
+ s.add_runtime_dependency "faraday", "~> 0.5.0"
19
+ s.add_runtime_dependency "faraday_middleware", "~> 0.1.6"
20
+ s.add_runtime_dependency "hashie", "~> 0.4.0"
21
+
22
+ s.add_development_dependency "bundler", ">= 1.0.0"
23
+ s.add_development_dependency "bluecloth", ">= 2.0"
24
+ s.add_development_dependency "rake", "~> 0.8"
25
+ s.add_development_dependency "yard", "~> 0.6"
26
+ s.add_development_dependency "rspec"
27
+
28
+ s.files = `git ls-files`.split("\n")
29
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
30
+ s.require_path = 'lib'
31
+ end
@@ -0,0 +1,22 @@
1
+ module CloudConnect
2
+ module Channels
3
+
4
+ # Retrieve list of channels
5
+ # WARNING: This method uses calls not officially supported by Mobile Devices.
6
+ #
7
+ # @return [Array of Hashie::Mash] Channels
8
+ def channels(reload = false)
9
+ return @channels if @channels && !reload
10
+ page = 1
11
+ limit = 100
12
+ channels = []
13
+ while (slice = connection.get(connection.build_url("channels", :per_page => limit, :page => page)).body).size > 0
14
+ page += 1
15
+ channels += slice.map!{|hash| hash.values.first} if slice.size > 0
16
+ slice.size < limit ? break : sleep(1)
17
+ end
18
+ @channels = channels.sort_by(&:channel)
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module CloudConnect
2
+ module Fields
3
+
4
+ # Retrieve a list of fields.
5
+ # WARNING: This method uses calls not officially supported by Mobile Devices.
6
+ #
7
+ # @return [[Hashie::Mash]] Array of Fields
8
+ def fields(reload = false)
9
+ return @fields if @fields && !reload
10
+ page = 1
11
+ limit = 100
12
+ fields = []
13
+ while (slice = connection.get(connection.build_url("fields", :per_page => limit, :page => page)).body).size > 0
14
+ page += 1
15
+ fields += slice.map!{|hash| hash.values.first} if slice.size > 0
16
+ slice.size < limit ? break : sleep(1)
17
+ end
18
+ @fields = fields.sort_by(&:id)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,49 @@
1
+ module CloudConnect
2
+ module Messages
3
+
4
+ # Returns all messages that match parameters provided in +opts+ list
5
+ # (if +opts+ is provided)
6
+ #
7
+ # @param [Hash] opts the options to filter the messages.
8
+ # @option opts [String] :ret ('id, time') Select attributes to fetch
9
+ # @option opts [String] :channelids List of channel ids
10
+ # @option opts [String] :messageids List of message ids
11
+ # @option opts [String] :unitids List of unit ids
12
+ # @option opts [String] :userids List of user ids
13
+ # @option opts [String] :status Message status
14
+ # (0 (new), 1 (sent), 2 (received), 3 (failed))
15
+ # @option opts [String] :timedout (false) Include timed-out messages
16
+ # @option opts [String] :from Minimum date
17
+ # @option opts [String] :to Maximum date
18
+ # @option opts [String] :direction Message direction
19
+ # (unittouser or usertounit)
20
+ # @option opts [String] :replyto List of reference message ids
21
+ # @option opts [Integer] :id_min Minimum ID
22
+ # @option opts [Integer] :id_max Maximum ID
23
+ # @option opts [Integer] :limit (25) Number of elements to fetch
24
+ # @return [Array<Hashie::Mash>] Messages
25
+ # @see http://develop.g8teway.com/p/messages.html#listing_messages
26
+ def messages(opts = {})
27
+ messages = connection.get(connection.build_url("messages", opts)).body
28
+ messages.map!{|hash| hash.values.first}
29
+ end
30
+
31
+ # Send a message to a specific device
32
+ #
33
+ # @param [Integer] unit
34
+ # @param [Integer] channel
35
+ # @param [String] content
36
+ # @param [Hash] opts
37
+ # @return [Hashie::Mash] The message
38
+ # @see http://develop.g8teway.com/p/messages.html#sending_a_new_message
39
+ def send_message(unit, channel, content, opts = {})
40
+ # TODO: rename #message_create ?
41
+ opts.merge! :channelid => channel, :unitid => unit, :content => content
42
+ response = connection.post do |req|
43
+ req.url 'messages'
44
+ req.body = {:message => opts}
45
+ end
46
+ response.body.values.first
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,22 @@
1
+ module CloudConnect
2
+ module Trackings
3
+
4
+ # Retrieve list of tracking data
5
+ #
6
+ # @see http://develop.g8teway.com/p/tracking_records.html#listing_tracking_records
7
+ # @param [Hash] opts the options to filter the tracking data.
8
+ # @options opts [String] :ret Select attributes to fetch
9
+ # @options opts [String] :userids List of unit ids
10
+ # @options opts [String] :fieldids List of field ids
11
+ # @options opts [Integer] :from Minimum ID
12
+ # @options opts [Integer] :to Maximum ID
13
+ # @options opts [Integer] :id_min Minimum ID
14
+ # @options opts [Integer] :id_max Maximum ID
15
+ # @options opts [Integer] :limit Number of elements to fetch (default 25
16
+ # @return [Array of Hashie::Mash] Tracking data
17
+ def trackings(opts = {})
18
+ trackings = connection.get(connection.build_url("tracking_records", opts)).body
19
+ trackings.map!{|hash| hash.values.first}
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,77 @@
1
+ module CloudConnect
2
+ module Units
3
+
4
+ # Returns all units that match parameters provided in +opts+ list
5
+ # (if +opts+ is provided)
6
+ #
7
+ # @param [Hash] opts the options to filter the units.
8
+ # @option opts [String] :ret ('id, lat, long, time') Select attributes to fetch
9
+ # @option opts [String] :unitids List of unit ids
10
+ # @option opts [String] :fieldids List of field ids
11
+ # @option opts [String] :unknow Allow unknown position
12
+ # @option opts [Integer] :id_min Minimum ID
13
+ # @option opts [Integer] :id_max Maximum ID
14
+ # @option opts [Integer] :limit Number of elements to fetch (default 25
15
+ # @return [Array<Hashie::Mash>] Units
16
+ # @see http://develop.g8teway.com/p/units.html#listing_units
17
+ def units(opts = {})
18
+ units = connection.get(connection.build_url("units", opts)).body
19
+ units.map!{|hash| hash.values.first}
20
+ units.each{|u| u.extend UnitMethods; u._cloud_connect = self;}
21
+ end
22
+
23
+ # Search for a specific unit knowing it's modid
24
+ #
25
+ # @param [String] modids the comma separated list modids.
26
+ # Partial modids can be provided using the '*' caracter
27
+ # @example
28
+ # find_unit("*3216*")
29
+ # @return [Array<Hashie::Mash>] Unit
30
+ # @see http://develop.g8teway.com/p/units.html#searching_units
31
+ def find_units(modids)
32
+ # TODO: Rename unit_search?
33
+ units = connection.get(connection.build_url("units/search", :modids => modids)).body
34
+ units.map!{|hash| hash.values.first}
35
+ units.each{|u| u.extend UnitMethods; u._cloud_connect = self;}
36
+ end
37
+
38
+ # Return information about a specific unit
39
+ #
40
+ # @param [String] unit_id Unit ID
41
+ # @param [Hash] opts the options to filter the units.
42
+ # @option opts [String] :ret ('id, lat, long, time') Select attributes to fetch
43
+ # @option opts [String] :fieldids List of field ids
44
+ # @option opts [String] :unknow Allow unknown position
45
+ # @return [Hashie::Mash] Unit info
46
+ def unit(unit_id=nil, opts = {})
47
+ units = connection.get(connection.build_url("units", opts.merge(:unitids => unit_id))).body
48
+ units.map!{|hash| hash.values.first}
49
+ units.each{|u| u.extend UnitMethods; u._cloud_connect = self;}
50
+ units.first
51
+ end
52
+
53
+ module UnitMethods
54
+ # @private
55
+ attr_accessor :_cloud_connect
56
+
57
+ # Return the last known location of a specific unit
58
+ #
59
+ # @return [Integer lat, Integer long] Latitude, Longitude
60
+ def location
61
+ [lat.to_f / 100_000, lng.to_f / 100_000]
62
+ end
63
+
64
+ # Send a message to the unit
65
+ #
66
+ # @param [Integer] channel
67
+ # @param [String] content
68
+ # @param [Hash] opts
69
+ # @return [Hashie::Mash] The message
70
+ # @see http://develop.g8teway.com/p/messages.html#sending_a_new_message
71
+ def send_message(channel, content, opts = {})
72
+ raise "Unknown unit id, try providing :ret => 'id' when fetching units from the API." unless id && id > 0
73
+ _cloud_connect.send_message(id, channel, content, opts)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,36 @@
1
+ module CloudConnect
2
+ module Users
3
+
4
+ # Retrieve list of users
5
+ #
6
+ # @see http://develop.g8teway.com/p/users.html#searching_and_listing_users
7
+ # @param [Hash] opts the options to filter the units.
8
+ # @options opts [String] :ret Select attributes to fetch
9
+ # @options opts [String] :userids List of unit ids
10
+ # @options opts [String] :logins List of field ids
11
+ # @options opts [Integer] :id_min Minimum ID
12
+ # @options opts [Integer] :id_max Maximum ID
13
+ # @options opts [Integer] :limit Number of elements to fetch (default 25
14
+ # @return [Array of Hashie::Mash] Users
15
+ def users(opts = {})
16
+ opts.default! :ret => %w(id login email).join(',')
17
+ users = connection.get(connection.build_url('users', opts)).body
18
+ users.map!{|hash| hash.values.first}
19
+ end
20
+
21
+ # Return information about a specific user
22
+ #
23
+ # @param [String] user_id Unit ID
24
+ # @return [Hashie::Mash] User info
25
+ def user(user_id = nil)
26
+ user_id = username if user_id.nil? || user_id == ""
27
+ if user_id.to_i.to_s == user_id.to_s
28
+ users = connection.get(connection.build_url('users', :userids => user_id)).body
29
+ else
30
+ users = connection.get(connection.build_url('users', :logins => user_id)).body
31
+ end
32
+ users.map!{|hash| hash.values.first}
33
+ users.first
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,62 @@
1
+ module CloudConnect
2
+ class Client
3
+ attr_reader :username, :password, :account, :env, :cookie
4
+ attr_writer :cookie
5
+
6
+ def initialize(options={})
7
+ @username = options[:username] || CloudConnect.username
8
+ @password = options[:password] || CloudConnect.password
9
+ @account = options[:account] || CloudConnect.account
10
+ @env = options[:env] || CloudConnect.env || "prod"
11
+ end
12
+
13
+ # Raw HTTP connection, either Faraday::Connection
14
+ #
15
+ # @return [Faraday::Connection]
16
+ def connection
17
+ params = {}
18
+ params[:access_token] = @access_token if @access_token
19
+ @connection ||= Faraday::Connection.new(:url => api_url, :params => params, :headers => default_headers) do |builder|
20
+ builder.use Faraday::Request::CookieAuth, self
21
+ builder.adapter Faraday.default_adapter
22
+ builder.use Faraday::Response::RaiseHttp5xx
23
+ builder.use Faraday::Response::ParseJson
24
+ builder.use Faraday::Response::RaiseHttp4xx
25
+ builder.use Faraday::Response::Mashify
26
+ #builder.response :yajl # Faraday::Request::Yajl
27
+ end
28
+ end
29
+
30
+ # Provides the URL for accessing the API
31
+ #
32
+ # @return [String]
33
+ def api_url
34
+ if env == "preprod"
35
+ "http://srv/api/v2"
36
+ else
37
+ "http://#{env}.g8teway.com/api/v2"
38
+ end
39
+ end
40
+
41
+
42
+ def login
43
+ req = connection.post('sessions', {:login => username, :password => password, :client => account})
44
+ end
45
+
46
+ private
47
+ # @private
48
+ def default_headers
49
+ headers = {
50
+ :accept => 'application/json',
51
+ :user_agent => 'CloudClient Ruby gem'
52
+ }
53
+ end
54
+
55
+ include Units
56
+ include Users
57
+ include Channels
58
+ include Messages
59
+ include Fields
60
+ include Trackings
61
+ end
62
+ end
@@ -0,0 +1,12 @@
1
+ module CloudConnect
2
+ class Error < StandardError; end
3
+ class BadGateway < Error; end
4
+ class BadRequest < Error; end
5
+ class Forbidden < Error; end
6
+ class InternalServerError < Error; end
7
+ class NotAcceptable < Error; end
8
+ class NotFound < Error; end
9
+ class ServiceUnavailable < Error; end
10
+ class UnprocessableEntity < Error; end
11
+ class Unauthorized < Error; end
12
+ end
@@ -0,0 +1,3 @@
1
+ module CloudConnect
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'forwardable'
4
+
5
+ begin
6
+ require 'yajl'
7
+ MultiJson.engine = :yajl
8
+ rescue LoadError
9
+ require 'json'
10
+ MultiJson.engine = :json_gem
11
+ end
12
+
13
+ module CloudConnect
14
+ class << self
15
+ attr_accessor :username
16
+ attr_accessor :password
17
+ attr_accessor :account
18
+ attr_accessor :env
19
+
20
+ def configure
21
+ yield self
22
+ true
23
+ end
24
+
25
+ require 'ext/object'
26
+ require 'ext/module'
27
+ require 'ext/hash'
28
+
29
+ require 'faraday/cookie_auth'
30
+ require 'faraday/raise_http_4xx'
31
+ require 'faraday/raise_http_5xx'
32
+
33
+ require 'cloud_connect/error'
34
+ require 'cloud_connect/client/units'
35
+ require 'cloud_connect/client/users'
36
+ require 'cloud_connect/client/messages'
37
+ require 'cloud_connect/client/trackings'
38
+ require 'cloud_connect/client/channels'
39
+ require 'cloud_connect/client/fields'
40
+ require 'cloud_connect/client'
41
+ end
42
+ end
data/lib/ext/hash.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Hash
2
+ def default!(other_hash)
3
+ merge!( other_hash ){|k,o,n| o }
4
+ end
5
+ end
data/lib/ext/module.rb ADDED
@@ -0,0 +1,59 @@
1
+ # From active_support
2
+ #
3
+ # http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/module/delegation.rb
4
+ # http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/module/remove_method.rb
5
+ class Module
6
+
7
+ # Used by delegate
8
+ # @see http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/module/remove_method.rb
9
+ def remove_possible_method(method)
10
+ remove_method(method)
11
+ rescue NameError
12
+ end
13
+
14
+ # Provides a delegate class method to easily expose contained objects' methods
15
+ # as your own. Pass one or more methods (specified as symbols or strings)
16
+ # and the name of the target object via the <tt>:to</tt> option (also a symbol
17
+ # or string). At least one method and the <tt>:to</tt> option are required.
18
+ # @see http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/module/delegation.rb
19
+ def delegate(*methods)
20
+ options = methods.pop
21
+ unless options.is_a?(Hash) && to = options[:to]
22
+ raise ArgumentError, "Delegation needs a target. Supply an options hash with a :to key as the last argument (e.g. delegate :hello, :to => :greeter)."
23
+ end
24
+
25
+ if options[:prefix] == true && options[:to].to_s =~ /^[^a-z_]/
26
+ raise ArgumentError, "Can only automatically set the delegation prefix when delegating to a method."
27
+ end
28
+
29
+ prefix = options[:prefix] && "#{options[:prefix] == true ? to : options[:prefix]}_" || ''
30
+
31
+ file, line = caller.first.split(':', 2)
32
+ line = line.to_i
33
+
34
+ methods.each do |method|
35
+ on_nil =
36
+ if options[:allow_nil]
37
+ 'return'
38
+ else
39
+ %(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
40
+ end
41
+
42
+ module_eval(<<-EOS, file, line - 5)
43
+ if instance_methods(false).map(&:to_s).include?("#{prefix}#{method}")
44
+ remove_possible_method("#{prefix}#{method}")
45
+ end
46
+
47
+ def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)
48
+ #{to}.__send__(#{method.inspect}, *args, &block) # client.__send__(:name, *args, &block)
49
+ rescue NoMethodError # rescue NoMethodError
50
+ if #{to}.nil? # if client.nil?
51
+ #{on_nil} # return # depends on :allow_nil
52
+ else # else
53
+ raise # raise
54
+ end # end
55
+ end # end
56
+ EOS
57
+ end
58
+ end
59
+ end
data/lib/ext/object.rb ADDED
@@ -0,0 +1,57 @@
1
+ class Object
2
+ # mainly from active_support on http://github.com/rails/rails/
3
+
4
+ # Returns +value+ after yielding +value+ to the block. This simplifies the
5
+ # process of constructing an object, performing work on the object, and then
6
+ # returning the object from a method. It is a Ruby-ized realization of the K
7
+ # combinator, courtesy of Mikael Brockman.
8
+ #
9
+ # ==== Examples
10
+ #
11
+ # # Without returning
12
+ # def foo
13
+ # values = []
14
+ # values << "bar"
15
+ # values << "baz"
16
+ # return values
17
+ # end
18
+ #
19
+ # foo # => ['bar', 'baz']
20
+ #
21
+ # # returning with a local variable
22
+ # def foo
23
+ # returning values = [] do
24
+ # values << 'bar'
25
+ # values << 'baz'
26
+ # end
27
+ # end
28
+ #
29
+ # foo # => ['bar', 'baz']
30
+ #
31
+ # # returning with a block argument
32
+ # def foo
33
+ # returning [] do |values|
34
+ # values << 'bar'
35
+ # values << 'baz'
36
+ # end
37
+ # end
38
+ #
39
+ # foo # => ['bar', 'baz']
40
+ def returning(value)
41
+ yield(value)
42
+ value
43
+ end
44
+
45
+ # Tries to send the method only if object responds to it. Return +nil+ otherwise.
46
+ #
47
+ # ==== Example :
48
+ #
49
+ # # Without try
50
+ # @person ? @person.name : nil
51
+ #
52
+ # With try
53
+ # @person.try(:name)
54
+ def try(method, *args, &block)
55
+ send(method, *args, &block) if respond_to?(method, true)
56
+ end
57
+ end
@@ -0,0 +1,21 @@
1
+ module Faraday
2
+ class Request::CookieAuth < Faraday::Middleware
3
+ delegate :cookie, :cookie=, :to => :@client, :allow_nil => true
4
+
5
+ def call(env)
6
+ env[:request_headers]['Cookie'] = cookie if cookie
7
+
8
+ env[:response].on_complete do |finished_env|
9
+ if finished_env[:response_headers]['set-cookie']
10
+ self.cookie = finished_env[:response_headers]['set-cookie'].split('; ')[0]
11
+ end
12
+ end
13
+
14
+ @app.call(env)
15
+ end
16
+
17
+ def initialize(app, client = nil)
18
+ @app, @client = app, client
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,42 @@
1
+ module Faraday
2
+ class Response::RaiseHttp4xx < Response::Middleware
3
+ def self.register_on_complete(env)
4
+ env[:response].on_complete do |response|
5
+ puts "STATUS: <<<<<<<<< #{response[:status]} >>>>>>>>>>>>"
6
+ case response[:status].to_i
7
+ when 400
8
+ raise CloudConnect::BadRequest, error_message(response)
9
+ when 401
10
+ raise CloudConnect::Unauthorized, error_message(response)
11
+ when 403
12
+ raise CloudConnect::Forbidden, error_message(response)
13
+ when 404
14
+ raise CloudConnect::NotFound, error_message(response)
15
+ when 406
16
+ raise CloudConnect::NotAcceptable, error_message(response)
17
+ when 422
18
+ raise CloudConnect::UnprocessableEntity, error_message(response)
19
+ #when 420
20
+ # raise CloudConnect::EnhanceYourCalm.new error_message(response), response[:response_headers]
21
+ end
22
+ end
23
+ end
24
+
25
+ def initialize(app)
26
+ super
27
+ @parser = nil
28
+ end
29
+
30
+ private
31
+
32
+ def self.error_message(response)
33
+ if response[:body] && response[:body].is_a?(Hash) && response[:body]['error']
34
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}: #{(response[:body]['error'])}"
35
+ elsif response[:body] && response[:body].is_a?(Array)
36
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}: #{(response[:body].join(', '))}"
37
+ else
38
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}: #{(response[:body])}"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,27 @@
1
+ module Faraday
2
+ class Response::RaiseHttp5xx < Response::Middleware
3
+ def self.register_on_complete(env)
4
+ env[:response].on_complete do |response|
5
+ case response[:status].to_i
6
+ when 500
7
+ raise CloudConnect::InternalServerError, error_message(response, "Something is technically wrong.")
8
+ when 502
9
+ raise CloudConnect::BadGateway, error_message(response, "CloudConnect is down or being upgraded.")
10
+ when 503
11
+ raise CloudConnect::ServiceUnavailable, error_message(response, "(__-){ CloudConnect is over capacity.")
12
+ end
13
+ end
14
+ end
15
+
16
+ def initialize(app)
17
+ super
18
+ @parser = nil
19
+ end
20
+
21
+ private
22
+
23
+ def self.error_message(response, body=nil)
24
+ "#{response[:method].to_s.upcase} #{response[:url].to_s}: #{response[:response_headers]['status']}:#{(' ' + body) if body} Check http://status.g8teway.com/ for updates on the status of the CloudConnect service."
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,245 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloud_connect
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jean-Paul Bonnetouche
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-16 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: multi_json
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 31
44
+ segments:
45
+ - 0
46
+ - 0
47
+ - 0
48
+ version: 0.0.0
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: faraday
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 11
60
+ segments:
61
+ - 0
62
+ - 5
63
+ - 0
64
+ version: 0.5.0
65
+ type: :runtime
66
+ version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: faraday_middleware
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ hash: 23
76
+ segments:
77
+ - 0
78
+ - 1
79
+ - 6
80
+ version: 0.1.6
81
+ type: :runtime
82
+ version_requirements: *id004
83
+ - !ruby/object:Gem::Dependency
84
+ name: hashie
85
+ prerelease: false
86
+ requirement: &id005 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ hash: 15
92
+ segments:
93
+ - 0
94
+ - 4
95
+ - 0
96
+ version: 0.4.0
97
+ type: :runtime
98
+ version_requirements: *id005
99
+ - !ruby/object:Gem::Dependency
100
+ name: bundler
101
+ prerelease: false
102
+ requirement: &id006 !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ hash: 23
108
+ segments:
109
+ - 1
110
+ - 0
111
+ - 0
112
+ version: 1.0.0
113
+ type: :development
114
+ version_requirements: *id006
115
+ - !ruby/object:Gem::Dependency
116
+ name: bluecloth
117
+ prerelease: false
118
+ requirement: &id007 !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ hash: 3
124
+ segments:
125
+ - 2
126
+ - 0
127
+ version: "2.0"
128
+ type: :development
129
+ version_requirements: *id007
130
+ - !ruby/object:Gem::Dependency
131
+ name: rake
132
+ prerelease: false
133
+ requirement: &id008 !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ hash: 27
139
+ segments:
140
+ - 0
141
+ - 8
142
+ version: "0.8"
143
+ type: :development
144
+ version_requirements: *id008
145
+ - !ruby/object:Gem::Dependency
146
+ name: yard
147
+ prerelease: false
148
+ requirement: &id009 !ruby/object:Gem::Requirement
149
+ none: false
150
+ requirements:
151
+ - - ~>
152
+ - !ruby/object:Gem::Version
153
+ hash: 7
154
+ segments:
155
+ - 0
156
+ - 6
157
+ version: "0.6"
158
+ type: :development
159
+ version_requirements: *id009
160
+ - !ruby/object:Gem::Dependency
161
+ name: rspec
162
+ prerelease: false
163
+ requirement: &id010 !ruby/object:Gem::Requirement
164
+ none: false
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ hash: 3
169
+ segments:
170
+ - 0
171
+ version: "0"
172
+ type: :development
173
+ version_requirements: *id010
174
+ description: Ruby Wrapper for the Mobile Devices Cloud Connect API
175
+ email:
176
+ - jean-paul.bonnetouche@mobile-devices.fr
177
+ executables: []
178
+
179
+ extensions: []
180
+
181
+ extra_rdoc_files: []
182
+
183
+ files:
184
+ - .gitignore
185
+ - .yardopts
186
+ - Gemfile
187
+ - Gemfile.lock
188
+ - HISTORY.md
189
+ - README.md
190
+ - Rakefile
191
+ - cloud_connect.gemspec
192
+ - lib/cloud_connect.rb
193
+ - lib/cloud_connect/client.rb
194
+ - lib/cloud_connect/client/channels.rb
195
+ - lib/cloud_connect/client/fields.rb
196
+ - lib/cloud_connect/client/messages.rb
197
+ - lib/cloud_connect/client/trackings.rb
198
+ - lib/cloud_connect/client/units.rb
199
+ - lib/cloud_connect/client/users.rb
200
+ - lib/cloud_connect/error.rb
201
+ - lib/cloud_connect/version.rb
202
+ - lib/ext/hash.rb
203
+ - lib/ext/module.rb
204
+ - lib/ext/object.rb
205
+ - lib/faraday/cookie_auth.rb
206
+ - lib/faraday/raise_http_4xx.rb
207
+ - lib/faraday/raise_http_5xx.rb
208
+ has_rdoc: true
209
+ homepage: http://rubygems.org/gems/cloud_connect
210
+ licenses: []
211
+
212
+ post_install_message:
213
+ rdoc_options: []
214
+
215
+ require_paths:
216
+ - lib
217
+ required_ruby_version: !ruby/object:Gem::Requirement
218
+ none: false
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ hash: 3
223
+ segments:
224
+ - 0
225
+ version: "0"
226
+ required_rubygems_version: !ruby/object:Gem::Requirement
227
+ none: false
228
+ requirements:
229
+ - - ">="
230
+ - !ruby/object:Gem::Version
231
+ hash: 23
232
+ segments:
233
+ - 1
234
+ - 3
235
+ - 6
236
+ version: 1.3.6
237
+ requirements: []
238
+
239
+ rubyforge_project:
240
+ rubygems_version: 1.3.7
241
+ signing_key:
242
+ specification_version: 3
243
+ summary: Wrapper for Cloud Connect
244
+ test_files: []
245
+