ecoportal-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +53 -0
  7. data/README.md +34 -0
  8. data/Rakefile +6 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +8 -0
  11. data/ecoportal-api.gemspec +28 -0
  12. data/lib/ecoportal/api.rb +11 -0
  13. data/lib/ecoportal/api/common.rb +13 -0
  14. data/lib/ecoportal/api/common/base_model.rb +39 -0
  15. data/lib/ecoportal/api/common/batch_operation.rb +94 -0
  16. data/lib/ecoportal/api/common/batch_response.rb +16 -0
  17. data/lib/ecoportal/api/common/client.rb +58 -0
  18. data/lib/ecoportal/api/common/doc_helpers.rb +27 -0
  19. data/lib/ecoportal/api/common/hash_diff.rb +37 -0
  20. data/lib/ecoportal/api/common/response.rb +22 -0
  21. data/lib/ecoportal/api/common/wrapped_response.rb +38 -0
  22. data/lib/ecoportal/api/internal.rb +31 -0
  23. data/lib/ecoportal/api/internal/.git-keep +0 -0
  24. data/lib/ecoportal/api/internal/account.rb +31 -0
  25. data/lib/ecoportal/api/internal/login_provider.rb +9 -0
  26. data/lib/ecoportal/api/internal/login_providers.rb +18 -0
  27. data/lib/ecoportal/api/internal/people.rb +19 -0
  28. data/lib/ecoportal/api/internal/permissions.rb +9 -0
  29. data/lib/ecoportal/api/internal/person.rb +40 -0
  30. data/lib/ecoportal/api/internal/person_details.rb +13 -0
  31. data/lib/ecoportal/api/internal/person_schema.rb +14 -0
  32. data/lib/ecoportal/api/internal/person_schemas.rb +15 -0
  33. data/lib/ecoportal/api/internal/policy_group.rb +9 -0
  34. data/lib/ecoportal/api/internal/policy_groups.rb +18 -0
  35. data/lib/ecoportal/api/internal/preferences.rb +9 -0
  36. data/lib/ecoportal/api/internal/schema_field.rb +8 -0
  37. data/lib/ecoportal/api/internal/schema_field_value.rb +8 -0
  38. data/lib/ecoportal/api/v1.rb +23 -0
  39. data/lib/ecoportal/api/v1/.git-keep +0 -0
  40. data/lib/ecoportal/api/v1/people.rb +98 -0
  41. data/lib/ecoportal/api/v1/person.rb +50 -0
  42. data/lib/ecoportal/api/v1/person_details.rb +51 -0
  43. data/lib/ecoportal/api/v1/person_schema.rb +46 -0
  44. data/lib/ecoportal/api/v1/person_schemas.rb +24 -0
  45. data/lib/ecoportal/api/v1/schema_field.rb +9 -0
  46. data/lib/ecoportal/api/v1/schema_field_value.rb +67 -0
  47. data/lib/ecoportal/api/version.rb +5 -0
  48. metadata +146 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5b99e6fa7d712d6b40f6c3a838aadecacd2ecf2d3b12100975a50b2811927872
4
+ data.tar.gz: d11092f6185355695b1f49aa3d5a37fecefbc6c874a68bd2f1d4df3bce8476c9
5
+ SHA512:
6
+ metadata.gz: 4b30f4789e25beb110b599f93958ee019052827c3b0e75337071c7aa05bc5dd8dc8002f3f95b1174260379da895b677c84167404b6e486842bee380e8d1a83ab
7
+ data.tar.gz: b722086c23c0331adc7b7335e718598d584b612fa9692d4f59ecb7886af04fe803ece3449c8f921f83f4080880a1c516e53493408e21f80c5b3aaabedfb7a483
data/.gitignore ADDED
@@ -0,0 +1,12 @@
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
12
+ scratch.rb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -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.1
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 ecoportal-api.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ecoportal-api (0.1.0)
5
+ http (~> 3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.5.2)
11
+ public_suffix (>= 2.0.2, < 4.0)
12
+ diff-lcs (1.3)
13
+ domain_name (0.5.20180417)
14
+ unf (>= 0.0.5, < 1.0.0)
15
+ http (3.3.0)
16
+ addressable (~> 2.3)
17
+ http-cookie (~> 1.0)
18
+ http-form_data (~> 2.0)
19
+ http_parser.rb (~> 0.6.0)
20
+ http-cookie (1.0.3)
21
+ domain_name (~> 0.5)
22
+ http-form_data (2.1.1)
23
+ http_parser.rb (0.6.0)
24
+ public_suffix (3.0.2)
25
+ rake (10.5.0)
26
+ rspec (3.7.0)
27
+ rspec-core (~> 3.7.0)
28
+ rspec-expectations (~> 3.7.0)
29
+ rspec-mocks (~> 3.7.0)
30
+ rspec-core (3.7.1)
31
+ rspec-support (~> 3.7.0)
32
+ rspec-expectations (3.7.0)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.7.0)
35
+ rspec-mocks (3.7.0)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.7.0)
38
+ rspec-support (3.7.1)
39
+ unf (0.1.4)
40
+ unf_ext
41
+ unf_ext (0.0.7.5)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler (~> 1.16)
48
+ ecoportal-api!
49
+ rake (~> 10.0)
50
+ rspec (~> 3.0)
51
+
52
+ BUNDLED WITH
53
+ 1.16.1
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # Ecoportal::API
2
+
3
+ This is a basic collection of helpers that can be used to interact with the
4
+ [ecoPortal](https://www.ecoportal.com) API.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'ecoportal-api', require: %w[ecoportal/api]
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install ecoportal-api
21
+
22
+ ## Usage
23
+
24
+ TODO
25
+
26
+ ## Development
27
+
28
+ 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.
29
+
30
+ 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).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ecoportal/ecoportal-api.
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ecoportal/api"
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__)
data/bin/setup ADDED
@@ -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,28 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "ecoportal/api/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ecoportal-api"
8
+ spec.version = Ecoportal::API::VERSION
9
+ spec.authors = ["Tapio Saarinen"]
10
+ spec.email = ["tapio@ecoportal.co.nz"]
11
+
12
+ spec.summary = %q{A collection of helpers for interacting with the ecoPortal MS's various APIs}
13
+ spec.homepage = "https://www.ecoportal.com"
14
+ spec.licenses = %w[MIT]
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+
27
+ spec.add_dependency 'http', '~> 3'
28
+ end
@@ -0,0 +1,11 @@
1
+ require "cgi"
2
+ require "ecoportal/api/version"
3
+
4
+ module Ecoportal
5
+ module API
6
+ end
7
+ end
8
+
9
+ require "ecoportal/api/common"
10
+ require "ecoportal/api/v1"
11
+ require "ecoportal/api/internal"
@@ -0,0 +1,13 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ end
5
+ end
6
+ end
7
+ require 'ecoportal/api/common/client'
8
+ require 'ecoportal/api/common/response'
9
+ require 'ecoportal/api/common/wrapped_response'
10
+ require 'ecoportal/api/common/base_model'
11
+ require 'ecoportal/api/common/batch_operation'
12
+ require 'ecoportal/api/common/batch_response'
13
+ require 'ecoportal/api/common/hash_diff'
@@ -0,0 +1,39 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ class BaseModel
5
+ attr_reader :doc
6
+ def initialize(doc = {})
7
+ @doc = doc
8
+ @original_doc = JSON.parse(doc.to_json)
9
+ end
10
+
11
+ def print
12
+ puts JSON.pretty_generate(as_json)
13
+ self
14
+ end
15
+
16
+ def as_json
17
+ doc
18
+ end
19
+
20
+ def as_update
21
+ new_doc = as_json
22
+ Common::HashDiff.diff(new_doc, @original_doc)
23
+ end
24
+
25
+ def self.passthrough(*methods, to:)
26
+ methods.each do |method|
27
+ method = method.to_s
28
+ define_method method do
29
+ send(to)[method]
30
+ end
31
+ define_method "#{method}=" do |value|
32
+ send(to)[method] = value
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,94 @@
1
+ require 'ecoportal/api/common/doc_helpers'
2
+ module Ecoportal
3
+ module API
4
+ module Common
5
+ class BatchOperation
6
+ include Common::DocHelpers
7
+
8
+ def initialize(base_path, wrapper)
9
+ @base_path = base_path
10
+ @wrapper = wrapper
11
+ @operations = []
12
+ end
13
+
14
+ def as_json
15
+ {
16
+ actions: @operations.map do |operation|
17
+ operation.slice(:path, :method, :body)
18
+ end
19
+ }
20
+ end
21
+
22
+ def process_response(response)
23
+ if response.success?
24
+ response.body.each.with_index do |subresponse, idx|
25
+ next unless callback = @operations[idx][:callback]
26
+
27
+ status = subresponse["status"]
28
+ body = subresponse["response"]
29
+ method = @operations[idx][:method]
30
+
31
+ batch_response = BatchResponse.new(status, body)
32
+ if batch_response.success? && method == "GET"
33
+ callback.call response, @wrapper.new(body)
34
+ else
35
+ callback.call batch_response
36
+ end
37
+ end
38
+ else
39
+ raise "Total failure in batch operation"
40
+ end
41
+ end
42
+
43
+ def get(id)
44
+ @operations << {
45
+ path: @base_path + "/" + CGI::escape(id),
46
+ method: "GET",
47
+ callback: block_given? && Proc.new
48
+ }
49
+ end
50
+
51
+ def update(doc)
52
+ id = get_id(doc)
53
+ body = get_body(doc)
54
+ @operations << {
55
+ path: @base_path + "/" + CGI::escape(id),
56
+ method: "PATCH",
57
+ body: body,
58
+ callback: block_given? && Proc.new
59
+ }
60
+ end
61
+
62
+ def upsert(doc)
63
+ id = get_id(doc)
64
+ body = get_body(doc)
65
+ @operations << {
66
+ path: @base_path + "/" + CGI::escape(id),
67
+ method: "POST",
68
+ body: body,
69
+ callback: block_given? && Proc.new
70
+ }
71
+ end
72
+
73
+ def delete(doc)
74
+ id = get_id(doc)
75
+ @operations << {
76
+ path: @base_path + "/" + CGI::escape(id),
77
+ method: "DELETE",
78
+ callback: block_given? && Proc.new
79
+ }
80
+ end
81
+
82
+ def create(doc)
83
+ body = get_body(doc)
84
+ @operations << {
85
+ path: @base_path,
86
+ method: "POST",
87
+ body: body,
88
+ callback: block_given? && Proc.new
89
+ }
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,16 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ class BatchResponse
5
+ attr_reader :status, :body
6
+ def initialize(status, body)
7
+ @status = status
8
+ @body = body
9
+ end
10
+ def success?
11
+ (0..299).include?(status)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,58 @@
1
+ require 'http'
2
+ module Ecoportal
3
+ module API
4
+ module Common
5
+ class Client
6
+ def initialize(api_key:, version: "v1", host: "live.ecoportal.com")
7
+ @version = version
8
+ @api_key = api_key
9
+ if host.match(/^localhost|^127\.0\.0\.1/)
10
+ @base_uri = "http://#{host}/api/"
11
+ else
12
+ @base_uri = "https://#{host}/api/"
13
+ end
14
+ end
15
+
16
+ def get(path, params: {})
17
+ request do |http|
18
+ http.get(url_for(path), params: params)
19
+ end
20
+ end
21
+
22
+ def post(path, data:)
23
+ request do |http|
24
+ http.post(url_for(path), json: data)
25
+ end
26
+ end
27
+
28
+ def patch(path, data:)
29
+ request do |http|
30
+ http.patch(url_for(path), json: data)
31
+ end
32
+ end
33
+
34
+ def delete(path)
35
+ request do |http|
36
+ http.delete(url_for(path))
37
+ end
38
+ end
39
+
40
+ def request
41
+ wrap_response yield(base_request)
42
+ end
43
+
44
+ def wrap_response(response)
45
+ Ecoportal::API::Common::Response.new(response)
46
+ end
47
+
48
+ def base_request
49
+ @base_request ||= HTTP.headers("X-ApiKey" => @api_key).accept(:json)
50
+ end
51
+
52
+ def url_for(path)
53
+ @base_uri+@version+path
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,27 @@
1
+ module Ecoportal
2
+ module API
3
+ module Common
4
+ module DocHelpers
5
+ def get_body(doc)
6
+ if doc.respond_to?(:as_update)
7
+ doc.as_update
8
+ elsif doc.respond_to?(:as_json)
9
+ doc.as_json
10
+ else
11
+ doc
12
+ end
13
+ end
14
+ def get_id(doc)
15
+ id = nil
16
+ id ||= doc.id if doc.respond_to?(:id)
17
+ id ||= doc.external_id if doc.respond_to?(:external_id)
18
+ id ||= doc["id"] if doc.is_a?(Hash)
19
+ id ||= doc["external_id"] if doc.is_a?(Hash)
20
+ id ||= doc if doc.is_a?(String)
21
+ id or raise "No ID has been given!"
22
+ id
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end