cloth 0.0.1 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9692d7a1b35d348a10c02be04bcb6eba99d6e35
4
- data.tar.gz: 6b71f68a0b7ed1674c4e9e4420aec79e2b9ecbe9
3
+ metadata.gz: a4859db3b96e68fb4659b746327882b2234af798
4
+ data.tar.gz: 07f971ddcf128572453e1706f8d904e4a35f8378
5
5
  SHA512:
6
- metadata.gz: 3b13455acf666a243d1d2bc32263972d3762efaeb23a4e4df7f93783457af3a1e901ae9c6d26036c3e71a7ea800fae785518feaffa9b7a31aeea730644e39995
7
- data.tar.gz: 6f99d65a5ddf3426550c5c288d7875ed2ac1ba915ff75e31698733af55c0c3785504086bc532aff017840872d14107680c467d646c96eae73b57554663bc6e3f
6
+ metadata.gz: 904ed1d055ce2d37c6eb4b125d56a5c67ef2a31d6771550e9267a626dcb9ff4913609f69d73813d8f8a43c7a4453be4e8eb5e15726ef7f93e2877b30259b290b
7
+ data.tar.gz: cfefa91159832584e3a265a467196fca65bb509cdc56f9271e0a449050471b8dad46e995b26a7f7801e23139fdda40aa63bfa3725c5db6142a3ccbb056d71037
@@ -1,3 +1,4 @@
1
+ require "cloth/account"
1
2
  require "cloth/client"
2
3
  require "cloth/item"
3
4
  require "cloth/user"
@@ -6,7 +7,6 @@ require "cloth/version"
6
7
  module Cloth
7
8
  class << self
8
9
  attr_accessor :api_key, :client
9
- attr_reader :api_url
10
10
 
11
11
  def client
12
12
  @_client ||= Cloth::Client.new
@@ -15,5 +15,9 @@ module Cloth
15
15
  def api_url
16
16
  @api_url ||= "https://api.cloth.network"
17
17
  end
18
+
19
+ def api_url=(url)
20
+ @api_url = url
21
+ end
18
22
  end
19
23
  end
@@ -0,0 +1,9 @@
1
+ module Cloth
2
+ class Account
3
+ class << self
4
+ def usage
5
+ Cloth.client.get('/api/usage')
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,6 @@
1
+ require 'json'
2
+ require 'typhoeus'
3
+
1
4
  module Cloth
2
5
  class Client
3
6
  attr_reader :api_key
@@ -6,33 +9,37 @@ module Cloth
6
9
  raise Exception, "An API key must be set before the Cloth client can be used. Set an API key with 'Cloth.api_key = ...'" unless @api_key
7
10
  end
8
11
 
9
- def get(url, options)
10
- request(:get, url, options)
12
+ def get(url, options = {})
13
+ JSON.parse(request(:get, url, options).run.body)
11
14
  end
12
15
 
13
- def post(url, options)
14
- request(:post, url, options)
16
+ def post(url, options = {})
17
+ headers = { 'Content-Type': "application/x-www-form-urlencoded" }
18
+ JSON.parse(request(:post, url, options.merge({ headers: headers })).run.body)
15
19
  end
16
20
 
17
- def request(method = :get, url, options)
21
+ def request(method, url, options = {})
18
22
  fixed_url = url[0] == "/" ? url[1..-1] : url
23
+ url = [Cloth.api_url, fixed_url].join('/')
19
24
  Typhoeus::Request.new(
20
- [Cloth.api_url, fixed_url].join('/'),
25
+ url,
21
26
  method: method,
22
27
  body: options[:body],
23
28
  params: options[:params],
24
29
  headers: {
25
- 'Cloth-Api-Key': @api_key
26
- }.merge(options[:headers])
27
- ).run
30
+ 'Accept': 'application/json',
31
+ 'Cloth-Api-Key': @api_key,
32
+ 'Content-Type': 'application/json'
33
+ }.merge(options[:headers] || {})
34
+ )
28
35
  end
29
36
 
30
37
  class << self
31
- def get(url, options)
38
+ def get(url, options = {})
32
39
  Cloth.client.get(url, options)
33
40
  end
34
41
 
35
- def post(url, options)
42
+ def post(url, options = {})
36
43
  Cloth.client.post(url, options)
37
44
  end
38
45
  end
@@ -1,30 +1,42 @@
1
1
  module Cloth
2
2
  class Item
3
+ def initialize(id, data)
4
+ @id = id
5
+ @data = data
6
+ @user = data["user_id"] if @data && data["user_id"]
7
+ end
8
+
3
9
  attr_reader :data
4
10
  attr_reader :id
11
+ attr_reader :user
5
12
 
6
13
  def recommend
7
- Cloth.client.post("/api/items/recommend", { id: id, data: data })
14
+ Cloth.client.post("/api/items/recommend", { body: { item: { id: id, data: data } } })
8
15
  end
9
16
 
10
17
  def unrecommend
11
- Cloth.client.post("/api/items/unrecommend", { id: id, data: data })
18
+ Cloth.client.post("/api/items/unrecommend", { body: { item: { id: id, data: data } } })
12
19
  end
13
20
 
14
21
  def recommendations
15
- Cloth.client.get("/api/items/#{id}/recommendations")
22
+ resp = Cloth.client.get("/api/items/#{id}/recommendations")
23
+ if resp['recommendations']
24
+ resp['recommendations']
25
+ else
26
+ resp
27
+ end
16
28
  end
17
29
 
18
30
  class << self
19
- def recommend_item(id, data)
31
+ def recommend(id, data = nil)
20
32
  Item.new(id, data).recommend
21
33
  end
22
34
 
23
- def unrecommend_item(id, data)
35
+ def unrecommend(id, data = nil)
24
36
  Item.new(id, data).unrecommend
25
37
  end
26
38
 
27
- def recommendations(id, data)
39
+ def recommendations(id, data = nil)
28
40
  Item.new(id, data).recommendations
29
41
  end
30
42
  end
@@ -1,3 +1,3 @@
1
1
  module Cloth
2
- VERSION = "0.0.1".freeze
2
+ VERSION = "0.0.3".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kristian Freeman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-07 00:00:00.000000000 Z
11
+ date: 2017-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -78,6 +78,7 @@ files:
78
78
  - bin/console
79
79
  - bin/setup
80
80
  - lib/cloth.rb
81
+ - lib/cloth/account.rb
81
82
  - lib/cloth/client.rb
82
83
  - lib/cloth/item.rb
83
84
  - lib/cloth/user.rb