layer-api 0.4.0 → 0.4.1

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
  SHA1:
3
- metadata.gz: 68514306a1bf3af9a052d78f8b05a0320187b8e8
4
- data.tar.gz: bf7c980017a973f4161d4506a028c9946b1d9493
3
+ metadata.gz: f6ab8370a2e62e08d609cec42258773e8513f9d8
4
+ data.tar.gz: 84cb431cb17c4a4f7056c960c07e194e3b3b87b1
5
5
  SHA512:
6
- metadata.gz: 2ceea987ca922d113bb8fb8d96e696f8d8487a731915c68b28d78e4b1a6982e9ec8ad739d4324324ab51060ca3b0f2302f923bc6f6674efc85f5a1deffbebe87
7
- data.tar.gz: d1b427e746217ed81afc5ed239113cffa7226ecb8b545a59416336841c0939a976987c24e538f934de20e95e5ae527595ea3b9f665f769271166e581d68f7b52
6
+ metadata.gz: d423646475953f4ef3d8c420abe8d7b663223c3a0e33fd5d1702648761efd4e10cb89b3d7d3e242020ea2cdc252f78af3dfe9ee0ef5a7e3010a2a7d4970debbf
7
+ data.tar.gz: 266871324eb01f3bde7faa784f07d0b7a2995d70c9cf27b01fa49732a49722851e083b279bbab41a9f94880725f06074c6cc6a1464e62cee7243aa1c3c470b7d
data/MIGRATING.md CHANGED
@@ -28,7 +28,7 @@ In `0.3.x`, each function returned a Hash containing a parsed JSON representatio
28
28
 
29
29
  `0.4` introduced `Resource` - A base class used to encapsulate each JSON response.
30
30
 
31
- `Resource` can allow attributes to be accessed with dot notation. Alternatively, you can return a Hashed version of the resource using `Resource.attributes`. For example:
31
+ `Resource` can allow attributes to be accessed by calling the attribute name on the instance. Alternatively, you can return a Hashed version of the resource using `Resource.attributes`. For example:
32
32
 
33
33
  ```ruby
34
34
 
data/README.md CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
28
28
  ## Usage
29
29
 
30
30
  ### Resources
31
- All client methods return `Resource` objects or a collection of `Resource` objects. Every attribute from a resource can be accessed calling attribute methods:
31
+ All client methods return `Resource` objects or a collection of `Resource` objects. Every attribute from a resource can be accessed by calling attribute methods:
32
32
 
33
33
  ```ruby
34
34
  conversation = platform.conversations.find("fb2f3a48-523d-4449-a57f-c6651fc6612c")
@@ -4,13 +4,13 @@ module Layer
4
4
  attr_accessor :api_token, :app_id
5
5
 
6
6
  def initialize(options = {})
7
+ id = options[:app_id] || ENV['LAYER_APP_ID']
7
8
  @api_token = options[:api_token] || ENV['LAYER_API_TOKEN']
8
- @app_id = options[:app_id] || ENV['LAYER_APP_ID']
9
- strip_layer_prefix(@app_id)
9
+ @app_id = strip_layer_prefix(id)
10
10
  end
11
11
 
12
12
  def client
13
- @http_client ||= Layer::HttpClient.new(app_id, api_token)
13
+ @http_client ||= Layer::HttpClient.new(@app_id, @api_token)
14
14
  end
15
15
 
16
16
  def get(url, *args)
@@ -38,20 +38,26 @@ module Layer
38
38
  end
39
39
 
40
40
  def announcements
41
- Layer::ResourceProxy.new(nil, Layer::Resources::Announcement)
41
+ Layer::ResourceProxy.new(client, nil, Layer::Resources::Announcement)
42
42
  end
43
43
 
44
44
  def conversations
45
- Layer::ResourceProxy.new(nil, Layer::Resources::Conversation)
45
+ Layer::ResourceProxy.new(client, nil, Layer::Resources::Conversation)
46
46
  end
47
47
 
48
48
  def users
49
- Layer::ResourceProxy.new(nil, Layer::Resources::User)
49
+ Layer::ResourceProxy.new(client, nil, Layer::Resources::User)
50
50
  end
51
51
 
52
52
  def generate_identity_token(options = {})
53
53
  Layer::IdentityToken.new(options)
54
54
  end
55
+
56
+ def inspect
57
+ "#<#{self.class} api_token=\"#{@api_token}\" app_id=\"#{@app_id}\">"
58
+ end
59
+
60
+ alias_method :to_s, :inspect
55
61
  end
56
62
  end
57
63
  end
@@ -2,8 +2,9 @@ module Layer
2
2
  class Resource
3
3
  attr_reader :attributes, :client
4
4
 
5
- def initialize(attributes)
5
+ def initialize(attributes, client)
6
6
  @attributes = attributes
7
+ @client = client
7
8
  end
8
9
 
9
10
  def update(params)
@@ -18,10 +19,6 @@ module Layer
18
19
  client.delete(url)
19
20
  end
20
21
 
21
- def client
22
- self.class.client
23
- end
24
-
25
22
  def method_missing(method, *args, &block)
26
23
  method_key = method.to_s
27
24
  attributes.has_key?(method_key) ? attributes[method_key] : super
@@ -36,6 +33,12 @@ module Layer
36
33
  attributes["id"].split("/").last if attributes["id"]
37
34
  end
38
35
 
36
+ def inspect
37
+ "#<#{self.class} attributes=#{@attributes}>"
38
+ end
39
+
40
+ alias_method :to_s, :inspect
41
+
39
42
  class << self
40
43
  def class_name
41
44
  name.split("::").last
@@ -49,28 +52,21 @@ module Layer
49
52
  pluralized_name.downcase
50
53
  end
51
54
 
52
- def client
53
- Layer::HttpClient.new(
54
- ENV['LAYER_APP_ID'],
55
- ENV['LAYER_API_TOKEN']
56
- )
57
- end
58
-
59
- def create(url, params = {})
55
+ def create(client, url, params = {})
60
56
  response = client.post(url, body: params.to_json)
61
- new(response)
57
+ new(response, client)
62
58
  end
63
59
 
64
- def find(url, id)
60
+ def find(client, url, id)
65
61
  response = client.get("#{url}/#{id}")
66
- new(response)
62
+ new(response, client)
67
63
  end
68
64
 
69
- def list(url, params = {})
65
+ def list(client, url, params = {})
70
66
  collection = client.get(url, body: params.to_json)
71
67
 
72
68
  if collection.any?
73
- collection.map{ |resource| new(resource) }
69
+ collection.map{ |resource| new(resource, client) }
74
70
  else
75
71
  []
76
72
  end
@@ -1,6 +1,7 @@
1
1
  module Layer
2
2
  class ResourceProxy
3
- def initialize(base, resource)
3
+ def initialize(client, base, resource)
4
+ @client = client
4
5
  @base = base
5
6
  @resource = resource
6
7
  end
@@ -10,7 +11,7 @@ module Layer
10
11
  end
11
12
 
12
13
  def method_missing(method, *args, &block)
13
- @resource.public_send(method, *([url] + args), &block)
14
+ @resource.public_send(method, *([@client, url] + args), &block)
14
15
  end
15
16
 
16
17
  def respond_to_missing?(method, include_private = false)
@@ -1,30 +1,32 @@
1
1
  module Layer
2
2
  module Resources
3
3
  class Block < Layer::Resource
4
- def self.find(url, user)
4
+ def self.find(client, url, user)
5
5
  user_id = user.instance_of?(User) ? user.id : user
6
- new_abstract_instance(url, user_id)
6
+ new_abstract_instance(url, user_id, client)
7
7
  end
8
8
 
9
- def self.list(url, params = {})
9
+ def self.list(client, url, params = {})
10
10
  collection = client.get(url, body: params.to_json)
11
11
 
12
12
  if collection.any?
13
- collection.map{ |resource| new_abstract_instance(url, resource['user_id']) }
13
+ collection.map do |resource|
14
+ new_abstract_instance(url, resource['user_id'], client)
15
+ end
14
16
  else
15
17
  []
16
18
  end
17
19
  end
18
20
 
19
- def self.create(url, params = {})
21
+ def self.create(client, url, params = {})
20
22
  user_params = params.instance_of?(User) ? {user_id: params.id} : params
21
23
 
22
24
  client.post(url, body: user_params.to_json)
23
- new_abstract_instance(url, user_params[:user_id])
25
+ new_abstract_instance(url, user_params[:user_id], client)
24
26
  end
25
27
 
26
- def self.new_abstract_instance(url, id)
27
- new({"id" => id, "url" => "#{url}/#{id}"})
28
+ def self.new_abstract_instance(url, id, client)
29
+ new({"id" => id, "url" => "#{url}/#{id}"}, client)
28
30
  end
29
31
  end
30
32
  end
@@ -2,7 +2,7 @@ module Layer
2
2
  module Resources
3
3
  class Conversation < Layer::Resource
4
4
  def messages
5
- Layer::ResourceProxy.new(self, Message)
5
+ Layer::ResourceProxy.new(client, self, Message)
6
6
  end
7
7
  end
8
8
  end
@@ -1,20 +1,20 @@
1
1
  module Layer
2
2
  module Resources
3
3
  class User < Layer::Resource
4
- def self.find(url, id)
5
- new({"id" => id, "url" => "users/#{id}"})
4
+ def self.find(client, url, id)
5
+ new({"id" => id, "url" => "users/#{id}"}, client)
6
6
  end
7
7
 
8
8
  def blocks
9
- Layer::ResourceProxy.new(self, Layer::Resources::Block)
9
+ Layer::ResourceProxy.new(client, self, Layer::Resources::Block)
10
10
  end
11
11
 
12
12
  def conversations
13
- Layer::ResourceProxy.new(self, Layer::Resources::Conversation)
13
+ Layer::ResourceProxy.new(client, self, Layer::Resources::Conversation)
14
14
  end
15
15
 
16
16
  def messages
17
- Layer::ResourceProxy.new(self, Layer::Resources::Message)
17
+ Layer::ResourceProxy.new(client, self, Layer::Resources::Message)
18
18
  end
19
19
  end
20
20
  end
data/lib/layer/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Layer
2
2
  module Api
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: layer-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jake Kelly
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-03 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -191,7 +191,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
191
191
  version: '0'
192
192
  requirements: []
193
193
  rubyforge_project:
194
- rubygems_version: 2.4.5.1
194
+ rubygems_version: 2.5.2
195
195
  signing_key:
196
196
  specification_version: 4
197
197
  summary: A ruby toolkit for Layer's Web API's (https://developer.layer.com/docs)