ruby-montage 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.ruby-version +1 -0
  4. data/CODE_OF_CONDUCT.md +13 -0
  5. data/Gemfile +6 -0
  6. data/LICENSE.txt +21 -0
  7. data/README.md +132 -0
  8. data/Rakefile +12 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +7 -0
  11. data/circle.yml +13 -0
  12. data/lib/montage.rb +6 -0
  13. data/lib/montage/client.rb +109 -0
  14. data/lib/montage/client/documents.rb +79 -0
  15. data/lib/montage/client/files.rb +39 -0
  16. data/lib/montage/client/schemas.rb +19 -0
  17. data/lib/montage/collection.rb +43 -0
  18. data/lib/montage/collections.rb +21 -0
  19. data/lib/montage/collections/documents.rb +13 -0
  20. data/lib/montage/collections/errors.rb +13 -0
  21. data/lib/montage/collections/files.rb +13 -0
  22. data/lib/montage/collections/schemas.rb +13 -0
  23. data/lib/montage/errors.rb +5 -0
  24. data/lib/montage/operators.rb +34 -0
  25. data/lib/montage/operators/equal.rb +17 -0
  26. data/lib/montage/operators/gt.rb +17 -0
  27. data/lib/montage/operators/gte.rb +17 -0
  28. data/lib/montage/operators/ilike.rb +17 -0
  29. data/lib/montage/operators/in.rb +17 -0
  30. data/lib/montage/operators/like.rb +17 -0
  31. data/lib/montage/operators/lt.rb +17 -0
  32. data/lib/montage/operators/lte.rb +17 -0
  33. data/lib/montage/operators/nil.rb +17 -0
  34. data/lib/montage/operators/not.rb +17 -0
  35. data/lib/montage/operators/not_in.rb +17 -0
  36. data/lib/montage/query.rb +107 -0
  37. data/lib/montage/query_parser.rb +112 -0
  38. data/lib/montage/resource.rb +47 -0
  39. data/lib/montage/resources.rb +25 -0
  40. data/lib/montage/resources/document.rb +9 -0
  41. data/lib/montage/resources/error.rb +9 -0
  42. data/lib/montage/resources/file.rb +9 -0
  43. data/lib/montage/resources/schema.rb +9 -0
  44. data/lib/montage/resources/token.rb +13 -0
  45. data/lib/montage/response.rb +49 -0
  46. data/lib/montage/support.rb +25 -0
  47. data/lib/montage/version.rb +3 -0
  48. data/ruby-montage.gemspec +36 -0
  49. metadata +251 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6cd914f1ed0f45c379b03e7d74c6730202efcfd6
4
+ data.tar.gz: 33cedf4412dd2baee765919a1971d6be3c776af0
5
+ SHA512:
6
+ metadata.gz: 0fd3016e5a69c1b39cf5097b7cea6088fdc40350e6340095db359de273a63dd8e418b746825191fc0538fb0b00ca1ad6753e8fbd2e325c11b5ff7cbc6d710a4b
7
+ data.tar.gz: f0a366156d9011c20fc000fce2c02a6401c84b040a8189e04560772192a1b39ab310beaf0a89e64f6869a2832eebfcc836e198a41500a75994423b52298f832c
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea
11
+ .env
12
+ tmp.json
13
+ ruby-montage-*.gem
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-montage.gemspec
4
+ gemspec
5
+
6
+ gem "pry"
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 dphaener
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.
data/README.md ADDED
@@ -0,0 +1,132 @@
1
+ [![Code Climate](https://codeclimate.com/github/EditLLC/ruby-montage/badges/gpa.svg)](https://codeclimate.com/github/EditLLC/ruby-montage)
2
+ [![Circle CI](https://circleci.com/gh/EditLLC/ruby-montage/tree/master.svg?style=svg)](https://circleci.com/gh/EditLLC/ruby-montage/tree/master)
3
+ [![codecov.io](http://codecov.io/github/EditLLC/ruby-montage/coverage.svg?branch=master)](http://codecov.io/github/EditLLC/ruby-montage?branch=master)
4
+
5
+ # Ruby Montage
6
+
7
+ A Ruby wrapper for the [Montage](https://www.foo.com) API
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ruby-montage', require: 'montage'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install ruby-montage
24
+
25
+ ## Usage
26
+
27
+ In order to use the API, you will first have to retrieve an authorization token
28
+
29
+ ```ruby
30
+ client = Montage::Client.new do |c|
31
+ c.username = "YOUR_USERNAME"
32
+ c.password = "YOUR_PASSWORD"
33
+ c.api_version # Optional, defaults to 1
34
+ c.domain = "test" # Your Montage subdomain
35
+ end
36
+
37
+ response = client.auth
38
+ token = response.token.value
39
+ ```
40
+
41
+ This token does not expire, so it is recommended that you store this token somewhere in your database to avoid having to
42
+ make an API call to retrieve it on every request.
43
+
44
+ After retrieving a token, you can simply use the client to perform API requests:
45
+
46
+ ```ruby
47
+ client.schemas
48
+ ```
49
+
50
+ Or, use the same token later to initialize a new instance of the client:
51
+
52
+ ```ruby
53
+ client = Montage::Client.new do |c|
54
+ c.token = "YOUR_TOKEN"
55
+ c.domain = "test" # Your Montage subdomain
56
+ end
57
+ ```
58
+
59
+ All API actions are available as methods on the client object. The following methods are available:
60
+
61
+ ## Schemas
62
+
63
+ | Action | Method |
64
+ | :------------------------------ | :-------------------------------------------------- |
65
+ | Get a list of schemas | `#schemas` |
66
+ | Fetch a single schema | `#schema(name)` |
67
+
68
+ ## Documents
69
+
70
+ | Action | Method |
71
+ | :------------------------------ | :-------------------------------------------------- |
72
+ | Query documents in a schema | `#documents(schema, query: query)` |
73
+ | Fetch a single document | `#document(schema, document_uuid)` |
74
+ | Fetch the next set of documents | `#document_cursor(schema, cursor)` |
75
+ | Delete a document | `#delete_document(schema, document_uuid)` |
76
+ | Create or update a set of documents | `#create_or_update_documents(schema,documents)` |
77
+
78
+
79
+ ## Operators
80
+
81
+ | Operator | Montage Equivalent |
82
+ | :-----------| :--------- |
83
+ | != | __not |
84
+ | >= | __gte |
85
+ | <= | __lte |
86
+ | = | |
87
+ | > | __gt |
88
+ | < | __lt |
89
+ | not in | __notin |
90
+ | in | __in |
91
+ | not in | __notin |
92
+ | like | __contains |
93
+ | ilike | __icontains |
94
+
95
+
96
+ ## The Query Object
97
+
98
+ The Montage API requires a JSON serialized query object to query a schema
99
+
100
+ {
101
+ "limit": 10,
102
+ "offset": 10,
103
+ "order_by": "foo",
104
+ "ordering": "asc",
105
+ "filter": {
106
+ ...
107
+ }
108
+ }
109
+
110
+ The Ruby Montage API wrapper provides a query object that has a DSL similar to that of a Rails ActiveRecord object:
111
+
112
+ ```ruby
113
+ query = Montage::Query.new
114
+ query = query.where("foo > 5").limit(10).order(foo: :asc)
115
+ ```
116
+
117
+ This query object can now be passed into an API call to documents to retrieve a set of documents:
118
+
119
+ ```ruby
120
+ c.documents("movies", query)
121
+ ```
122
+
123
+ Using this query object is not required in order to complete a request. Any object that responds to `.to_json` and
124
+ returns an object that conforms to the above query standards will work. This is merely provided as a convenience.
125
+
126
+ ## Contributing
127
+
128
+ 1. Fork it ( https://github.com/[my-github-username]/ruby-montage/fork )
129
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
130
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
131
+ 4. Push to the branch (`git push origin my-new-feature`)
132
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require "rake/testtask"
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "lib"
7
+ t.pattern = "test/**/*_test.rb"
8
+ t.verbose = true
9
+ end
10
+
11
+ desc "Run tests"
12
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "montage"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,13 @@
1
+ dependencies:
2
+ override:
3
+ - 'rvm-exec 1.9.3-p551 bundle install'
4
+ - 'rvm-exec 2.0.0-p598 bundle install'
5
+ - 'rvm-exec 2.1.5 bundle install'
6
+ - 'rvm-exec 2.2.0 bundle install'
7
+
8
+ test:
9
+ override:
10
+ - 'CI=true rvm-exec 1.9.3-p551 bundle exec rake'
11
+ - 'CI=true rvm-exec 2.0.0-p598 bundle exec rake'
12
+ - 'CI=true rvm-exec 2.1.5 bundle exec rake'
13
+ - 'CI=true rvm-exec 2.2.0 bundle exec rake'
data/lib/montage.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'montage/version'
2
+ require 'montage/client'
3
+ require 'montage/query'
4
+
5
+ module Montage
6
+ end
@@ -0,0 +1,109 @@
1
+ require 'montage/response'
2
+ require 'faraday'
3
+ require 'faraday_middleware'
4
+ require 'json'
5
+ require 'montage/client/files'
6
+ require 'montage/client/schemas'
7
+ require 'montage/client/documents'
8
+ require 'montage/errors'
9
+
10
+ module Montage
11
+ class Client
12
+ include Files
13
+ include Schemas
14
+ include Documents
15
+
16
+ attr_accessor :token, :username, :password, :domain, :api_version
17
+
18
+ def initialize
19
+ @api_version = 1
20
+ yield(self) if block_given?
21
+ raise MissingAttributeError, "You must declare the domain attribute" unless @domain
22
+ end
23
+
24
+ def content_type
25
+ "application/json"
26
+ end
27
+
28
+ def auth
29
+ build_response("token") do
30
+ connection.post do |req|
31
+ req.headers.delete("Authorization")
32
+ req.url "auth/"
33
+ req.body = { username: username, password: password }.to_json
34
+ end
35
+ end
36
+ end
37
+
38
+ def get(url, resource_name, options = {})
39
+ build_response(resource_name) do
40
+ connection.get do |req|
41
+ req.url url
42
+ req.params = options
43
+ end
44
+ end
45
+ end
46
+
47
+ def post(url, resource_name, options = {})
48
+ build_response(resource_name) do
49
+ connection.post do |req|
50
+ req.url url
51
+ req.body = options.to_json
52
+ end
53
+ end
54
+ end
55
+
56
+ def put(url, resource_name, options = {})
57
+ build_response(resource_name) do
58
+ connection.put do |req|
59
+ req.url url
60
+ req.body = options.to_json
61
+ end
62
+ end
63
+ end
64
+
65
+ def delete(url, resource_name, options = {})
66
+ build_response(resource_name) do
67
+ connection.delete do |req|
68
+ req.url url
69
+ req.body = options.to_json
70
+ end
71
+ end
72
+ end
73
+
74
+ def set_token(token)
75
+ @token = token
76
+ connection.headers["Authorization"] = "Token #{token}"
77
+ end
78
+
79
+ def response_successful?(response)
80
+ return false if response.body["errors"]
81
+ response.success?
82
+ end
83
+
84
+ def build_response(resource_name, &block)
85
+ response = yield
86
+ resource = response_successful?(response) ? resource_name : "error"
87
+
88
+ response_object = Montage::Response.new(response.status, response.body, resource)
89
+
90
+ if resource_name == "token" && response.success?
91
+ set_token(response_object.token.value)
92
+ end
93
+
94
+ response_object
95
+ end
96
+
97
+ def connection
98
+ @connect ||= Faraday.new do |f|
99
+ f.adapter :net_http
100
+ f.url_prefix = "http://#{domain}.dev.montagehot.club/api/v#{api_version}/"
101
+ f.headers["User-Agent"] = "Montage Ruby v#{Montage::VERSION}"
102
+ f.headers["Content-Type"] = content_type
103
+ f.headers["Accept"] = "*/*"
104
+ f.headers["Authorization"] = "Token #{token}"
105
+ f.response :json, content_type: /\bjson$/
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,79 @@
1
+ module Montage
2
+ class Client
3
+ module Documents
4
+ # Public: Get a list of documents
5
+ #
6
+ # Params:
7
+ # schema - *Required* The name of the schema to run the query against
8
+ # query - *Optional* A Montage::Query object to pass along with the request
9
+ #
10
+ # Returns a Montage::Response
11
+ #
12
+ def documents(schema, query = {})
13
+ post("schemas/#{schema}/query/", "document", query)
14
+ end
15
+
16
+ # Public: Fetch a document
17
+ #
18
+ # Params:
19
+ # schema - *Required* The name of the schema to fetch the document from
20
+ # document_uuid - *Required* The uuid of the document to fetch
21
+ #
22
+ # Returns a Montage::Response
23
+ #
24
+ def document(schema, document_uuid)
25
+ get("schemas/#{schema}/#{document_uuid}/", "document")
26
+ end
27
+
28
+ # Public: Get the set of documents for the given cursor
29
+ #
30
+ # Params:
31
+ # schema - *Required* The name of the schema to run the query against
32
+ # cursor - *Required* The cursor that was returned in the last document API call
33
+ #
34
+ # Returns a Montage::Response
35
+ #
36
+ # def document_cursor(schema, cursor)
37
+ # get("schemas/#{schema}/?cursor=#{cursor}", "document")
38
+ # end
39
+
40
+ # Public: Create or update a set of documents
41
+ #
42
+ # Params:
43
+ # schema - *Required* The name of the schema to add the document to
44
+ # document - *Required* The Hash representation of the document you would like to create
45
+ #
46
+ # The document must conform to the schema definition
47
+ #
48
+ # Returns a Montage::Response
49
+ def create_or_update_documents(schema, documents)
50
+ post("schemas/#{schema}/save/", "document", documents)
51
+ end
52
+
53
+ # Public: Update a document
54
+ #
55
+ # Params:
56
+ # schema - *Required* The name of the schema the document belongs to
57
+ # document_uuid - *Required* The uuid of the document you wish to update
58
+ # document - *Required* The Hash representation of the document you would like to update
59
+ #
60
+ # The document must conform to the schema definition
61
+ #
62
+ # Returns a Montage::Response
63
+ def update_document(schema, document)
64
+ post("schemas/#{schema}/save/", "document", document)
65
+ end
66
+
67
+ # Public: Delete a document
68
+ #
69
+ # Params:
70
+ # schema - *Required* The name of the schema the document belongs to
71
+ # document_uuid - *Required* The uuid of the document you widh to delete
72
+ #
73
+ # Returns a Montage::Response
74
+ def delete_document(schema, document_uuid)
75
+ delete("schemas/#{schema}/#{document_uuid}/", "document")
76
+ end
77
+ end
78
+ end
79
+ end