caseblocks 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
+ SHA256:
3
+ metadata.gz: b172872a97d643f7269d75119c04695c3fe4bef7caa6ac8c483f732973b8b21c
4
+ data.tar.gz: f126f936256e213f444498c92d8d0b5c245eec4a9f4f8a7cc1bed052c8827247
5
+ SHA512:
6
+ metadata.gz: 31a56027a5527632f7e1d73901c7fd1d405c898eee1acc680fde5b3be0c21e4ef37f162180ecf06255e7f71bc53ab1e4d23977a863e130645def1a7ee3ef1f17
7
+ data.tar.gz: b6f77a353bc6bff79288398eaa31698f77a4b162f54496ab7002432b0124397e3afcd367a3a1b367beef538df06c5ae7664f020339223fa9f0bfb9237acd6bad
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ caseblocks_gem
@@ -0,0 +1 @@
1
+ ruby-2.5.1
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
@@ -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 stewart@theizone.co.uk. 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/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in caseblocks.gemspec
6
+ gemspec
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ caseblocks (0.1.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (5.2.0)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 0.7, < 2)
13
+ minitest (~> 5.1)
14
+ tzinfo (~> 1.1)
15
+ awesome_print (1.8.0)
16
+ concurrent-ruby (1.0.5)
17
+ diff-lcs (1.3)
18
+ i18n (1.0.1)
19
+ concurrent-ruby (~> 1.0)
20
+ minitest (5.11.3)
21
+ rake (10.5.0)
22
+ rspec (3.7.0)
23
+ rspec-core (~> 3.7.0)
24
+ rspec-expectations (~> 3.7.0)
25
+ rspec-mocks (~> 3.7.0)
26
+ rspec-core (3.7.1)
27
+ rspec-support (~> 3.7.0)
28
+ rspec-expectations (3.7.0)
29
+ diff-lcs (>= 1.2.0, < 2.0)
30
+ rspec-support (~> 3.7.0)
31
+ rspec-mocks (3.7.0)
32
+ diff-lcs (>= 1.2.0, < 2.0)
33
+ rspec-support (~> 3.7.0)
34
+ rspec-support (3.7.1)
35
+ thread_safe (0.3.6)
36
+ tzinfo (1.2.5)
37
+ thread_safe (~> 0.1)
38
+ yard (0.9.14)
39
+
40
+ PLATFORMS
41
+ ruby
42
+
43
+ DEPENDENCIES
44
+ awesome_print
45
+ bundler (~> 1.16)
46
+ caseblocks!
47
+ rake (~> 10.0)
48
+ rspec (~> 3.0)
49
+ yard
50
+
51
+ BUNDLED WITH
52
+ 1.16.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Stewart McKee
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,43 @@
1
+ # Caseblocks
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/caseblocks`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'caseblocks'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install caseblocks
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/caseblocks. 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.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Caseblocks project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/caseblocks/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "caseblocks"
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
+ # Caseblocks::Client.setup(:authentication_token => "gsDc6vArQdArthQVRTSj")
14
+ $client = Caseblocks::Client.new(:host => "http://localhost:8888", :authentication_token => "gmcQVzJznHXqXyR4VaXe")
15
+
16
+ require "irb"
17
+ 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,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "caseblocks/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "caseblocks"
8
+ spec.version = Caseblocks::VERSION
9
+ spec.authors = ["Stewart McKee"]
10
+ spec.email = ["stewart@theizone.co.uk"]
11
+
12
+ spec.summary = ""
13
+ spec.description = ""
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "activesupport"
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "awesome_print"
32
+ spec.add_development_dependency "yard"
33
+ end
@@ -0,0 +1,19 @@
1
+ require "caseblocks/version"
2
+ require "caseblocks/errors/general_error"
3
+ require "caseblocks/errors/validation_error"
4
+ require "caseblocks/client"
5
+ require "caseblocks/case"
6
+ require "caseblocks/case_type"
7
+ require "caseblocks/task"
8
+ require "caseblocks/bucket"
9
+ require "caseblocks/bucket_group"
10
+ require "caseblocks/relationship"
11
+
12
+ require "caseblocks/helpers/elastic"
13
+
14
+ require 'awesome_print'
15
+
16
+
17
+ module Caseblocks
18
+ # Your code goes here...
19
+ end
@@ -0,0 +1,52 @@
1
+ module Caseblocks
2
+ class Bucket
3
+
4
+ def self.all(client)
5
+ res = client.request("/case_blocks/buckets.json")
6
+ res["buckets"].map{|bucket| Caseblocks::Bucket.new(bucket, client)}
7
+ end
8
+
9
+ def self.find(id, client)
10
+ res = client.request("/case_blocks/buckets/#{id}.json")
11
+ return Bucket.new(res["bucket"], client)
12
+ end
13
+
14
+ def initialize(bucket_data, client)
15
+ @client = client
16
+ @bucket_data = bucket_data
17
+ end
18
+
19
+ def name
20
+ @bucket_data["name"]
21
+ end
22
+ def id
23
+ @bucket_data["id"]
24
+ end
25
+
26
+ def case_type
27
+ @case_type ||= Caseblocks::CaseType.find(case_type_id, @client)
28
+ end
29
+
30
+ def case_type_id
31
+ @bucket_data["case_type_id"] || @bucket_data["work_type_id"] || @bucket_data["people_type_id"] || @bucket_data["organization_type_id"]
32
+ end
33
+
34
+
35
+ def cases(options={})
36
+ res = @client.request("case_blocks/bucket_stats/#{id}")
37
+ total_results = res["bucket_summary"]["total"]
38
+
39
+ page = options[:page] || 0
40
+ page_size = options[:page_size] || 10
41
+
42
+ case_list = []
43
+
44
+ (0..total_results/page_size).each do |page|
45
+ query = {:bucket_id => id, :page => page, :page_size => page_size}.map{|k,v| "#{k}=#{v}"}.join("&")
46
+ case_list += res = @client.request("/case_blocks/#{case_type.code}?#{query}")[case_type.code].map{|c| Caseblocks::Case.new(c, @client) }
47
+ end
48
+ case_list
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,29 @@
1
+ module Caseblocks
2
+ class BucketGroup
3
+
4
+ def self.all(client)
5
+ res = client.new.request("/case_blocks/bucket_groups.json")
6
+ res["bucket_groups"].map{|bg| Caseblocks::BucketGroup.new(bg, client)}
7
+ end
8
+
9
+ def self.find(id, client)
10
+ res = client.request("/case_blocks/bucket_group/#{id}.json")
11
+ return Bucket.new(res["bucket_group"], client)
12
+ end
13
+
14
+ def initialize(bucket_group_data, client)
15
+ @client = client
16
+ @bucket_group_data = bucket_group_data
17
+ end
18
+
19
+ def name
20
+ @bucket_group_data["name"]
21
+ end
22
+
23
+ def buckets
24
+ query = @bucket_group_data["buckets"].map{|id| "ids%5B%5D=#{id}" }.join("&")
25
+ @buckets ||= @client.request("/case_blocks/buckets.json?#{query}")["buckets"].map{|bucket| Caseblocks::Bucket.new(bucket, client) }
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,141 @@
1
+ require 'active_support/inflector'
2
+ # @since 0.1.0
3
+ module Caseblocks
4
+
5
+ # Class for handling Case objects
6
+ #
7
+ # Relationships are accessed using dynamic method names based on the related case type code, for example if you have an order case type relating to customer then kase.customers will return an array of related customer cases
8
+ #
9
+ # Currently there is a very simplistic caching mechanism, so may cause issues if you need to refresh data
10
+ #
11
+ # @author Stewart McKee
12
+ # @since 0.1.0
13
+ class Case
14
+
15
+ # Finds an individual case from an id
16
+ # @param id [String] the bson id of the case being loaded
17
+ # @param client [Caseblocks::Client] client to be used
18
+ # @return [Caseblocks::Case] the case being searched for or nil if the case is not found
19
+ def self.find(id, client=Caseblocks::Client.default)
20
+ res = client.request("/case_blocks/cases/#{id}.json")
21
+ return Case.new(res["case"], client)
22
+ end
23
+
24
+ # Finds an individual case based on search criteria matched to the cases data
25
+ # @param criteria [Hash] a hash where the keys are the field names and values are the search param for that field. Note wildcards are not supported
26
+ # @param client [Caseblocks::Client] client to be used
27
+ # @return [Caseblocks::Case] the case being searched for or nil if the case is not found
28
+ def self.find_by(criteria, client=Caseblocks::Client.default)
29
+ Caseblocks::Case.find_all_by(criteria, client).first
30
+ end
31
+
32
+ # Finds all cases matching the supplied search criteria matched to the cases data
33
+ # @param criteria [Hash] a hash where the keys are the field names and values are the search param for that field. Note wildcards are not supported
34
+ # @param client [Caseblocks::Client] client to be used
35
+ # @return [Array<Caseblocks::Case>] returns an array of Caseblocks::Case objects that match the requested criteria
36
+ def self.find_all_by(criteria, client=Caseblocks::Client.default, options={})
37
+ options[:page_size] = 10 unless options.has_key?(:page_size)
38
+ options[:page] = 0 unless options.has_key?(:page)
39
+
40
+ current_page = 0
41
+ page_size = options[:page_size]
42
+ total = -1
43
+ results = []
44
+ while (total == -1 || ((current_page * page_size) + page_size) < total) do
45
+ options[:query_type] = "AND" unless options.has_key?(:query_type)
46
+ query = {:query => criteria.map{|k,v| "#{k}:\"#{Caseblocks::Helpers::Elastic.encode_query(v).gsub("&", "%26")}\""}.join(" #{options[:query_type]} ")}
47
+ query.merge!(options)
48
+ res = client.request("/case_blocks/search.json", query)
49
+ total = res.inject(0){|total, ct| total += ct["total"].to_i }
50
+ cases = res.map{|ct| ct["cases"] }.flatten
51
+ results += cases.map{|kase| Case.new(kase, client) }
52
+ current_page += 1
53
+ options[:page] = current_page
54
+ end
55
+ results
56
+ end
57
+
58
+ # Initializes a new case object
59
+ # @param case_data [Hash] hash of data from the case
60
+ # @param client [Caseblocks::Client] client to be used
61
+ # @return [Caseblocks::Case] the case object
62
+ def initialize(case_data, client)
63
+ @client = client
64
+ @case_data = case_data
65
+ end
66
+
67
+ # Returns the bson id for the case
68
+ #@return [String] BSON id for the current case
69
+ def id
70
+ @case_data["_id"]
71
+ end
72
+
73
+ # Case type for the current case
74
+ #@return [Caseblocks::CaseType] Case Type for the current case
75
+ def case_type
76
+ @case_type ||= Caseblocks::CaseType.find(case_type_id, @client)
77
+ end
78
+
79
+ # Case type id for the current case
80
+ #@return [Integer] The id for the current case, taking into account work/people/organization types
81
+ def case_type_id
82
+ @case_data["case_type_id"] || @case_data["work_type_id"] || @case_data["people_type_id"] || @case_data["organization_type_id"]
83
+ end
84
+
85
+ # The case data for the current case. This can be treated as the hash of data within the case as the save operation will use this data to update the case
86
+ #@return [Hash] Case data object
87
+ def attributes
88
+ @case_data
89
+ end
90
+
91
+
92
+ # Creates a case within the supplied case type with the data requested. Will fail if validation fails with a Caseblocks::ValidationError error
93
+ # @param data [Hash] The case data to create
94
+ # @param data [Caseblocks::Client] the client to connect to caseblocks with
95
+ # @example Create a simple case
96
+ # client = Caseblocks::Client.new(:authentication_token => "qsodjgoasgdsjgsd")
97
+ # case_type = Caseblocks::CaseType.find(204, client)
98
+ # kase = Caseblocks::Case.create({:title => "I'm a teapot", :case_type => case_type}, client)
99
+ # puts kase.id
100
+ # puts kase.attributes["title"]
101
+ # @raise [Caseblocks::ValidationError] This occurs when a validation error occurrs with regards to the data supplied. This could be explicit validation failures or could be a conflict of document version numbers
102
+ # @raise [Caseblocks::Error] General error
103
+ # @return [Caseblocks::Case] The newly created case object with the data supplied from the server. This will include default values and fields created by the server.
104
+ def self.create(data, case_type, client=Caseblocks::Client.default)
105
+ data["case_type_id"] = case_type.id
106
+ res = client.post("/case_blocks/#{case_type.code.pluralize}.json", {case_type.code => data})
107
+ Case.new(res[case_type.code], client)
108
+ end
109
+
110
+ # Saves the current data against the case and updates attributes with latest from server
111
+ # @return [Boolean] Returns true if the result is a success
112
+ def save
113
+ res = @client.put("/case_blocks/cases/#{id}.json", @case_data)
114
+ @case_data.merge(res["case"])
115
+ true
116
+ end
117
+
118
+ # @private
119
+ def method_missing(m, *args, &block)
120
+ matched_relationship = case_type.relationships.detect{|r| r.case_type.code == m.to_s.singularize}
121
+ if matched_relationship
122
+ return @client.request("/case_blocks/#{matched_relationship.case_type.code.pluralize}?related_cases%5Brelation_id%5D=#{matched_relationship.id}&related_cases%5Brelationship_type%5D=CaseBlocks%3A%3ACaseTypeDirectRelationship&related_cases%5Bcase_from_id%5D=#{id}&related_cases%5Bpage_size%5D=1000")[matched_relationship.case_type.code.pluralize].map{|kase| Caseblocks::Case.new(kase, @client) }
123
+ else
124
+ super
125
+ end
126
+ end
127
+
128
+ # @todo Implement me
129
+ def tasklists
130
+ []
131
+ end
132
+
133
+ # Returns an array of Caseblocks::Task that are on this case. This is a full list of all tasks, not split by tasklist.
134
+ #@return [Array<Caseblocks::Task>] Array of task objects
135
+ def tasks
136
+ @tasks ||= Caseblocks::Task.find_all(@case_data["tasks"], @client)
137
+ end
138
+
139
+
140
+ end
141
+ end
@@ -0,0 +1,69 @@
1
+ module Caseblocks
2
+ class CaseType
3
+
4
+ def self.all(client)
5
+ case_types = []
6
+ case_types += client.request("/case_blocks/case_types.json")["case_types"].map{|case_type| Caseblocks::CaseType.new(case_type, client) }
7
+ case_types += client.request("/case_blocks/people_types.json")["people_types"].map{|case_type| Caseblocks::CaseType.new(case_type, client) }
8
+ case_types += client.request("/case_blocks/organization_types.json")["organization_types"].map{|case_type| Caseblocks::CaseType.new(case_type, client) }
9
+ case_types
10
+ end
11
+
12
+ def self.find(id, client)
13
+ res = client.request("/case_blocks/case_types/#{id}.json")
14
+ CaseType.new(res["case_type"], client)
15
+ end
16
+
17
+ def self.find_by(criteria, client)
18
+ CaseType.all(client).detect{|ct| criteria.reject{|k,v| ct.attributes[k.to_s] == v }.empty? }
19
+ end
20
+
21
+ def initialize(case_type_data, client)
22
+ @client = client
23
+ @case_type_data = case_type_data
24
+ end
25
+
26
+ def id
27
+ @case_type_data["id"]
28
+ end
29
+ def name
30
+ @case_type_data["name"]
31
+ end
32
+ def description
33
+ @case_type_data["description"]
34
+ end
35
+ def current_schema
36
+ @case_type_data["schema"].last
37
+ end
38
+ def code
39
+ @case_type_data["code"]
40
+ end
41
+
42
+ def attributes
43
+ @case_type_data
44
+ end
45
+
46
+ def cases(options={})
47
+ Caseblocks::Case.find_all_by({:case_type_id => id}, @client, options)
48
+ end
49
+
50
+ def relationships
51
+ if @case_type_data["direct_relationships"].length > 0
52
+ query = @case_type_data["direct_relationships"].map{|dr| "ids%5B%5D=#{dr}" }.join("&")
53
+ @relationships ||= @client.request("/case_blocks/case_type_direct_relationships.json?#{query}")["case_type_direct_relationships"].map{|r| Caseblocks::Relationship.new(r, @client) }
54
+ else
55
+ @relationships ||= []
56
+ end
57
+ end
58
+
59
+ def buckets
60
+ query = "case_type_id=#{id}"
61
+ @buckets ||= @client.request("/case_blocks/buckets.json?#{query}")["buckets"].map{|bg| Caseblocks::Bucket.new(bg, @client) }
62
+ end
63
+ def bucket_groups
64
+ query = @case_type_data["bucket_groups"].map{|id| "ids%5B%5D=#{id}" }.join("&")
65
+ @bucket_groups ||= @client.request("/case_blocks/bucket_groups.json?#{query}")["bucket_groups"].map{|bg| Caseblocks::BucketGroup.new(bg, @client) }
66
+ end
67
+
68
+ end
69
+ end
@@ -0,0 +1,104 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'awesome_print'
4
+
5
+ module Caseblocks
6
+ class Client
7
+
8
+ def initialize(options={})
9
+ @options = options
10
+ @host = URI.parse(@options[:host] || "https://login.caseblocks.com")
11
+ @authentication_token = @options[:authentication_token]
12
+ @logger = @options[:logger] || Logger.new(STDOUT)
13
+ @cache= {}
14
+ end
15
+
16
+ def self.setup(options)
17
+ @default_client = Client.new(options)
18
+ end
19
+
20
+ def self.default
21
+ @default_client
22
+ end
23
+
24
+ def case_types
25
+ Caseblocks::CaseType.all(self)
26
+ end
27
+
28
+ def clear_cache(pattern=nil)
29
+ if pattern.nil?
30
+ @cache = {}
31
+ else
32
+ # puts "Clearing Cache for #{pattern}"
33
+ @cache = @cache.reject{|k,v| k.to_s.include?(pattern) }
34
+ # ap @cache.keys
35
+ end
36
+
37
+ end
38
+
39
+ def request(url, query={})
40
+ uri = @host.merge(url)
41
+ uri.query = (uri.query.to_s.split("&") + query.merge(:auth_token => @authentication_token).map{|k,v| "#{k}=#{URI.encode(v.to_s)}"}).join("&")
42
+ req = Net::HTTP::Get.new(uri)
43
+ req['Content-Type'] = "application/json"
44
+
45
+ if @cache[uri.to_s]
46
+ @logger.debug "CACHE #{uri.to_s}"
47
+ return @cache[uri.to_s]
48
+ end
49
+ @logger.debug "GET #{uri.to_s}"
50
+
51
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') {|http|
52
+ http.request(req)
53
+ }
54
+ if res.code.to_i >= 200 && res.code.to_i < 300
55
+ @cache[uri.to_s] = JSON.parse(res.body)
56
+ return @cache[uri.to_s]
57
+ else
58
+ if res.code.to_i == 404
59
+ raise Caseblocks::Errors::GeneralError.new(res.body, :url => uri.to_s)
60
+ else
61
+ raise Caseblocks::Errors::GeneralError.new(res.body, :url => uri.to_s)
62
+ end
63
+ end
64
+ end
65
+
66
+ def post(url, data)
67
+ uri = @host.merge(url)
68
+ uri.query = (uri.query.to_s.split("&") + {}.merge(:auth_token => @authentication_token).map{|k,v| "#{k}=#{v.to_s}"}).join("&")
69
+ req = Net::HTTP::Post.new(uri)
70
+ req.body = data.to_json
71
+ req.content_type = "application/json"
72
+ @logger.debug "POST #{uri.to_s}"
73
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
74
+ http.request(req)
75
+ end
76
+ if res.code.to_i >= 200 && res.code.to_i < 300
77
+ JSON.parse(res.body)
78
+ elsif res.code.to_i == 422
79
+ raise Caseblocks::Errors::ValidationError.new("Validation Error", JSON.parse(res.body)["errors"], :url => uri.to_s)
80
+ else
81
+ raise Caseblocks::Errors::GeneralError.new(res.body, :url => uri.to_s)
82
+ end
83
+ end
84
+
85
+ def put(url, data)
86
+ uri = @host.merge(url)
87
+ uri.query = (uri.query.to_s.split("&") + {}.merge(:auth_token => @authentication_token).map{|k,v| "#{k}=#{v}"}).join("&")
88
+ @logger.debug "PUT #{uri.to_s}"
89
+ req = Net::HTTP::Put.new(uri, 'Content-Type' => 'application/json')
90
+ req.body = data.to_json
91
+ res = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => uri.scheme == 'https') do |http|
92
+ http.request(req)
93
+ end
94
+ if res.code.to_i >= 200 && res.code.to_i < 300
95
+ JSON.parse(res.body)
96
+ elsif res.code.to_i == 422
97
+ raise Caseblocks::Errors::ValidationError.new("Validation Error", JSON.parse(res.body)["errors"], :url => uri.to_s)
98
+ else
99
+ raise Caseblocks::Errors::GeneralError.new(res.body, :url => uri.to_s)
100
+ end
101
+ end
102
+
103
+ end
104
+ end
@@ -0,0 +1,13 @@
1
+ module Caseblocks
2
+ module Errors
3
+ class GeneralError < StandardError
4
+ def initialize(message, options={})
5
+ super(message)
6
+ @options = options
7
+ end
8
+ def params
9
+ @options
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ module Caseblocks
2
+ module Errors
3
+ class ValidationError < StandardError
4
+ def initialize(message, errors, options)
5
+ super(message)
6
+ @errors = errors
7
+ @options = options
8
+ end
9
+ def errors
10
+ @errors
11
+ end
12
+ def params
13
+ @options
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,11 @@
1
+ module Caseblocks
2
+ module Helpers
3
+ class Elastic
4
+
5
+ def self.encode_query(query)
6
+ reserved_characters = ["\\", " ", "/", "+", "–", "-", "!", "(", ")", "{", "}", "[", "]", "^", "\"", "~", "?", ":"]
7
+ reserved_characters.inject(query.to_s){|q, char| q.gsub(char, "\\\\#{char}")}
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ module Caseblocks
2
+ class Relationship
3
+
4
+ def initialize(relationship_data, client)
5
+ @client = client
6
+ @relationship_data = relationship_data
7
+ end
8
+
9
+ def id
10
+ @relationship_data["id"]
11
+ end
12
+
13
+ def from
14
+ @from ||= Caseblocks::CaseType.find(case_type_id("from"), @client)
15
+ end
16
+ def case_type
17
+ @case_type ||= Caseblocks::CaseType.find(case_type_id("to"), @client)
18
+ end
19
+
20
+ def case_type_id(direction)
21
+ @relationship_data["#{direction}_case_type_id"] || @relationship_data["#{direction}_work_type_id"] || @relationship_data["#{direction}_people_type_id"] || @relationship_data["#{direction}_organization_type_id"]
22
+ end
23
+
24
+ def attributes
25
+ @relationship_data
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,53 @@
1
+ module Caseblocks
2
+ class Task
3
+
4
+ def self.find(id, client)
5
+ res = client.request("/case_blocks/tasks/#{id}.json")
6
+ return Task.new(res["task"], client)
7
+ end
8
+
9
+ def self.find_all(ids, client)
10
+ query = ids.map{|id| "ids%5B%5D=#{id}" }
11
+ res = client.request("/case_blocks/tasks.json?#{query}")
12
+ res["tasks"].map{|t| Task.new(t, client)}
13
+ end
14
+
15
+ def initialize(task_data, client)
16
+ @client = client
17
+ @task_data = task_data
18
+ end
19
+
20
+ def id
21
+ @task_data["id"]
22
+ end
23
+ def description
24
+ @task_data["description"]
25
+ end
26
+ def action_type
27
+ @task_data["action_type"]
28
+ end
29
+
30
+ def execute
31
+ if ["ModifyCaseActionTask", "JavascriptActionTask"].include? @task_data["action_type"]
32
+ raise "Not Implemented"
33
+ else
34
+ raise "Can only execute a JavascriptActionTask or ModifyCaseActionTask"
35
+ end
36
+ end
37
+
38
+ def start
39
+ raise "Not Implemented"
40
+ end
41
+ def complete
42
+ raise "Not Implemented"
43
+ end
44
+ def schedule_at(date)
45
+ raise "Not Implemented"
46
+ end
47
+ def assign_to(user)
48
+ raise "Not Implemented"
49
+ end
50
+
51
+ end
52
+ end
53
+
@@ -0,0 +1,3 @@
1
+ module Caseblocks
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: caseblocks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Stewart McKee
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-09-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: awesome_print
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: ''
98
+ email:
99
+ - stewart@theizone.co.uk
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".ruby-gemset"
107
+ - ".ruby-version"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - Gemfile.lock
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - caseblocks.gemspec
118
+ - lib/caseblocks.rb
119
+ - lib/caseblocks/bucket.rb
120
+ - lib/caseblocks/bucket_group.rb
121
+ - lib/caseblocks/case.rb
122
+ - lib/caseblocks/case_type.rb
123
+ - lib/caseblocks/client.rb
124
+ - lib/caseblocks/errors/general_error.rb
125
+ - lib/caseblocks/errors/validation_error.rb
126
+ - lib/caseblocks/helpers/elastic.rb
127
+ - lib/caseblocks/relationship.rb
128
+ - lib/caseblocks/task.rb
129
+ - lib/caseblocks/version.rb
130
+ homepage: ''
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.7.6
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: ''
154
+ test_files: []