jess 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3ba4716ea02efd8b3125dd57ede1c75fafc26de9
4
+ data.tar.gz: 03f2507e70f5ca75b2bfac60e61be5e8d12a1b68
5
+ SHA512:
6
+ metadata.gz: 6a8519484aa74c44671a609ec1e9acab79eeba09d1094b4ed22c988f459c0671287c39eac8ff94ca999cf4708ef638c5f20e157de8c697dc268c3c0d726dabb0
7
+ data.tar.gz: 2e0a4267865b068782beb774541741cd636bc925a46bd31532f3f5d7351d0582b37c60978192eb2b74c77cbbdf394fb83725cd41c68836ca4f1913564fa03c83
@@ -0,0 +1,7 @@
1
+ Describe what you changed and why you changed it, and then review the checklist below.
2
+
3
+ ---
4
+
5
+ - [ ] All tests and RuboCop checks pass (`bundle exec rake`)
6
+ - [ ] Test(s) included
7
+ - [ ] CHANGELOG entry included
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,41 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ DisplayStyleGuide: true
4
+ TargetRubyVersion: 2.1
5
+
6
+ Metrics/AbcSize:
7
+ Exclude:
8
+ - "test/**/*"
9
+
10
+ Metrics/ClassLength:
11
+ Exclude:
12
+ - "test/**/*"
13
+
14
+ Metrics/MethodLength:
15
+ Exclude:
16
+ - "test/**/*"
17
+
18
+ Style/BarePercentLiterals:
19
+ EnforcedStyle: percent_q
20
+
21
+ Style/ClassAndModuleChildren:
22
+ Enabled: false
23
+
24
+ Style/Documentation:
25
+ Exclude:
26
+ - 'test/**/*'
27
+
28
+ Style/DoubleNegation:
29
+ Enabled: false
30
+
31
+ Style/FrozenStringLiteralComment:
32
+ Enabled: false
33
+
34
+ Style/SpaceAroundEqualsInParameterDefault:
35
+ EnforcedStyle: no_space
36
+
37
+ Style/StringLiterals:
38
+ EnforcedStyle: double_quotes
39
+
40
+ Style/TrivialAccessors:
41
+ AllowPredicates: true
@@ -0,0 +1,7 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.1
5
+ - 2.2
6
+ - 2.3.1
7
+ before_install: gem install bundler -v 1.13.1
@@ -0,0 +1,16 @@
1
+ # Jess Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ Jess is in a pre-1.0 state. This means that its APIs and behavior are subject to breaking changes without deprecation notices. Until 1.0, version numbers will follow a [Semver][]-ish `0.y.z` format, where `y` is incremented when new features or breaking changes are introduced, and `z` is incremented for lesser changes or bug fixes.
6
+
7
+ ## [Unreleased][]
8
+
9
+ * Your contribution here!
10
+
11
+ ## 0.1.0 (2016-10-03)
12
+
13
+ * Initial release
14
+
15
+ [Semver]: http://semver.org
16
+ [Unreleased]: https://github.com/mattbrictson/chandler/compare/v0.1.0...HEAD
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at matt@mattbrictson.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,21 @@
1
+ # Contributing to Jess
2
+
3
+ Have a feature idea, bug fix, or refactoring suggestion? Contributions are welcome! Here are some general guidelines.
4
+
5
+ ## Discuss first!
6
+
7
+ 1. Check [Issues][] to see if your contribution has already been discussed and/or implemented.
8
+ 2. If not, open an issue to discuss your contribution. I won't accept all changes and do not want to waste your time.
9
+ 3. Once you have the :thumbsup:, fork the repo, make your changes, and open a PR.
10
+
11
+ ## Before you commit
12
+
13
+ * Did you add tests? New features or changes in behavior should be tested.
14
+ * Do all tests and RuboCop checks pass? Run `bundle exec rake` to be sure.
15
+ * Did you add a CHANGELOG entry? All non-trivial PRs need one.
16
+
17
+ ## Questions?
18
+
19
+ Need help writing an appropriate test? Not sure the best way to attack a problem? Please ask by [posting a GitHub issue][Issues]!
20
+
21
+ [Issues]: https://github.com/mattbrictson/jess/issues
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jess.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Matt Brictson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,125 @@
1
+ # Jess
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/jess.svg)](http://badge.fury.io/rb/jess)
4
+ [![Build Status](https://travis-ci.org/mattbrictson/jess.svg?branch=master)](https://travis-ci.org/mattbrictson/jess)
5
+ [![Code Climate](https://codeclimate.com/github/mattbrictson/jess/badges/gpa.svg)](https://codeclimate.com/github/mattbrictson/jess)
6
+ [![Coverage Status](https://coveralls.io/repos/github/mattbrictson/jess/badge.svg?branch=master)](https://coveralls.io/github/mattbrictson/jess?branch=master)
7
+
8
+ **Jess is an extremely lightweight, read-only client for the JAMF Software Server (JSS) API.**
9
+ This is a young project and is experimental. Currently it only provides access to computer and mobile device records.
10
+
11
+ Jess aims to be:
12
+
13
+ * Fast
14
+ * Easy to use
15
+ * Small (zero dependencies)
16
+ * Well-tested
17
+ * Multi-tenant friendly (connect to many JSS endpoints)
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem "jess"
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ $ bundle
30
+
31
+ Or install it yourself as:
32
+
33
+ $ gem install jess
34
+
35
+ *Note that Jess requires Ruby 2.1.0 or newer.*
36
+
37
+ ## Usage
38
+
39
+ First establish a connection.
40
+
41
+ ```ruby
42
+ require "jess"
43
+
44
+ # Simple
45
+ conn = Jess.connect("https://jsshost", username: "user", password: "secret")
46
+
47
+ # Advanced, with more options available
48
+ http = Jess::HttpClient.new(
49
+ "https://jsshost",
50
+ username: "user",
51
+ password: "secret",
52
+ net_http_options: {
53
+ keep_alive_timeout: 5,
54
+ open_timeout: 5,
55
+ read_timeout: 10,
56
+ verify_mode: OpenSSL::SSL::VERIFY_NONE
57
+ },
58
+ logger: nil # disable default logging
59
+ )
60
+ conn = Jess::Connection.new(http)
61
+ ```
62
+
63
+ Then retrieve a computer:
64
+
65
+ ```ruby
66
+ computer = conn.computers.find(1234)
67
+ computer.id # => 1234
68
+ computer.name # => "Matt's iMac"
69
+ computer.general.ip_address # => "10.0.0.17"
70
+ computer.hardware.model # => "iMac Intel (Retina 5k, 27-Inch, Late 2015)"
71
+ ```
72
+
73
+ Or a mobile device:
74
+
75
+ ```ruby
76
+ phone = conn.mobile_devices.find(5678)
77
+ phone.id # => 5678
78
+ phone.general.serial_number # => "G15ER8WGSC61L"
79
+ ```
80
+
81
+ In general, the objects provided by Jess mirror the structure of the JSON returned by the JSS API. The exception to this rule is `extension_attributes`, which for convenience are transformed into a Hash-like object for easy access:
82
+
83
+ ```ruby
84
+ computer.extension_attributes.key?("My Ext Attr Name") # => true
85
+ computer.extension_attributes["My Ext Attr Name"] # => "value"
86
+ ```
87
+
88
+ If you ever need access to the raw JSON data of any object, use the `_json` method:
89
+
90
+ ```ruby
91
+ computer.extension_attributes._json # => [{ ... }]
92
+ ```
93
+
94
+ Jess does not perform any type conversions. For example, timestamps are provided exactly as returned in the original JSON; they are not converted to Ruby DateTime objects.
95
+
96
+ ```ruby
97
+ computer.purchasing.po_date_utc # => "2016-03-18T00:00:00.000-0500"
98
+ ```
99
+
100
+ ## Why not ruby-jss?
101
+
102
+ The [ruby-jss](http://pixaranimationstudios.github.io/ruby-jss/) gem is the most popular Ruby library for accessing the JSS API. Compared to Jess, ruby-jss has many more features, and is probably the more suitable solution for most use cases. So when might you use Jess instead?
103
+
104
+ * Jess offers a cleaner connection model that doesn't rely on global variables, which makes Jess easier to use if you need to connect to multiple JSS endpoints in the same Ruby process.
105
+ * Jess internally uses Ruby standard library Net::HTTP and supports persistent HTTP/1.1 connections; this may be faster than ruby-jss in some scenarios.
106
+
107
+ ## Who uses Jess?
108
+
109
+ Jess powers [Robot Cloud Dashboard](http://www.robotcloud.net/dashboard/).
110
+
111
+ ## Development
112
+
113
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the tests and RuboCop checks. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
114
+
115
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
116
+
117
+ ## Contributing
118
+
119
+ Bug reports and other discussions are welcome on GitHub at <https://github.com/mattbrictson/jess>. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
120
+
121
+ Before opening a pull request, please read [CONTRIBUTING.md](CONTRIBUTING.md) for important coding guidelines and policies.
122
+
123
+ ## License
124
+
125
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,15 @@
1
+ require "bundler/gem_tasks"
2
+ require "chandler/tasks"
3
+ require "rake/testtask"
4
+ require "rubocop/rake_task"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ RuboCop::RakeTask.new
13
+
14
+ task "release:rubygem_push" => "chandler:push"
15
+ task default: [:test, :rubocop]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jess"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "jess/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jess"
8
+ spec.version = Jess::VERSION
9
+ spec.authors = ["Matt Brictson"]
10
+ spec.email = ["opensource@mattbrictson.com"]
11
+
12
+ spec.summary = "Lightweight, unofficial client for the JAMF Software "\
13
+ "Server (JSS) API"
14
+ spec.homepage = "https://github.com/mattbrictson/jess"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.required_ruby_version = ">= 2.1.0"
25
+
26
+ spec.add_development_dependency "awesome_print", "~> 1.7"
27
+ spec.add_development_dependency "bundler", "~> 1.13"
28
+ spec.add_development_dependency "chandler", "~> 0.3"
29
+ spec.add_development_dependency "coveralls", "~> 0.8.15"
30
+ spec.add_development_dependency "rake", "~> 11.0"
31
+ spec.add_development_dependency "minitest", "~> 5.0"
32
+ spec.add_development_dependency "minitest-reporters", "~>1.1"
33
+ spec.add_development_dependency "rubocop", "~> 0.43"
34
+ spec.add_development_dependency "webmock", "~> 2.1"
35
+ end
@@ -0,0 +1,32 @@
1
+ require "jess/version"
2
+
3
+ # Jess is a lightweight client for the JAMF Software Server (JSS) API.
4
+ #
5
+ # Example usage:
6
+ #
7
+ # conn = Jess.connect("https://jsshost", username: "user", password: "secret")
8
+ # computer = conn.computers.find(1234)
9
+ # computer.id # => 1234
10
+ # computer.name # => "Matt's iMac"
11
+ # computer.hardware.model # => "iMac Intel (Retina 5k, 27-Inch, Late 2015)"
12
+ #
13
+ module Jess
14
+ autoload "Computer", "jess/computer"
15
+ autoload "Computers", "jess/computers"
16
+ autoload "Connection", "jess/connection"
17
+ autoload "ExtensionAttributes", "jess/extension_attributes"
18
+ autoload "HttpClient", "jess/http_client"
19
+ autoload "MobileDevice", "jess/mobile_device"
20
+ autoload "MobileDevices", "jess/mobile_devices"
21
+ autoload "Resource", "jess/resource"
22
+
23
+ # Establish a connection with JSS and return a Jess::Connection object that
24
+ # can be used to interact with the JSS API. This is a convenience method. For
25
+ # more fine-grained control over the connection, create a Jess::HttpClient
26
+ # using the desired options, then pass it to Jess::Connection.new.
27
+ #
28
+ def self.connect(url, username:, password:)
29
+ client = HttpClient.new(url, username: username, password: password)
30
+ Connection.new(client)
31
+ end
32
+ end
@@ -0,0 +1,19 @@
1
+ require "forwardable"
2
+
3
+ module Jess
4
+ # A Computer record returned from JSS. The data mirrors the structure of the
5
+ # JSS Computer JSON representation, with sections for general, hardware,
6
+ # extension attributes, etc.
7
+ class Computer < Resource
8
+ extend Forwardable
9
+ def_delegators :general, :id, :name
10
+
11
+ def extension_attributes
12
+ @ext_attrs ||= ExtensionAttributes.new(super)
13
+ end
14
+
15
+ def inspect
16
+ "Jess::Computer<##{id}, #{name}>"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ require "json"
2
+
3
+ module Jess
4
+ # Computer-related JSS API operations
5
+ class Computers
6
+ attr_reader :http_client
7
+
8
+ def initialize(http_client)
9
+ @http_client = http_client
10
+ end
11
+
12
+ # Retrieve a computer by ID.
13
+ def find(id)
14
+ json = JSON.parse(http_client.get("computers/id/#{id}"))
15
+ Computer.new(json.fetch("computer"))
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ module Jess
2
+ # Provides a high-level facade for operations of the JSS API. This is the
3
+ # primary interface for all operations provided by the Jess gem.
4
+ class Connection
5
+ attr_reader :http_client
6
+
7
+ def initialize(http_client)
8
+ @http_client = http_client
9
+ end
10
+
11
+ def computers
12
+ @computers ||= Computers.new(http_client)
13
+ end
14
+
15
+ def mobile_devices
16
+ @mobile_devices ||= MobileDevices.new(http_client)
17
+ end
18
+
19
+ def inspect
20
+ "Jess::Connection<#{http_client.inspect}>"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ require "forwardable"
2
+
3
+ module Jess
4
+ # A Hash-like wrapper around the extension attributes that facilitates easy
5
+ # key/value access.
6
+ class ExtensionAttributes < Resource
7
+ extend Forwardable
8
+ def_delegators :@values, :[], :fetch, :key?, :keys, :size, :length, :to_h
9
+
10
+ def initialize(json)
11
+ super
12
+ @values = json.each_with_object({}) do |attr, hash|
13
+ hash[attr.name] = attr.value
14
+ end
15
+ @values.freeze
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,80 @@
1
+ require "logger"
2
+ require "net/http"
3
+
4
+ module Jess
5
+ # Provides low-level access to making authenticated GET requests to the JSS
6
+ # API, with basic logging and error handling. You will normally not need to
7
+ # use this class directly, unless you need to make HTTP requests that haven't
8
+ # been wrapped by the higher level `Jess::Connection` API.
9
+ #
10
+ class HttpClient
11
+ autoload :Error, "jess/http_client/error"
12
+ autoload :BadCredentials, "jess/http_client/error"
13
+ autoload :ConnectionError, "jess/http_client/error"
14
+ autoload :LoggingDecorator, "jess/http_client/logging_decorator"
15
+ autoload :NotFound, "jess/http_client/error"
16
+ autoload :ServerError, "jess/http_client/error"
17
+ autoload :ErrorDecorator, "jess/http_client/error_decorator"
18
+
19
+ attr_reader :url, :username, :logger, :net_http_options
20
+
21
+ def initialize(url, username:, password:,
22
+ logger: default_logger, net_http_options: nil)
23
+ @url = url
24
+ @username = username
25
+ @password = password
26
+ @net_http_options = net_http_options || {}
27
+ @logger = logger
28
+ end
29
+
30
+ # Makes a GET request for the given path. The result is the raw, unparsed
31
+ # body of the HTTP response. The path is resolved relative to the
32
+ # `resource_uri` and should not start with a slash.
33
+ def get(path, accept: "application/json")
34
+ req = Net::HTTP::Get.new(URI.join(resource_uri, path))
35
+ req.basic_auth(username, password)
36
+ req["Accept"] = accept
37
+ response = http.request(req)
38
+ response.body.to_s
39
+ end
40
+
41
+ # The canonical JSSResource URI used to issue requests.
42
+ def resource_uri
43
+ @resource_uri ||= begin
44
+ root_url = url.to_s.sub(%r{JSSResource/*$}, "")
45
+ root_url << "/" unless root_url.end_with?("/")
46
+ URI.join(root_url, "JSSResource/")
47
+ end
48
+ end
49
+
50
+ def inspect
51
+ url = resource_uri.to_s.sub(%r{://}, "://#{username}@")
52
+ "Jess::HttpClient<#{url}>"
53
+ end
54
+
55
+ private
56
+
57
+ attr_reader :password
58
+
59
+ def default_logger
60
+ defined?(Rails) ? Rails.logger : Logger.new($stderr)
61
+ end
62
+
63
+ def http
64
+ @http ||= begin
65
+ http = Net::HTTP.new(resource_uri.host, resource_uri.port)
66
+ http.read_timeout = 15
67
+ http.open_timeout = 15
68
+ http.use_ssl = true if resource_uri.scheme == "https"
69
+ apply_net_http_options(http)
70
+ LoggingDecorator.new(logger, ErrorDecorator.new(http))
71
+ end
72
+ end
73
+
74
+ def apply_net_http_options(http)
75
+ net_http_options.each do |attr, value|
76
+ http.public_send("#{attr}=", value)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,28 @@
1
+ module Jess
2
+ class HttpClient
3
+ # Base class for exceptions raised by Jess::HttpClient. These exceptions
4
+ # hold a reference to the URI and HTTP method (e.g. "GET", "POST") that were
5
+ # being attempted when the error occurred.
6
+ #
7
+ class Error < StandardError
8
+ attr_accessor :uri, :http_method
9
+
10
+ def to_s
11
+ "#{super} (during #{http_method.to_s.upcase} #{uri})"
12
+ end
13
+ end
14
+
15
+ # Raised when Jess::HttpClient fails to open an HTTP connection.
16
+ ConnectionError = Class.new(Error)
17
+
18
+ # Raised when Jess::HttpClient receives a 500 error from the server.
19
+ ServerError = Class.new(Error)
20
+
21
+ # Raised when Jess::HttpClient receives a 404 error from the server.
22
+ NotFound = Class.new(Error)
23
+
24
+ # Raised when Jess::HttpClient receives a 401 error from the server, which
25
+ # happens when the username and/or password are incorrect.
26
+ BadCredentials = Class.new(Error)
27
+ end
28
+ end
@@ -0,0 +1,44 @@
1
+ require "delegate"
2
+
3
+ module Jess
4
+ class HttpClient
5
+ # Wraps a Net::HTTP object to provide exception handling around the
6
+ # `request` method, such that 400 and 500 error codes are translated into
7
+ # appropriate Jess::HttpClient::Error exceptions.
8
+ #
9
+ class ErrorDecorator < SimpleDelegator
10
+ def request(req)
11
+ super.tap do |response|
12
+ raise_if_error_code(response)
13
+ end
14
+ rescue StandardError => e
15
+ handle_exception(e, req)
16
+ end
17
+
18
+ private
19
+
20
+ def raise_if_error_code(res)
21
+ return if res.is_a?(Net::HTTPSuccess)
22
+ raise BadCredentials, res.message if res.code == "401"
23
+ raise NotFound, res.message if res.code == "404"
24
+ raise ServerError, res.message if res.is_a?(Net::HTTPServerError)
25
+ raise Error, res.message
26
+ end
27
+
28
+ def handle_exception(e, req)
29
+ case e
30
+ when IOError, Timeout::Error
31
+ e = ConnectionError.new(e.message)
32
+ when Error
33
+ # pass
34
+ else
35
+ e = Error.new(e.inspect)
36
+ end
37
+
38
+ e.uri = req.uri
39
+ e.http_method = req.method
40
+ raise e
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,51 @@
1
+ require "delegate"
2
+
3
+ module Jess
4
+ class HttpClient
5
+ # Wraps a Net::HTTP object to log all requests to a given Logger object.
6
+ class LoggingDecorator < SimpleDelegator
7
+ def initialize(logger, http)
8
+ super(http)
9
+ @logger = logger
10
+ end
11
+
12
+ def request(req)
13
+ return super if logger.nil?
14
+
15
+ log_request(req)
16
+ response = super
17
+ log_response(response, req.uri)
18
+ response
19
+ rescue Error => e
20
+ logger.error(e.to_s) if logger
21
+ raise
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :logger
27
+
28
+ def log_request(req)
29
+ logger.debug { "#{req.method} #{req.uri}" }
30
+ end
31
+
32
+ def log_response(response, uri)
33
+ logger.debug do
34
+ "Received #{response_desc(response)} from #{uri}"
35
+ end
36
+ end
37
+
38
+ def response_desc(response)
39
+ content_type = response.content_type
40
+ desc = ""
41
+ desc << if response.body && response.body.length
42
+ "#{response.body.length} bytes"
43
+ else
44
+ "response"
45
+ end
46
+ desc << " (#{content_type})" if content_type
47
+ desc
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,19 @@
1
+ require "forwardable"
2
+
3
+ module Jess
4
+ # A Mobile Device record returned from JSS. The data mirrors the structure of
5
+ # the JSS Mobile Device JSON representation, with sections for general,
6
+ # security, extension attributes, etc.
7
+ class MobileDevice < Resource
8
+ extend Forwardable
9
+ def_delegators :general, :id, :name
10
+
11
+ def extension_attributes
12
+ @ext_attrs ||= ExtensionAttributes.new(super)
13
+ end
14
+
15
+ def inspect
16
+ "Jess::MobileDevice<##{id}, #{name}>"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ require "json"
2
+
3
+ module Jess
4
+ # Mobile device-related JSS API operations
5
+ class MobileDevices
6
+ attr_reader :http_client
7
+
8
+ def initialize(http_client)
9
+ @http_client = http_client
10
+ end
11
+
12
+ # Retrieve a mobile device by ID.
13
+ def find(id)
14
+ json = JSON.parse(http_client.get("mobiledevices/id/#{id}"))
15
+ MobileDevice.new(json.fetch("mobile_device"))
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,39 @@
1
+ module Jess
2
+ # Wraps a JSON object that is returned from the JSS API. The underlying raw
3
+ # JSON is available via `_json`. Properties of the JSON can be accessed via
4
+ # `method_missing`, so that a Resource behaves like a typical Ruby object.
5
+ # Accessing a non-existent JSON property will raise `NoMethodError`.
6
+ #
7
+ class Resource
8
+ attr_reader :_json
9
+
10
+ def initialize(json)
11
+ @_json = json.freeze
12
+ end
13
+
14
+ private
15
+
16
+ def method_missing(symbol, *args)
17
+ if _json.key?(symbol.to_s)
18
+ _as_resource(_json.public_send(:[], symbol.to_s, *args))
19
+ else
20
+ super
21
+ end
22
+ end
23
+
24
+ def respond_to_missing?(symbol, include_all)
25
+ super || _json.key?(symbol.to_s)
26
+ end
27
+
28
+ def _as_resource(json)
29
+ case json
30
+ when Hash
31
+ Resource.new(json)
32
+ when Array
33
+ json.map(&method(:_as_resource))
34
+ else
35
+ json
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Jess
2
+ VERSION = "0.1.0".freeze
3
+ end
metadata ADDED
@@ -0,0 +1,197 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jess
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Brictson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: chandler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.15
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.15
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '11.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '11.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: minitest-reporters
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.1'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.1'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.43'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.43'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '2.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '2.1'
139
+ description:
140
+ email:
141
+ - opensource@mattbrictson.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".github/pull_request_template.md"
147
+ - ".gitignore"
148
+ - ".rubocop.yml"
149
+ - ".travis.yml"
150
+ - CHANGELOG.md
151
+ - CODE_OF_CONDUCT.md
152
+ - CONTRIBUTING.md
153
+ - Gemfile
154
+ - LICENSE.txt
155
+ - README.md
156
+ - Rakefile
157
+ - bin/console
158
+ - bin/setup
159
+ - jess.gemspec
160
+ - lib/jess.rb
161
+ - lib/jess/computer.rb
162
+ - lib/jess/computers.rb
163
+ - lib/jess/connection.rb
164
+ - lib/jess/extension_attributes.rb
165
+ - lib/jess/http_client.rb
166
+ - lib/jess/http_client/error.rb
167
+ - lib/jess/http_client/error_decorator.rb
168
+ - lib/jess/http_client/logging_decorator.rb
169
+ - lib/jess/mobile_device.rb
170
+ - lib/jess/mobile_devices.rb
171
+ - lib/jess/resource.rb
172
+ - lib/jess/version.rb
173
+ homepage: https://github.com/mattbrictson/jess
174
+ licenses:
175
+ - MIT
176
+ metadata: {}
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: 2.1.0
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ requirements: []
192
+ rubyforge_project:
193
+ rubygems_version: 2.6.6
194
+ signing_key:
195
+ specification_version: 4
196
+ summary: Lightweight, unofficial client for the JAMF Software Server (JSS) API
197
+ test_files: []