girlscout 0.1.1 → 0.1.2

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: c5fa3f67f3bf514d55a3582c9ce93560531cb7c8
4
- data.tar.gz: b87bf3f9d6f3247c4342f5ec6b399260c8ab767b
3
+ metadata.gz: 8a99e3a73cad13c9706618f33156409078b88197
4
+ data.tar.gz: c5b8f193355bcb154ed20585a66d0c4fa51bafe1
5
5
  SHA512:
6
- metadata.gz: 14544dd9c72f6d11cb7228e96e86b926d641c0ec1b015abc197c2c0fdc80b144a716c47b830a379757a1b08baf5fc565f70d3cb23b6d3df15570c25696507991
7
- data.tar.gz: 626e846be80ea33367525ce3dbec8eb29e0d1536f88af695effcfbd9ba2aab25dd435c67a99ca57da9abcfb9b441dcb3149bf88c8e32de720b5c290c6099cd47
6
+ metadata.gz: e2662d4c3defe52c44bd6b1e821fbd0310574623afd5ad532192080ba3edc37e0c85af3b7bfd39b70ad44126727283d7218151854ea538c8556f22ead779ea62
7
+ data.tar.gz: 66165ecdec0431b54f53e65c8970538a8bdbf3d565ce03fc984ebefcca40d14300c1906e03ab0e6c5827ac091df260f7db3827ec20c8bab4859eecf103c186c2
@@ -0,0 +1,4 @@
1
+ module GirlScout
2
+ class Attachment < GirlScout::Object
3
+ end
4
+ end
@@ -0,0 +1,44 @@
1
+ module GirlScout
2
+ module Concerns
3
+ module HasAttributes
4
+ def attributes
5
+ @attributes || {}
6
+ end
7
+
8
+ def as_json
9
+ @attributes
10
+ end
11
+
12
+ def [](key)
13
+ @attributes[attr_key(key)]
14
+ end
15
+
16
+ def []=(key, value)
17
+ @attributes[attr_key(key)] = value
18
+ end
19
+
20
+ def key?(key)
21
+ @attributes.key?(attr_key(key))
22
+ end
23
+
24
+ def respond_to?(method_name)
25
+ key?(method_name) || super
26
+ end
27
+
28
+ def method_missing(method_name, *args, &block)
29
+ key = attr_key(method_name)
30
+ return super unless key?(key)
31
+
32
+ @attributes[key]
33
+ end
34
+
35
+ protected
36
+
37
+ def attr_key(sym)
38
+ parts = sym.to_s.split('_') # camelize w/o active support
39
+ key = parts[0] + parts[1..-1].map(&:capitalize).join('')
40
+ end
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,36 @@
1
+ module GirlScout
2
+ module Concerns
3
+ module HasResource
4
+ attr_writer :resource
5
+
6
+ def self.included(klass)
7
+ klass.extend self
8
+ end
9
+
10
+ def resource
11
+ @resource ||= build_resource
12
+ end
13
+
14
+ def resource_url
15
+ "#{GirlScout::Config.api_prefix}#{@path}"
16
+ end
17
+
18
+ def endpoint(path)
19
+ @path = path
20
+ end
21
+
22
+ private
23
+
24
+ def build_resource
25
+ klass = self.class
26
+ return klass.resource if klass.respond_to?(:resource)
27
+
28
+ Resource.new(resource_url, {
29
+ user: Config.api_key,
30
+ password: 'X'
31
+ })
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,19 @@
1
+ module GirlScout
2
+ DEFAULT_API_PREFIX = 'https://api.helpscout.net/v1'
3
+
4
+ class Config
5
+ class << self
6
+ attr_accessor :api_key
7
+ attr_accessor :api_prefix
8
+
9
+ def api_prefix
10
+ @api_prefix || DEFAULT_API_PREFIX
11
+ end
12
+
13
+ def reset!
14
+ @api_key = nil
15
+ @api_prefix = DEFAULT_API_PREFIX
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,39 @@
1
+ module GirlScout
2
+ class Conversation < GirlScout::Object
3
+ endpoint '/conversations'
4
+
5
+ class << self
6
+ def find(id)
7
+ Conversation.new(resource["/#{id}"].get["item"])
8
+ end
9
+
10
+ def create(attributes)
11
+ attributes = attributes.as_json if attributes.is_a?(Object)
12
+ attributes["reload"] ||= true
13
+ Conversation.new(resource.post(attributes)["item"])
14
+ end
15
+ end
16
+
17
+ def customer
18
+ # TODO: Test
19
+ @customer ||= Customer.new(self["customer"] || {})
20
+ end
21
+
22
+ def threads
23
+ @threads ||= (self["threads"] || []).map { |attr| Thread.new(attr) }
24
+ end
25
+
26
+ def mailbox
27
+ @mailbox ||= Mailbox.new(self["mailbox"] || {})
28
+ end
29
+
30
+ def as_json
31
+ # TODO: Test
32
+ json = super.dup
33
+ json["customer"] = customer.as_json if key?("customer")
34
+ json["threads"] = threads.map(&:as_json) if key?("threads")
35
+ json["mailbox"] = mailbox.as_json if key?("mailbox")
36
+ json
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ module GirlScout
2
+ class Customer < GirlScout::Object
3
+ endpoint '/customers'
4
+
5
+ class << self
6
+ def all
7
+ List.new(resource.get, Customer)
8
+ end
9
+
10
+ def find(id)
11
+ Customer.new(resource["/#{id}"].get["item"])
12
+ end
13
+ end
14
+
15
+ def as_json
16
+ json = super
17
+ json["type"] = "customer"
18
+ json
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ module GirlScout
2
+ class Folder < GirlScout::Object
3
+ end
4
+ end
@@ -0,0 +1,28 @@
1
+ module GirlScout
2
+ class List < GirlScout::Object
3
+ include Enumerable
4
+
5
+ attr_accessor :item_class
6
+ alias_method :size, :count
7
+ alias_method :length, :count
8
+
9
+ def initialize(attr, item_class)
10
+ super(attr)
11
+ @item_class = item_class
12
+ end
13
+
14
+ def items
15
+ @items ||= (@attributes["items"] || []).map do |attr|
16
+ @item_class.new(attr)
17
+ end
18
+ end
19
+
20
+ def [](index)
21
+ items[index]
22
+ end
23
+
24
+ def each(&block)
25
+ items.each(&block)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,49 @@
1
+ module GirlScout
2
+ class Mailbox < GirlScout::Object
3
+ endpoint '/mailboxes'
4
+
5
+ class << self
6
+ def all
7
+ List.new(resource.get, Mailbox)
8
+ end
9
+
10
+ def find(id)
11
+ Mailbox.new(resource["/#{id}"].get["item"])
12
+ end
13
+ end
14
+
15
+ def folders
16
+ @folders ||= List.new(folder_resource.get, Folder)
17
+ end
18
+
19
+ def conversations
20
+ @conversations ||= List.new(conversation_resource.get, Conversation)
21
+ end
22
+
23
+ def users
24
+ @users ||= List.new(user_resource.get, User)
25
+ end
26
+
27
+ def customers
28
+ @customers ||= List.new(customer_resource.get, Customer)
29
+ end
30
+
31
+ protected
32
+
33
+ def folder_resource
34
+ @folder_resource ||= resource["/#{id}/folders"]
35
+ end
36
+
37
+ def conversation_resource
38
+ @conversation_resource ||= resource["/#{id}/conversations"]
39
+ end
40
+
41
+ def user_resource
42
+ @user_resource ||= resource["/#{id}/users"]
43
+ end
44
+
45
+ def customer_resource
46
+ @customer_resource ||= resource["/#{id}/customers"]
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ module GirlScout
2
+ class Object
3
+ include GirlScout::Concerns::HasAttributes
4
+ include GirlScout::Concerns::HasResource
5
+
6
+ def initialize(attr={}, options={})
7
+ attr = attr.attributes if attr.is_a?(Object)
8
+ attr = attr.inject({}) do |hash,(k,v)|
9
+ hash[attr_key(k)] = v
10
+ hash
11
+ end
12
+
13
+ @attributes = attr
14
+ @resource = options[:resource] if options[:resource]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,50 @@
1
+ require 'json'
2
+
3
+ module GirlScout
4
+ class Resource
5
+ def initialize(url="", options={})
6
+ @url = url
7
+ @options = options
8
+ end
9
+
10
+ def rest_resource
11
+ return @rest_resource if @rest_resource
12
+ return @rest_resource = @options[:resource] if @options[:resource]
13
+
14
+ resource_class = @options[:resource_class] || RestClient::Resource
15
+ @rest_resource = resource_class.new("#{@url}.json", @options)
16
+ end
17
+
18
+ def url
19
+ rest_resource.url
20
+ end
21
+
22
+ def [](path)
23
+ Resource.new("#{@url}#{path}", @options)
24
+ end
25
+
26
+ def get(query=nil)
27
+ opts = { }
28
+ opts[:headers] = { params: query } if query
29
+
30
+ parse(rest_resource.get(opts))
31
+ end
32
+
33
+ [:put, :post, :patch, :delete].each do |method|
34
+ define_method(method.to_s) do |payload=nil|
35
+ payload = serialize(payload) if payload
36
+ parse(rest_resource.send(method, payload, content_type: :json))
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def parse(response)
43
+ JSON.load(response)
44
+ end
45
+
46
+ def serialize(payload)
47
+ payload.to_json
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,15 @@
1
+ module GirlScout
2
+ class Thread < GirlScout::Object
3
+ def attachments
4
+ @attachments ||= (self["attachments"] || []).map { |attr| Attachment.new(attr) }
5
+ end
6
+
7
+ def created_by
8
+ return @created_by if @created_by
9
+
10
+ attr = @attributes["createdBy"]
11
+ creator_type = attr["type"].capitalize.constantize rescue User
12
+ @created_by ||= creator_type.new(attr)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ module GirlScout
2
+ class User < GirlScout::Object
3
+ endpoint '/users'
4
+
5
+ class << self
6
+ def all
7
+ List.new(resource.get, User)
8
+ end
9
+
10
+ def find(id)
11
+ User.new(resource["/#{id}"].get["item"])
12
+ end
13
+
14
+ def me
15
+ find("me")
16
+ end
17
+ end
18
+
19
+ def as_json
20
+ json = super
21
+ json["type"] = "user"
22
+ json
23
+ end
24
+ end
25
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: girlscout
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chakrit Wichian
@@ -115,6 +115,19 @@ extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
117
  - lib/girlscout.rb
118
+ - lib/girlscout/attachment.rb
119
+ - lib/girlscout/concerns/has_attributes.rb
120
+ - lib/girlscout/concerns/has_resource.rb
121
+ - lib/girlscout/config.rb
122
+ - lib/girlscout/conversation.rb
123
+ - lib/girlscout/customer.rb
124
+ - lib/girlscout/folder.rb
125
+ - lib/girlscout/list.rb
126
+ - lib/girlscout/mailbox.rb
127
+ - lib/girlscout/object.rb
128
+ - lib/girlscout/resource.rb
129
+ - lib/girlscout/thread.rb
130
+ - lib/girlscout/user.rb
118
131
  homepage: https://www.omise.co
119
132
  licenses:
120
133
  - MIT