omcms-ruby-client 1.0.0.pre.alpha

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5deece1d451527de6ac82ca314828492c15fcfa0fbb940144d86819d3a913ee7
4
+ data.tar.gz: d50b85197535e0d418ff0c8daf3b750e3c0843dd488dc80123658e8a7362312a
5
+ SHA512:
6
+ metadata.gz: a94d610dae99e9c644af1f572699d4db310a81a660dd39f77313d8daa1743ed1da3105ec841103fa6b80840bf96187b4cb597bb454cbabc1f920aa9830588a7b
7
+ data.tar.gz: 69e4a929a5a5841dabfd50118afdcee8732615dc98db7d72169bace4d3d6583066b67df9b444e270c0b04fba5a95a88dbd5cd894afb46df8a18ba3925563609f
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
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.1
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in omcms-ruby-client.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # OMCMS
2
+
3
+ This gem is a ruby wrapper of OMCMS API's
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'omcms-ruby-client'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install omcms-ruby-client
20
+
21
+ ## Usage
22
+
23
+ Create a client using your OMCMS connector's public key and private key
24
+
25
+ ```ruby
26
+ # config/initializers/omcms.rb
27
+
28
+ $omcms = OMCMS::Client.new(
29
+ host: ENV["OMCMS_API_HOST"], # http://localhost:8877
30
+ public_key: ENV["OMCMS_PUBLIC_KEY"],
31
+ private_key: ENV["OMCMS_PRIVATE_KEY"]
32
+ )
33
+ ```
34
+
35
+ ### Offering
36
+
37
+ You can fetch all or specific offering with `OMCMS::Client`
38
+
39
+ ```ruby
40
+ offerings = $omcms.offerings.all
41
+ # => fetch all offerings available on OMCMS
42
+
43
+ offering = $omcms.offerings.get(123)
44
+ # => offering(id = 123) fetched from OMCMS
45
+ ```
46
+
47
+ ### Component
48
+
49
+ You can fetch all or specific component with specified `offering`
50
+
51
+ ```ruby
52
+ offering = $omcms.offerings.get(123)
53
+
54
+ components = offering.components.all
55
+ # => all components of offering(id = 123)
56
+
57
+ component = offering.components.get(456)
58
+ # => component(id = 456) of offering(id = 123)
59
+ ```
60
+
61
+ ### Offering Data
62
+
63
+ You can fetch all or specific offering_data with specified `offering`
64
+
65
+ ```ruby
66
+ offering = $omcms.offerings.get(123)
67
+
68
+ offering_data = offering.data.all
69
+ # => all offering_data of offering(id = 123)
70
+
71
+ offering_data = offering.data.get(456)
72
+ # => offering_data(id: 456) of offering(id = 123)
73
+ ```
74
+
75
+ ## Development
76
+
77
+ 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.
78
+
79
+ 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).
80
+
81
+ ## Contributing
82
+
83
+ Bug reports and pull requests are welcome on GitHub at https://github.com/equitymultiple/omcms-ruby-client.
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 "omcms"
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,42 @@
1
+ module OMCMS
2
+ class Client
3
+ attr_reader :client
4
+
5
+ def initialize(opts)
6
+ configure_credentials(opts)
7
+ configure_client
8
+ end
9
+
10
+ def faraday &block
11
+ @faraday = block if block
12
+ @faraday
13
+ end
14
+
15
+ def configure_client
16
+ @client ||= Faraday.new do |f|
17
+ f.request :basic_auth, @username, @password
18
+ f.use SetHeader
19
+ f.use ParseResponse
20
+ f.response :json, :content_type => /\bjson$/
21
+ faraday.call(f) if faraday
22
+ f.adapter Faraday.default_adapter unless faraday
23
+ end
24
+ end
25
+
26
+ def offerings
27
+ OMCMS::Offering.new(@client, @response, @host)
28
+ end
29
+
30
+ private
31
+
32
+ def configure_credentials(opts)
33
+ raise ArgumentError.new "Public Key is missing for the OMCMS client" if opts[:public_key].to_s.empty?
34
+ raise ArgumentError.new "Private Key is missing for the OMCMS client" if opts[:private_key].to_s.empty?
35
+ raise ArgumentError.new "Host is missing for the OMCMS client" if opts[:host].to_s.empty?
36
+
37
+ @username = opts[:public_key]
38
+ @password = opts[:private_key]
39
+ @host = opts[:host]
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,17 @@
1
+ module OMCMS
2
+ class ParseResponse
3
+ def initialize app
4
+ @app = app
5
+ end
6
+
7
+ def call request_env
8
+ @app.call(request_env).on_complete do |response_env|
9
+ if response_env.status >= 400
10
+ return OMCMS::Response::Error.new(response_env)
11
+ else
12
+ return response_env.body
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,16 @@
1
+ module OMCMS
2
+ class SetHeader
3
+ HEADERS = {
4
+ :"user-agent" => "omcms/ruby/client/#{VERSION}"
5
+ }
6
+
7
+ def initialize app
8
+ @app = app
9
+ end
10
+
11
+ def call request_env
12
+ request_env[:request_headers].merge! HEADERS
13
+ @app.call(request_env)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module OMCMS
2
+ class Component < Resource
3
+ def initialize(client, response = {}, host)
4
+ @offering_id = response["id"]
5
+ super
6
+ end
7
+
8
+ def all
9
+ perform_run self, request_path()
10
+ end
11
+
12
+ def get(id)
13
+ perform_run self, request_path(id)
14
+ end
15
+
16
+ private
17
+
18
+ def request_path(key = nil)
19
+ ["offerings", @offering_id, "components", key].compact.join("/")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ module OMCMS
2
+ class Offering < Resource
3
+ def initialize(client, response, host)
4
+ super
5
+ end
6
+
7
+ def all
8
+ perform_run self, request_path()
9
+ end
10
+
11
+ def get(id)
12
+ perform_run self, request_path(id)
13
+ end
14
+
15
+ private
16
+
17
+ def request_path(key = nil)
18
+ ["offerings", key].compact.join("/")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ module OMCMS
2
+ class OfferingData < Resource
3
+ def initialize(client, response = {}, host)
4
+ @offering_id = response["id"]
5
+ super
6
+ end
7
+
8
+ def all
9
+ perform_run self, request_path()
10
+ end
11
+
12
+ def get(id)
13
+ perform_run self, request_path(id)
14
+ end
15
+
16
+ private
17
+
18
+ def request_path(key = nil)
19
+ ["offerings", @offering_id, "data", key].compact.join("/")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ module OMCMS
2
+ class Resource
3
+ def initialize(client, response = {}, host = nil)
4
+ @client = client
5
+ @host = host
6
+ end
7
+
8
+ private
9
+
10
+ def response_class(name)
11
+ Kernel.const_get("OMCMS::Response::#{name}")
12
+ end
13
+
14
+ def perform_run(instance, path)
15
+ request_url = [@host, "api", "apps", path].compact.join("/")
16
+ class_name = instance.class.name.split("::").last
17
+ response = @client.get request_url
18
+
19
+ return response if response.class == OMCMS::Response::Error
20
+ response_class(class_name).new(@client, response, @host)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ module OMCMS
2
+ module Response
3
+ class Body
4
+ attr_reader :body
5
+
6
+ def initialize(client, response, host)
7
+ @client = client
8
+ @body = response
9
+ @host = host
10
+ end
11
+
12
+ def to_json
13
+ parse(@body)
14
+ end
15
+
16
+ private
17
+
18
+ def parse(data = "")
19
+ if data.class == Hash
20
+ return data.keys.map do |attribute|
21
+ key = snakecase(attribute)
22
+ [key, parse(data[attribute])]
23
+ end.to_h
24
+ elsif data.class == Array
25
+ return data.map do |object|
26
+ hash = object.keys.map do |attribute|
27
+ key = snakecase(attribute)
28
+ [key, parse(object[attribute])]
29
+ end.to_h
30
+ end
31
+ end
32
+ return data
33
+ end
34
+
35
+
36
+ def snakecase(data = "")
37
+ data.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
38
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
39
+ .tr('-', '_')
40
+ .gsub(/\s/, '_')
41
+ .gsub(/__+/, '_')
42
+ .downcase
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ module OMCMS
2
+ module Response
3
+ class Component < Body
4
+ def initialize(client, data, host)
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ module OMCMS
2
+ module Response
3
+ class Error < StandardError
4
+ attr_reader :status
5
+
6
+ def initialize response
7
+ return unless response
8
+ unless response.respond_to? :body
9
+ response = add_body_to_error(response)
10
+ end
11
+
12
+ response.body.each do |key, value|
13
+ instance_variable_set("@#{key}", value)
14
+ self.class.send(:attr_reader, key)
15
+ end
16
+ end
17
+
18
+ def to_json
19
+ self.instance_variables.map do |attribute|
20
+ key = attribute.to_s.gsub('@','')
21
+ [snakecase(key), self.instance_variable_get(attribute)]
22
+ end.to_h
23
+ end
24
+
25
+ private
26
+
27
+ def add_body_to_error(response)
28
+ OpenStruct.new(
29
+ body: {
30
+ statusCode: 400,
31
+ name: "Bad Request",
32
+ message: response
33
+ }
34
+ )
35
+ end
36
+
37
+ def snakecase(data = "")
38
+ data.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
39
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
40
+ .tr('-', '_')
41
+ .gsub(/\s/, '_')
42
+ .gsub(/__+/, '_')
43
+ .downcase
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ module OMCMS
2
+ module Response
3
+ class Offering < Body
4
+ def initialize(client, data, host)
5
+ super
6
+ end
7
+
8
+ def components
9
+ OMCMS::Component.new(@client, self.body, @host)
10
+ end
11
+
12
+ def data
13
+ OMCMS::OfferingData.new(@client, self.body, @host)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module OMCMS
2
+ module Response
3
+ class OfferingData < Body
4
+ def initialize(client, data, host)
5
+ super
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module OMCMS
2
+ VERSION = "1.0.0-alpha"
3
+ end
data/lib/omcms.rb ADDED
@@ -0,0 +1,22 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+
4
+ require "omcms/client"
5
+ require "omcms/resource"
6
+ require "omcms/version"
7
+
8
+ require "omcms/resource/offering"
9
+ require "omcms/resource/component"
10
+ require "omcms/resource/offering_data"
11
+
12
+ require "omcms/response/body"
13
+ require "omcms/response/error"
14
+ require "omcms/response/offering"
15
+ require "omcms/response/component"
16
+ require "omcms/response/offering_data"
17
+
18
+ require "omcms/middleware/set_header"
19
+ require "omcms/middleware/parse_response"
20
+
21
+ module OMCMS
22
+ end
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "omcms/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "omcms-ruby-client"
7
+ spec.version = OMCMS::VERSION
8
+ spec.authors = ["Pradeep Rawat"]
9
+ spec.email = ["pradeep@akaruilabs.com"]
10
+
11
+ spec.summary = %q{A Ruby SDK for OMCMS APIs}
12
+ spec.description = %q{omcms-ruby-client make is simpler to use OMCMS connector APIs and integrate with existing Ruby application}
13
+ spec.homepage = "https://github.com/equitymultiple/omcms-ruby-client"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = "https://github.com/equitymultiple/omcms-ruby-client"
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+ spec.bindir = "exe"
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ["lib"]
26
+
27
+ spec.add_dependency "faraday", "~> 0.15"
28
+ spec.add_dependency "faraday_middleware", "~> 0.13"
29
+
30
+ spec.add_development_dependency "bundler", "~> 2.0"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omcms-ruby-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.pre.alpha
5
+ platform: ruby
6
+ authors:
7
+ - Pradeep Rawat
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.13'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.13'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description: omcms-ruby-client make is simpler to use OMCMS connector APIs and integrate
84
+ with existing Ruby application
85
+ email:
86
+ - pradeep@akaruilabs.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/omcms.rb
100
+ - lib/omcms/client.rb
101
+ - lib/omcms/middleware/parse_response.rb
102
+ - lib/omcms/middleware/set_header.rb
103
+ - lib/omcms/resource.rb
104
+ - lib/omcms/resource/component.rb
105
+ - lib/omcms/resource/offering.rb
106
+ - lib/omcms/resource/offering_data.rb
107
+ - lib/omcms/response/body.rb
108
+ - lib/omcms/response/component.rb
109
+ - lib/omcms/response/error.rb
110
+ - lib/omcms/response/offering.rb
111
+ - lib/omcms/response/offering_data.rb
112
+ - lib/omcms/version.rb
113
+ - omcms-ruby-client.gemspec
114
+ homepage: https://github.com/equitymultiple/omcms-ruby-client
115
+ licenses: []
116
+ metadata:
117
+ homepage_uri: https://github.com/equitymultiple/omcms-ruby-client
118
+ source_code_uri: https://github.com/equitymultiple/omcms-ruby-client
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">"
131
+ - !ruby/object:Gem::Version
132
+ version: 1.3.1
133
+ requirements: []
134
+ rubygems_version: 3.0.6
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: A Ruby SDK for OMCMS APIs
138
+ test_files: []