plate_api 0.4.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 516481a1cbc16f2e26dcd8319b5b682c4f729217ff1f21f48f885ea7ca3f6f74
4
- data.tar.gz: 6a5c9663a2185531bbb67b0f2c6a1c55941359ec233a14957ae7855ee83ed2a9
3
+ metadata.gz: 47fb3d26ae744fae7daf830907607587c1978be77029fa8d4cade526edeeccfe
4
+ data.tar.gz: 2f3e0817b62ee8f7bd344785adc353b12fb9189c539925ba7da7f181ede52246
5
5
  SHA512:
6
- metadata.gz: 2a1894b50e1398d7ada99a89411a51ceadac86c564b3b6556c3491bfcd6c40a1058490d9f6e5dc360b917f238589e312ceb3d255c0b57f2cfdbbd447905be8ae
7
- data.tar.gz: 6a71e32bfa9e55a080a8c06286b3c48b9d82c26961bcf280b69da1005309b409d2f3d1c889839053eb134b0173af060ebd8def0d959b4585711656493897f920
6
+ metadata.gz: b8a9bba6f6bc0582813b0df46d5a72d3f5de015e4bb5dcbf66bcd52fc043bb354db6bba5e9d2379160f0fbc96c3077d43914094ef8d3c09572381a1b8e4e8788
7
+ data.tar.gz: f66601a98fad886c26a10106697ea5c0632f423513581fce78431cfa094d97e980e04b83acb89a8a0917d90041ebde34c28cf7b215a1b2afcd7f6c9bc166b544
data/.byebug_history ADDED
@@ -0,0 +1,43 @@
1
+ c
2
+ attributes
3
+ m
4
+ c
5
+ attributes
6
+ m
7
+ c;m
8
+ m
9
+ c
10
+ m
11
+ c
12
+ attributes
13
+ s
14
+ up
15
+ s
16
+ up
17
+ s
18
+ new_object.attributes
19
+ c
20
+ new_object.attributes
21
+ new_object
22
+ c
23
+ full_path
24
+ c
25
+ n
26
+ val.is_a? File
27
+ val
28
+ c
29
+ val.is_a? File
30
+ val
31
+ parameters
32
+ c
33
+ site.id
34
+ site = subject.find(5)
35
+ c
36
+ site.class.superclass
37
+ site.class.methods
38
+ site.class
39
+ site.methods.class
40
+ site.methods - Object.methods
41
+ site.methods
42
+ site.id
43
+ site = subject.find(5)
data/.gitignore CHANGED
@@ -9,3 +9,5 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ coverage
13
+ /secrets
data/.travis.yml CHANGED
@@ -1,5 +1,16 @@
1
+
2
+ env:
3
+ global:
4
+ - CC_TEST_REPORTER_ID=5ad5ab58c1dde153aed2f4ed1ca671baa6d34530eb18a0c421fcb7bab89338e6
1
5
  sudo: false
2
6
  language: ruby
3
7
  rvm:
4
8
  - 2.5.1
5
9
  before_install: gem install bundler -v 1.16.1
10
+ before_script:
11
+ - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
12
+ - chmod +x ./cc-test-reporter
13
+ - ./cc-test-reporter before-build
14
+ script: bundle exec rake spec
15
+ after_script:
16
+ - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
data/Gemfile.lock CHANGED
@@ -1,19 +1,43 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- plate_api (0.4.0)
4
+ plate_api (1.0.0)
5
5
  faraday (~> 0.15.4)
6
6
  faraday_middleware (~> 0.13.1)
7
+ mimemagic (~> 0.3.3)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
12
+ activesupport (5.2.2)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ addressable (2.6.0)
18
+ public_suffix (>= 2.0.2, < 4.0)
19
+ ansi (1.5.0)
20
+ byebug (11.0.0)
21
+ concurrent-ruby (1.1.4)
22
+ crack (0.4.3)
23
+ safe_yaml (~> 1.0.0)
11
24
  diff-lcs (1.3)
25
+ docile (1.3.1)
26
+ factory_bot (5.0.1)
27
+ activesupport (>= 4.2.0)
12
28
  faraday (0.15.4)
13
29
  multipart-post (>= 1.2, < 3)
14
30
  faraday_middleware (0.13.1)
15
31
  faraday (>= 0.7.4, < 1.0)
32
+ hashdiff (0.3.8)
33
+ hirb (0.7.3)
34
+ i18n (1.5.3)
35
+ concurrent-ruby (~> 1.0)
36
+ json (2.1.0)
37
+ mimemagic (0.3.3)
38
+ minitest (5.11.3)
16
39
  multipart-post (2.0.0)
40
+ public_suffix (3.0.3)
17
41
  rake (10.5.0)
18
42
  rspec (3.8.0)
19
43
  rspec-core (~> 3.8.0)
@@ -28,15 +52,37 @@ GEM
28
52
  diff-lcs (>= 1.2.0, < 2.0)
29
53
  rspec-support (~> 3.8.0)
30
54
  rspec-support (3.8.0)
55
+ safe_yaml (1.0.5)
56
+ simplecov (0.16.1)
57
+ docile (~> 1.1)
58
+ json (>= 1.8, < 3)
59
+ simplecov-html (~> 0.10.0)
60
+ simplecov-console (0.4.2)
61
+ ansi
62
+ hirb
63
+ simplecov
64
+ simplecov-html (0.10.2)
65
+ thread_safe (0.3.6)
66
+ tzinfo (1.2.5)
67
+ thread_safe (~> 0.1)
68
+ webmock (3.5.1)
69
+ addressable (>= 2.3.6)
70
+ crack (>= 0.3.2)
71
+ hashdiff
31
72
 
32
73
  PLATFORMS
33
74
  ruby
34
75
 
35
76
  DEPENDENCIES
36
77
  bundler (~> 1.16)
78
+ byebug
79
+ factory_bot (~> 5.0.1)
37
80
  plate_api!
38
81
  rake (~> 10.0)
39
82
  rspec (~> 3.0)
83
+ simplecov
84
+ simplecov-console
85
+ webmock (~> 3.0)
40
86
 
41
87
  BUNDLED WITH
42
88
  1.16.1
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
+ ![Gem](https://img.shields.io/gem/dt/plate_api.svg)
2
+ [![Gem Version](https://badge.fury.io/rb/plate_api.svg)](https://badge.fury.io/rb/plate_api)
3
+ [![Build Status](https://travis-ci.com/platehub/plate_api.svg?branch=master)](https://travis-ci.com/platehub/plate_api)
4
+ [![Maintainability](https://api.codeclimate.com/v1/badges/1e1dafadc880983ce63a/maintainability)](https://codeclimate.com/github/platehub/plate_api/maintainability)
5
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/1e1dafadc880983ce63a/test_coverage)](https://codeclimate.com/github/platehub/plate_api/test_coverage)
1
6
  # PlateApi
2
7
 
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/plate_api`. 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
8
+ Welcome to the PlateApi gem. This gem provides a wrapper to communicate with the Plate API, taking care of
9
+ authentication.
6
10
 
7
11
  ## Installation
8
12
 
@@ -20,9 +24,28 @@ Or install it yourself as:
20
24
 
21
25
  $ gem install plate_api
22
26
 
23
- ## Usage
27
+ ## Getting started
28
+
29
+ To get started with a simple connector:
30
+
31
+ ```
32
+ # Initialze a connector
33
+ con == PlateApi::Connector.new("{{public_key}}", "{{secret_key}}")
34
+
35
+ # Find information of a specific site with id == 822
36
+ site = con.sites.find(822)
24
37
 
25
- TODO: Write usage instructions here
38
+ # Find all posts in this site
39
+ posts = site.posts
40
+
41
+ # Find all elements in the first post of this site
42
+ first_post = posts.first
43
+ elements = first_post.elements
44
+
45
+ # Update the content field 'body' of the first element
46
+ element = elements.first
47
+ element.update(body: "<h2>My new text</h2>")
48
+ ```
26
49
 
27
50
  ## Development
28
51
 
@@ -30,10 +53,6 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
30
53
 
31
54
  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
55
 
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/plate_api.
36
-
37
56
  ## License
38
57
 
39
58
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,32 +1,58 @@
1
- require "plate_api/get_request"
2
- require "plate_api/delete_request"
3
- require "plate_api/post_request"
4
- require "plate_api/put_request"
5
-
6
1
  module PlateApi
7
2
  class Connector
8
3
 
9
-
10
4
  def initialize(public_key, secret_key, custom_server=nil)
11
5
  @custom_server = custom_server
12
6
  @public_key = public_key
13
7
  @secret_key = secret_key
8
+ @handling_classes = {}
9
+ end
10
+
11
+ def get(url="", parameters={}, response_type=:json)
12
+ GetRequest.new(@public_key, @secret_key, url, parameters, @custom_server).execute(response_type)
13
+ end
14
+
15
+ def delete(url="", parameters={}, response_type=:json)
16
+ DeleteRequest.new(@public_key, @secret_key, url, parameters, @custom_server).execute(response_type)
14
17
  end
15
18
 
16
- def get(url="", parameters={})
17
- GetRequest.new(@public_key, @secret_key, url, parameters, @custom_server).send
19
+ def put(url="", put_params={}, response_type=:json)
20
+ PutRequest.new(@public_key, @secret_key, url, put_params, @custom_server).execute(response_type)
18
21
  end
19
22
 
20
- def delete(url="", parameters={})
21
- DeleteRequest.new(@public_key, @secret_key, url, parameters, @custom_server).send
23
+ def post(url="", post_params={}, response_type=:json)
24
+ PostRequest.new(@public_key, @secret_key, url, post_params, @custom_server).execute(response_type)
22
25
  end
23
26
 
24
- def put(url="", put_params={})
25
- PutRequest.new(@public_key, @secret_key, url, put_params, @custom_server).send
27
+ def post_multipart(url="", post_params={}, response_type=:json)
28
+ PostMultipartRequest.new(@public_key, @secret_key, url, post_params, @custom_server).execute(response_type)
26
29
  end
27
30
 
28
- def post(url="", post_params={})
29
- PostRequest.new(@public_key, @secret_key, url, post_params, @custom_server).send
31
+ def handler(handled_class)
32
+ @handling_classes[handled_class] ||= ObjectHandler.new(handled_class, self)
30
33
  end
34
+
35
+ def self.plate_object_classes
36
+ {
37
+ sites: PlateApi::PlateObject::Site,
38
+ partners: PlateApi::PlateObject::Partner,
39
+ companies: PlateApi::PlateObject::Company,
40
+ themes: PlateApi::PlateObject::Theme,
41
+ site_translations: PlateApi::PlateObject::SiteTranslation,
42
+ posts: PlateApi::PlateObject::Post,
43
+ sections: PlateApi::PlateObject::Section,
44
+ rows: PlateApi::PlateObject::Row,
45
+ columns: PlateApi::PlateObject::Column,
46
+ elements: PlateApi::PlateObject::Element
47
+ }
48
+ end
49
+
50
+ self.plate_object_classes.each do |k,v|
51
+ define_method(k) do
52
+ return handler(v)
53
+ end
54
+ end
55
+
56
+
31
57
  end
32
58
  end
@@ -1,5 +1,3 @@
1
- require "plate_api/request"
2
-
3
1
  module PlateApi
4
2
  class DeleteRequest < Request
5
3
  def initialize(public_key, secret, path, parameters={}, custom_server=nil)
@@ -1,5 +1,3 @@
1
- require "plate_api/request"
2
-
3
1
  module PlateApi
4
2
  class GetRequest < Request
5
3
  def initialize(public_key, secret, path, parameters={}, custom_server=nil)
@@ -0,0 +1,110 @@
1
+ module PlateApi
2
+ class ObjectHandler
3
+
4
+ attr_reader :api_connector
5
+ attr_reader :handling_class
6
+
7
+ def initialize(handling_class, api_connector)
8
+ raise ArgumentError.new("`handling_class` given for #new is not valid") unless handling_class
9
+ raise ArgumentError.new("`api_connector` given for #new is not valid") unless api_connector
10
+ @handling_class = handling_class
11
+ @api_connector = api_connector
12
+ end
13
+
14
+ def find(id)
15
+ raise ArgumentError.new("`id` given for #find is not valid") unless id
16
+ result = @api_connector.get(resource_path(id))
17
+ if result["data"]
18
+ return new_object(result["data"])
19
+ else
20
+ puts "No result: #{result}"
21
+ return nil
22
+ end
23
+ end
24
+
25
+ def update(id, attributes)
26
+ raise ArgumentError.new("`id` given for #update is not valid") unless id
27
+ raise ArgumentError.new("`attributes` given for #update is not valid") unless attributes.is_a? Hash
28
+ result = @api_connector.put(resource_path(id), {"data" => attributes})
29
+
30
+ if result["data"]
31
+ return new_object(result["data"])
32
+ else
33
+ puts "No result: #{result}"
34
+ return nil
35
+ end
36
+ end
37
+
38
+ def create(parent, attributes, create_method=:post)
39
+ raise ArgumentError.new("`parent` given for #create is not valid") unless parent
40
+ raise ArgumentError.new("`attributes` given for #create is not valid") unless attributes.is_a? Hash
41
+ parameters = case create_method
42
+ when :post
43
+ {"data" => attributes}
44
+ when :post_multipart
45
+ attributes
46
+ end
47
+
48
+ result = @api_connector.send(create_method, collection_path(parent.class, parent.id), parameters)
49
+
50
+ if result["data"]
51
+ return new_object(result["data"])
52
+ else
53
+ puts "No result: #{result}"
54
+ return nil
55
+ end
56
+ end
57
+
58
+ def delete(id)
59
+ raise ArgumentError.new("`id` given for #find is not valid") unless id
60
+ result = @api_connector.delete(resource_path(id))
61
+ if result["data"]
62
+ return new_object(result["data"])
63
+ else
64
+ puts "No result: #{result}"
65
+ return nil
66
+ end
67
+ end
68
+
69
+ def index(parent_class, parent_id)
70
+ raise ArgumentError.new("`parent_id` given for #index is not valid") unless parent_id
71
+ raise ArgumentError.new("`parent_class` given for #index is not valid") unless parent_class
72
+
73
+ result = @api_connector.get(collection_path(parent_class, parent_id))
74
+ if result["data"]
75
+ return result["data"].map{|x| new_object(x)}
76
+ else
77
+ puts "No result: #{result}"
78
+ return nil
79
+ end
80
+ end
81
+
82
+ private
83
+
84
+ # Construct a new object of @handling_class, given a succesful api_response
85
+ def new_object(api_response)
86
+ @handling_class.new(
87
+ api_response["id"],
88
+ api_response["attributes"],
89
+ api_response["relations"],
90
+ self
91
+ )
92
+ end
93
+
94
+ def resource_path(id)
95
+ "#{@handling_class.api_name}/#{id}"
96
+ end
97
+
98
+ def collection_path(parent_class=nil, parent_id=nil)
99
+ if (parent_class != nil) ^ (parent_id != nil)
100
+ raise ArgumentError.new("An invalid combination `parent_class` and `parent_id` is given. Provide both or none.")
101
+ end
102
+
103
+ if parent_class
104
+ "#{parent_class.api_name}/#{parent_id}/#{@handling_class.api_name}"
105
+ else
106
+ "#{@handling_class.api_name}"
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,18 @@
1
+ module PlateApi::PlateObject
2
+ class Attachment < Base
3
+
4
+ has_one :site, "PlateApi::PlateObject::Site"
5
+
6
+ def download
7
+ @object_handler.api_connector.get("attachments/#{@id}/download", {}, :raw)
8
+ end
9
+
10
+ def self.api_name
11
+ "attachments"
12
+ end
13
+
14
+ def self.parent_class
15
+ Site
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,117 @@
1
+ module PlateApi::PlateObject
2
+ class Base
3
+ attr_reader :id, :attributes, :relations
4
+ attr_accessor :object_handler
5
+
6
+ HasOneRelations = {}
7
+ HasManyRelations = {}
8
+
9
+ def initialize(id, attributes, relations, object_handler=nil)
10
+ @id = id
11
+ @object_handler = object_handler
12
+ initialize_state(attributes, relations)
13
+ end
14
+
15
+ def api_name
16
+ self.class.api_name
17
+ end
18
+
19
+ def reload
20
+ raise ArgumentError.new("No object_handler is set.") unless @object_handler
21
+ reinitialize(@object_handler.find(@id))
22
+ return self
23
+ end
24
+
25
+ def update(attributes)
26
+ raise ArgumentError.new("Input `attributes` is not a Hash") unless attributes.is_a? Hash
27
+ raise ArgumentError.new("No object_handler is attached to this object") unless @object_handler
28
+ if new_object = @object_handler.update(@id, attributes)
29
+ reinitialize(new_object)
30
+ else
31
+ raise ArgumentError.new("The update was unsuccesful.")
32
+ end
33
+ return self
34
+ end
35
+
36
+ def delete
37
+ raise ArgumentError.new("No object_handler is attached to this object") unless @object_handler
38
+ @object_handler.delete(@id)
39
+ end
40
+
41
+ def to_s
42
+ "<Plate #{self.class.name.split('::').last}, @id=#{@id}, @attributes=#{@attributes}, @object_handler=#{@object_handler}>"
43
+ end
44
+
45
+ def inspect
46
+ to_s
47
+ end
48
+
49
+ def method_missing(m, *args, &block)
50
+ if attributes[m.to_s]
51
+ return attributes[m.to_s]
52
+ elsif attributes["content"] && attributes["content"][m.to_s]
53
+ return attributes["content"][m.to_s]["value"]
54
+ else
55
+ super
56
+ end
57
+ end
58
+
59
+ def ==(other)
60
+ return other.id == @id && other.class == self.class
61
+ end
62
+
63
+ private
64
+
65
+ def reinitialize(new_object)
66
+ initialize_state(new_object.attributes, new_object.relations)
67
+ end
68
+
69
+ def initialize_state(attributes, relations)
70
+ set_relation_ids(relations)
71
+ @attributes = attributes
72
+ @relations = relations
73
+ end
74
+
75
+ def self.has_one(name, klass)
76
+ self.attr_accessor "#{name}_id"
77
+ HasOneRelations[name.to_s] = klass
78
+ define_has_one_method(name, klass)
79
+ end
80
+
81
+ def self.define_has_one_method(name, klass)
82
+ define_method(name.to_s) do
83
+ id = self.send("#{name}_id")
84
+ return nil unless id
85
+ @object_handler.api_connector.handler(Object.const_get(klass)).find(id)
86
+ end
87
+ end
88
+
89
+ def self.has_many(plural_name, singular_name, klass, define_create_method=false)
90
+ HasManyRelations[plural_name.to_s] = klass
91
+ define_has_many_method(plural_name, klass)
92
+ define_create_method(singular_name, klass) if define_create_method
93
+ end
94
+
95
+ def self.define_has_many_method(plural_name, klass)
96
+ define_method(plural_name.to_s) do
97
+ @object_handler.api_connector.handler(Object.const_get(klass)).index(self.class, @id)
98
+ end
99
+ end
100
+
101
+ def self.define_create_method(singular_name, klass)
102
+ define_method("create_#{singular_name}") do |create_attributes|
103
+ @object_handler.api_connector.handler(Object.const_get(klass)).create(self, create_attributes)
104
+ end
105
+ end
106
+
107
+ def set_relation_ids(relations_attributes)
108
+ return unless relations_attributes
109
+ self.class::HasOneRelations.keys.each do |relation_name|
110
+ val = relations_attributes["#{relation_name}_id"]
111
+ if val
112
+ send("#{relation_name}_id=", val)
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,15 @@
1
+ module PlateApi::PlateObject
2
+ class Column < Base
3
+
4
+ has_one :row, "PlateApi::PlateObject::Row"
5
+ has_many :elements, :element, "PlateApi::PlateObject::Element", true
6
+
7
+ def self.api_name
8
+ "columns"
9
+ end
10
+
11
+ def self.parent_class
12
+ Row
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module PlateApi::PlateObject
2
+ class Company < Base
3
+
4
+ has_one :partner, "PlateApi::PlateObject::Partner"
5
+ has_many :sites, :site, "PlateApi::PlateObject::Site"
6
+
7
+ def self.api_name
8
+ "companies"
9
+ end
10
+
11
+ def self.parent_class
12
+ Partner
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ module PlateApi::PlateObject
2
+ class Element < Base
3
+
4
+ has_one :column, "PlateApi::PlateObject::Column"
5
+
6
+ def self.api_name
7
+ "elements"
8
+ end
9
+
10
+ def self.parent_class
11
+ Column
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module PlateApi::PlateObject
2
+ class Partner < Base
3
+
4
+ has_many :companies, :company, "PlateApi::PlateObject::Company"
5
+
6
+ def self.api_name
7
+ "partners"
8
+ end
9
+
10
+ def self.parent_class
11
+ Partner
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ module PlateApi::PlateObject
2
+ class Post < Base
3
+
4
+ has_one :site_translation, "PlateApi::PlateObject::SiteTranslation"
5
+ has_many :sections, :section, "PlateApi::PlateObject::Section", true
6
+ has_many :rows, :row, "PlateApi::PlateObject::Row"
7
+ has_many :columns, :column, "PlateApi::PlateObject::Column"
8
+ has_many :elements, :element, "PlateApi::PlateObject::Element"
9
+
10
+ def self.api_name
11
+ "posts"
12
+ end
13
+
14
+ def self.parent_class
15
+ SiteTranslation
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module PlateApi::PlateObject
2
+ class Row < Base
3
+
4
+ has_one :section, "PlateApi::PlateObject::Section"
5
+ has_many :columns, :column, "PlateApi::PlateObject::Column", true
6
+ has_many :elements, :element, "PlateApi::PlateObject::Element"
7
+
8
+ def self.api_name
9
+ "rows"
10
+ end
11
+
12
+ def self.parent_class
13
+ Section
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ module PlateApi::PlateObject
2
+ class Section < Base
3
+
4
+ has_one :post, "PlateApi::PlateObject::Post"
5
+ has_many :rows, :row, "PlateApi::PlateObject::Row", true
6
+ has_many :columns, :column, "PlateApi::PlateObject::Column"
7
+ has_many :elements, :element, "PlateApi::PlateObject::Element"
8
+
9
+ def self.api_name
10
+ "sections"
11
+ end
12
+
13
+ def self.parent_class
14
+ Post
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,28 @@
1
+ module PlateApi::PlateObject
2
+ class Site < Base
3
+
4
+ has_one :theme, "PlateApi::PlateObject::Theme"
5
+ has_one :company, "PlateApi::PlateObject::Company"
6
+ has_many :site_translations, :site_translation, "PlateApi::PlateObject::SiteTranslation"
7
+ has_many :posts, :post, "PlateApi::PlateObject::Post"
8
+ has_many :sections, :section, "PlateApi::PlateObject::Section"
9
+ has_many :rows, :row, "PlateApi::PlateObject::Row"
10
+ has_many :columns, :column, "PlateApi::PlateObject::Column"
11
+ has_many :elements, :element, "PlateApi::PlateObject::Element"
12
+ has_many :attachments, :attachment, "PlateApi::PlateObject::Attachment"
13
+
14
+ def self.api_name
15
+ "sites"
16
+ end
17
+
18
+ def self.parent_class
19
+ Company
20
+ end
21
+
22
+ def create_attachment(file, extra_parameters={})
23
+ raise ArgumentError.new("No File is given as input") unless file.is_a? File
24
+ parameters = extra_parameters.merge({file: file})
25
+ @object_handler.api_connector.handler(PlateApi::PlateObject::Attachment).create(self, parameters, :post_multipart)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ module PlateApi::PlateObject
2
+ class SiteTranslation < Base
3
+
4
+ has_one :site, "PlateApi::PlateObject::Site"
5
+ has_many :posts, :post, "PlateApi::PlateObject::Post", true
6
+ has_many :sections, :section, "PlateApi::PlateObject::Section"
7
+ has_many :rows, :row, "PlateApi::PlateObject::Row"
8
+ has_many :columns, :column, "PlateApi::PlateObject::Column"
9
+ has_many :elements, :element, "PlateApi::PlateObject::Element"
10
+
11
+ def self.api_name
12
+ "site_translations"
13
+ end
14
+
15
+ def self.parent_class
16
+ Site
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module PlateApi::PlateObject
2
+ class Theme < Base
3
+
4
+ def self.api_name
5
+ "themes"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,35 @@
1
+ require 'mimemagic'
2
+
3
+ module PlateApi
4
+ class PostMultipartRequest < Request
5
+ HttpAdapter = :net_http
6
+
7
+ def initialize(public_key, secret, path, parameters={}, custom_server=nil)
8
+ super(public_key, secret, "POST", path, custom_server)
9
+
10
+ @post_parameters = map_parameters(parameters)
11
+ end
12
+
13
+ def extra_builder_options(builder)
14
+ builder.request :multipart
15
+ builder.request :url_encoded
16
+ end
17
+
18
+ def extra_request_options(request)
19
+ request.body = @post_parameters
20
+ end
21
+
22
+ def map_parameters(parameters)
23
+ parameters.keys.each do |key|
24
+ val = parameters[key]
25
+ if val.is_a? File
26
+ full_path = File.expand_path(val)
27
+ mime_type = MimeMagic.by_path(full_path).type
28
+ parameters[key] = Faraday::UploadIO.new(full_path, mime_type)
29
+ end
30
+ end
31
+
32
+ return parameters
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,3 @@
1
- require "plate_api/request"
2
-
3
1
  module PlateApi
4
2
  class PutRequest < Request
5
3
 
@@ -3,18 +3,20 @@ require 'faraday_middleware'
3
3
  require 'time'
4
4
  require "base64"
5
5
  require "openssl"
6
+ require "json"
6
7
 
7
8
 
8
9
  module PlateApi
9
10
  class Request
10
11
  DefaultApiBaseEndpoint = "https://www.startwithplate.com/api/v2"
12
+ HttpAdapter = Faraday.default_adapter
11
13
 
12
14
  def initialize(public_key, secret, method, path, custom_server=nil)
13
15
  base_api_endpoint = custom_server ? custom_server : DefaultApiBaseEndpoint
14
16
 
15
17
  @connection = ::Faraday.new(url: base_api_endpoint) do |faraday|
16
- faraday.response :json
17
- faraday.adapter Faraday.default_adapter
18
+ extra_builder_options(faraday)
19
+ faraday.adapter HttpAdapter
18
20
  end
19
21
 
20
22
  @public_key = public_key
@@ -23,16 +25,20 @@ module PlateApi
23
25
  @path = strip_path(path)
24
26
  end
25
27
 
26
- def send
28
+ def execute(response_type=:raw)
27
29
  response = @connection.send(@method.downcase) do |request|
28
30
  request.url url_path
29
31
  request.headers['Date'] = request_date
30
32
  request.headers['Authorization'] = calculate_signature
31
- request.headers['Content-Type'] = 'application/json'
32
33
  extra_request_options(request)
33
34
  end
34
35
 
35
- return response.body
36
+ return case response_type
37
+ when :raw
38
+ return response.body
39
+ when :json
40
+ return JSON.parse(response.body)
41
+ end
36
42
  end
37
43
 
38
44
  def request_date
@@ -62,6 +68,9 @@ module PlateApi
62
68
  def extra_request_options(request)
63
69
  end
64
70
 
71
+ def extra_builder_options(request)
72
+ end
73
+
65
74
  def strip_path(path)
66
75
  path.gsub(/^\/|\/$/, '')
67
76
  end
@@ -1,3 +1,3 @@
1
1
  module PlateApi
2
- VERSION = "0.4.0"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/plate_api.rb CHANGED
@@ -1,5 +1,15 @@
1
1
  require "plate_api/version"
2
+ require "plate_api/object_handler"
3
+ require "plate_api/plate_object/base"
4
+ Dir[File.join(__dir__,"plate_api/plate_object/", "*.rb")].each {|file| require file }
2
5
  require "plate_api/connector"
6
+ require "plate_api/request"
7
+ require "plate_api/get_request"
8
+ require "plate_api/delete_request"
9
+ require "plate_api/post_request"
10
+ require "plate_api/post_multipart_request"
11
+ require "plate_api/put_request"
12
+
3
13
 
4
14
  module PlateApi
5
15
  end
data/plate_api.gemspec CHANGED
@@ -9,7 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.date = '2019-01-05'
10
10
  spec.summary = "Connector for the Plate API"
11
11
  spec.description = "This gem can be used to connect to the Plate API. It takes care
12
- of the authentication procedure. "
12
+ of the authentication procedure. It also provides a basic wrapper around the Plate API,
13
+ so objects in Plate can be manipulated as local objects."
13
14
  spec.authors = ["David Kortleven"]
14
15
  spec.email = 'david@getplate.com'
15
16
  spec.files = ["lib/plate_api.rb"]
@@ -33,8 +34,15 @@ Gem::Specification.new do |spec|
33
34
 
34
35
  spec.add_dependency "faraday", "~> 0.15.4"
35
36
  spec.add_dependency "faraday_middleware", "~> 0.13.1"
37
+ spec.add_dependency "mimemagic", "~> 0.3.3"
36
38
 
37
39
  spec.add_development_dependency "bundler", "~> 1.16"
38
40
  spec.add_development_dependency "rake", "~> 10.0"
39
41
  spec.add_development_dependency "rspec", "~> 3.0"
42
+ spec.add_development_dependency "webmock", "~> 3.0"
43
+ spec.add_development_dependency "factory_bot", "~> 5.0.1"
44
+ spec.add_development_dependency "simplecov"
45
+ spec.add_development_dependency "simplecov-console"
46
+ spec.add_development_dependency "byebug"
47
+
40
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plate_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Kortleven
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.13.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: mimemagic
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.3.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.3.3
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,13 +94,86 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '3.0'
83
- description: "This gem can be used to connect to the Plate API. It takes care\n of
84
- the authentication procedure. "
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: factory_bot
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 5.0.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 5.0.1
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov-console
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: byebug
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ description: |-
168
+ This gem can be used to connect to the Plate API. It takes care
169
+ of the authentication procedure. It also provides a basic wrapper around the Plate API,
170
+ so objects in Plate can be manipulated as local objects.
85
171
  email: david@getplate.com
86
172
  executables: []
87
173
  extensions: []
88
174
  extra_rdoc_files: []
89
175
  files:
176
+ - ".byebug_history"
90
177
  - ".gitignore"
91
178
  - ".rspec"
92
179
  - ".travis.yml"
@@ -101,6 +188,20 @@ files:
101
188
  - lib/plate_api/connector.rb
102
189
  - lib/plate_api/delete_request.rb
103
190
  - lib/plate_api/get_request.rb
191
+ - lib/plate_api/object_handler.rb
192
+ - lib/plate_api/plate_object/attachment.rb
193
+ - lib/plate_api/plate_object/base.rb
194
+ - lib/plate_api/plate_object/column.rb
195
+ - lib/plate_api/plate_object/company.rb
196
+ - lib/plate_api/plate_object/element.rb
197
+ - lib/plate_api/plate_object/partner.rb
198
+ - lib/plate_api/plate_object/post.rb
199
+ - lib/plate_api/plate_object/row.rb
200
+ - lib/plate_api/plate_object/section.rb
201
+ - lib/plate_api/plate_object/site.rb
202
+ - lib/plate_api/plate_object/site_translations.rb
203
+ - lib/plate_api/plate_object/theme.rb
204
+ - lib/plate_api/post_multipart_request.rb
104
205
  - lib/plate_api/post_request.rb
105
206
  - lib/plate_api/put_request.rb
106
207
  - lib/plate_api/request.rb