craftar 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5d6ac3e86d4425c064d13ebff2762a2a13d52cd4
4
+ data.tar.gz: 272c0ca83bfbcdb592013d1b0202ac3f44ad308f
5
+ SHA512:
6
+ metadata.gz: 18ff4f41ef8447926484fbe020c9440e455db77d2e837b81092d124e4ea0fdadd2e04fe120678d47246b6005dc1b4dbbcc27890bf0ec152364797ffb60cb87d6
7
+ data.tar.gz: c170a93e46aa299187e4ea08784970d15a0a11790e13ea5dd09454f0e3c10d7c09e821d8b8b7b650e23df3f1594bc5e64b827fa0b00effa072dfc97576783da1
@@ -0,0 +1,9 @@
1
+ require 'craftar/base'
2
+ require 'craftar/collection'
3
+ require 'craftar/item'
4
+ require 'craftar/image'
5
+ require 'craftar/token'
6
+ require 'craftar/media'
7
+ module Craftar
8
+ class << self; attr_accessor :api_key; end
9
+ end
@@ -0,0 +1,101 @@
1
+ # The main Craftar class
2
+ module Craftar
3
+ class Base
4
+ include HTTMultiParty
5
+ base_uri 'https://my.craftar.net/api/v0'
6
+
7
+ # List the objects of the described class
8
+
9
+ # options: 'limit', 'offset'and any object specific option (see README)
10
+ # To manage the pagination, all responses have a ‘meta’ field
11
+ # The "meta" fields are:
12
+ # - total_count: total number of objects in the underlying object list
13
+ # - limit: how many objects are returned per page. By default it’s 20.
14
+ # - offset: how many initial objects are skipped in this response
15
+ # - previous: if available, provides a URI to the previous page
16
+ # - next: if available, provides a URI to the next page
17
+ # Fetching a page To navigate through pages, use the ‘limit‘ and ‘offset‘ parameters.
18
+ # The objects of the current page are in the 'objects' fields
19
+ def self.list(opts = {})
20
+ response = parse_response(get("/#{craftar_name}/", basic_options.merge(opts)))
21
+ raise (response ['error']['message']) if response['error']
22
+ objects = []
23
+ response['objects'].each do |object|
24
+ objects << self.new(object.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo})
25
+ end
26
+ response[:objects] = objects
27
+ response.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
28
+ end
29
+
30
+ # Find an object thanks to its uuid
31
+ def self.find(uuid)
32
+ response = parse_response(get("/#{craftar_name}/#{uuid}", basic_options))
33
+ return if response == {}
34
+ self.new(response.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo})
35
+ end
36
+
37
+ # create a new object
38
+ # options are the different specific parameters of this object.
39
+ # See readme
40
+ def self.create(options)
41
+ obj = self.new(options)
42
+ obj.save
43
+ end
44
+
45
+ # destroy an object
46
+ def destroy
47
+ self.class.delete("/#{self.class.craftar_name}/#{uuid}/", self.class.basic_options)
48
+ end
49
+
50
+ private
51
+
52
+ # the basic query option
53
+ def self.basic_options
54
+ { query: { api_key: Craftar.api_key } }
55
+ end
56
+
57
+ def call(method_name, opts)
58
+ response = self.class.send(method_name,
59
+ "/#{self.class.craftar_name}/",
60
+ query: { api_key: Craftar.api_key }.merge!(opts)
61
+ )
62
+ raise (response ['error']['message']) if response['error']
63
+ response
64
+ end
65
+
66
+ def json_call(method_name, opts)
67
+ uid = opts.delete(:uuid)
68
+ path = "/#{self.class.craftar_name}/"
69
+ path += "#{uid}/" if uid
70
+ path += "?api_key=#{Craftar.api_key}"
71
+ response = self.class.parse_response( HTTParty.send(
72
+ method_name,
73
+ self.class.base_uri + path,
74
+ body: opts.to_json,
75
+ headers: { 'Content-Type' => 'application/json' }
76
+ ))
77
+ raise response ['error']['message'] if response['error']
78
+ response
79
+ end
80
+
81
+ def self.parse_response(httparty)
82
+ response_body = httparty.response.body
83
+ response_body.to_s != '' ? JSON.parse(response_body) : {}
84
+ end
85
+
86
+ def prepare_file_from_url(url)
87
+ require 'open-uri'
88
+ extname = File.extname(url)
89
+ basename = File.basename(url, extname)
90
+
91
+ file = Tempfile.new([basename, extname])
92
+ file.binmode
93
+
94
+ open(URI.parse(url)) do |data|
95
+ file.write data.read
96
+ end
97
+ file.rewind
98
+ file
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,27 @@
1
+ module Craftar
2
+ class Collection < Craftar::Base
3
+ attr_reader :uuid, :name, :resource_uri
4
+ def self.craftar_name
5
+ 'collection'
6
+ end
7
+
8
+ def initialize(opts)
9
+ @name = opts[:name]
10
+ @uuid = opts[:uuid]
11
+ @resource_uri = opts[:resource_uri]
12
+ end
13
+
14
+ def save
15
+ response = json_call(:post, name: @name)
16
+ @uuid = response['uuid']
17
+ @resource_uri = response['resource_uri']
18
+ self
19
+ end
20
+
21
+ def update(opts)
22
+ response = json_call(:put, { uuid: @uuid, name: opts[:name] }.select { |_, value| !value.nil? })
23
+ @name = response['name']
24
+ self
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ module Craftar
2
+ class Image < Craftar::Base
3
+ attr_reader :uuid, :item, :resource_uri, :file, :name, :status, :thumb_120, :thumb_60, :tracking_data_status
4
+
5
+ def self.craftar_name
6
+ 'image'
7
+ end
8
+
9
+ def initialize(opts)
10
+ set_attributes(opts)
11
+ end
12
+
13
+ def save
14
+ response = call(:post, file: prepare_file_from_url(@file), item: item)
15
+ set_attributes(response)
16
+ self
17
+ end
18
+
19
+ private
20
+
21
+ def set_attributes(opts)
22
+ @uuid = opts['uuid'] || opts[:uuid]
23
+ @item = opts['item'] || opts[:item]
24
+ @file = opts['file'] || opts[:file]
25
+ @resource_uri = opts['resource_uri'] || opts[:resource_uri]
26
+ @name = opts['name'] || opts[:name]
27
+ @status = opts['status'] || opts[:status]
28
+ @thumb_120 = opts['thumb_120'] || opts[:thumb_120]
29
+ @thumb_60 = opts['thumb_60'] || opts[:thumb_60]
30
+ @tracking_data_status = opts['tracking_data_status'] || opts[:tracking_data_status]
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,53 @@
1
+ module Craftar
2
+ class Item < Craftar::Base
3
+ attr_reader :uuid, :name, :collection, :url, :content, :custom, :trackable, :resource_uri
4
+ def self.craftar_name
5
+ 'item'
6
+ end
7
+
8
+ def initialize(opts)
9
+ @name = opts[:name]
10
+ @collection = opts[:collection]
11
+ @url = opts[:url]
12
+ @content = opts[:content]
13
+ @custom = opts[:custom]
14
+ @trackable = opts[:trackable]
15
+ @uuid = opts[:uuid]
16
+ @resource_uri = opts[:resource_uri]
17
+ end
18
+
19
+ def save
20
+ response = json_call(
21
+ :post,
22
+ {
23
+ name: @name,
24
+ collection: @collection,
25
+ trackable: @trackable,
26
+ content: @content
27
+ }
28
+ )
29
+ @uuid = response['uuid']
30
+ @resource_uri = response['resource_uri']
31
+ self
32
+ end
33
+
34
+ def update(opts)
35
+ options = {
36
+ name: opts[:name],
37
+ collection: opts[:collection],
38
+ url: opts[:url],
39
+ content: opts[:content],
40
+ custom: opts[:custom],
41
+ trackable: opts[:trackable]
42
+ }.select { |_, value| !value.nil? }
43
+ response = json_call(:put, { uuid: @uuid }.merge(options))
44
+ @name = response['name']
45
+ @collection = response['collection']
46
+ @url = response['url']
47
+ @content = response['content']
48
+ @custom = response['custom']
49
+ @trackable = response['trackable']
50
+ self
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,27 @@
1
+ module Craftar
2
+ class Media < Craftar::Base
3
+ attr_reader :uuid, :file, :resource_uri, :name, :mimetype
4
+
5
+ def self.craftar_name
6
+ 'media'
7
+ end
8
+
9
+ def initialize(opts)
10
+ @uuid = opts[:uuid]
11
+ @file = opts[:file]
12
+ @resource_uri = opts[:resource_uri]
13
+ @name = opts[:name]
14
+ @mimetype = opts[:mimetype]
15
+ end
16
+
17
+ def save
18
+ response = call(:post, file: prepare_file_from_url(@file))
19
+ @uuid = response['uuid']
20
+ @file = response['file']
21
+ @resource_uri = response['resource_uri']
22
+ @name = response['name']
23
+ @mimetype = response['mimetype']
24
+ self
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,29 @@
1
+ module Craftar
2
+ class Token < Craftar::Base
3
+ attr_reader :token, :resource_uri, :collection
4
+ def self.craftar_name
5
+ 'token'
6
+ end
7
+
8
+ def initialize(opts)
9
+ @collection = opts[:collection]
10
+ @token = opts[:token]
11
+ @resource_uri = opts[:resource_uri]
12
+ end
13
+
14
+ def save
15
+ response = json_call(:post, collection: @collection)
16
+ @token = response['token']
17
+ @resource_uri = response['resource_uri']
18
+ self
19
+ end
20
+
21
+ def find(token)
22
+ raise 'You cannot find a token'
23
+ end
24
+
25
+ def destroy
26
+ self.class.delete("/token/#{@token}/", self.class.basic_options)
27
+ end
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: craftar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marieke Gueye
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 3.2.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: httmultiparty
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.3'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.3.16
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '0.3'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 0.3.16
53
+ description: " Ruby client that supports all of the Craftar CraftAR API methods. Intuitive
54
+ query methods allow you easily call API endpoints. "
55
+ email: marieke.gueye@gmail.com
56
+ executables: []
57
+ extensions: []
58
+ extra_rdoc_files: []
59
+ files:
60
+ - lib/craftar.rb
61
+ - lib/craftar/base.rb
62
+ - lib/craftar/collection.rb
63
+ - lib/craftar/image.rb
64
+ - lib/craftar/item.rb
65
+ - lib/craftar/media.rb
66
+ - lib/craftar/token.rb
67
+ homepage: https://github.com/mkou/craftar
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.4.5
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Ruby client for the official Craftar CraftAR Management API
91
+ test_files: []