twilio-lookups 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,36 @@
1
+ module TwilioLookups
2
+ module REST
3
+ class NextGenListResource < TwilioLookups::REST::ListResource
4
+ def list(params={}, full_path=false)
5
+ raise "Can't get a resource list without a REST Client" unless @client
6
+ response = @client.get @path, params, full_path
7
+ list_key = response['meta']['key']
8
+ raise "Couldn't find a list key in response meta" unless list_key
9
+ resources = response[list_key]
10
+ resource_list = resources.map do |resource|
11
+ @instance_class.new "#{@path}/#{resource[@instance_id_key]}", @client,
12
+ resource
13
+ end
14
+ client, list_class = @client, self.class
15
+ resource_list.instance_eval do
16
+ eigenclass = class << self; self; end
17
+ eigenclass.send :define_method, :next_page, &lambda {
18
+ if response['meta']['next_page_url']
19
+ list_class.new(response['meta']['next_page_url'], client).list({})
20
+ else
21
+ []
22
+ end
23
+ }
24
+ eigenclass.send :define_method, :previous_page, &lambda {
25
+ if response['meta']['previous_page_url']
26
+ list_class.new(response['meta']['previous_page_url'], client).list({})
27
+ else
28
+ []
29
+ end
30
+ }
31
+ end
32
+ resource_list
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,49 @@
1
+ module TwilioLookups
2
+ module REST
3
+ module Utils
4
+
5
+ def twilify(something)
6
+ return key_map(something, :twilify) if something.is_a? Hash
7
+ string = something.to_s
8
+ string.split('_').map do |string_part|
9
+ string_part[0,1].capitalize + string_part[1..-1]
10
+ end.join
11
+ end
12
+
13
+ def detwilify(something)
14
+ return key_map(something, :detwilify) if something.is_a? Hash
15
+ string = something.to_s
16
+ string = string[0,1].downcase + string[1..-1]
17
+ string.gsub(/[A-Z][a-z]*/) { |s| "_#{s.downcase}" }
18
+ end
19
+
20
+ protected
21
+
22
+ def resource(*resources)
23
+ custom_resource_names = { sms: 'SMS', sip: 'SIP' }
24
+ resources.each do |r|
25
+ resource = twilify r
26
+ relative_path = custom_resource_names.fetch(r, resource)
27
+ path = "#{@path}/#{relative_path}"
28
+ enclosing_module = if @submodule == nil
29
+ TwilioLookups::REST
30
+ else
31
+ TwilioLookups::REST.const_get(@submodule)
32
+ end
33
+ resource_class = enclosing_module.const_get resource
34
+ instance_variable_set("@#{r}", resource_class.new(path, @client))
35
+ end
36
+ self.class.instance_eval { attr_reader *resources }
37
+ end
38
+
39
+ private
40
+
41
+ def key_map(something, method)
42
+ something = something.to_a.flat_map do |pair|
43
+ [send(method, pair[0]).to_sym, pair[1]]
44
+ end
45
+ Hash[*something]
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,15 @@
1
+ module TwilioLookups
2
+ module Util
3
+ def url_encode(hash)
4
+ hash.to_a.map {|p| p.map {|e| CGI.escape get_string(e)}.join '='}.join '&'
5
+ end
6
+
7
+ def get_string(obj)
8
+ if obj.respond_to?(:strftime)
9
+ obj.strftime('%Y-%m-%d')
10
+ else
11
+ obj.to_s
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ module TwilioLookups
2
+ module Util
3
+ class ClientConfig
4
+ DEFAULTS = {
5
+ host: 'api.twilio.com',
6
+ port: 443,
7
+ use_ssl: true,
8
+ ssl_verify_peer: true,
9
+ ssl_ca_file: File.dirname(__FILE__) + '/../../../conf/cacert.pem',
10
+ timeout: 30,
11
+ proxy_addr: nil,
12
+ proxy_port: nil,
13
+ proxy_user: nil,
14
+ proxy_pass: nil,
15
+ retry_limit: 1
16
+ }
17
+
18
+ DEFAULTS.each_key do |attribute|
19
+ attr_accessor attribute
20
+ end
21
+
22
+ def initialize(opts={})
23
+ DEFAULTS.each do |attribute, value|
24
+ send("#{attribute}=".to_sym, opts.fetch(attribute, value))
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module TwilioLookups
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe TwilioLookups::REST::Lookups::PhoneNumbers do
4
+ it 'creates a phone_number object' do
5
+ client = TwilioLookups::REST::LookupsClient.new 'otherSid', 'otherToken'
6
+ expect(client).to respond_to(:phone_numbers)
7
+ end
8
+
9
+ it 'gets phone numbers without special encoding' do
10
+ number = '+13123131434'
11
+ client = TwilioLookups::REST::LookupsClient.new 'otherSid', 'otherToken'
12
+ expect(client).to receive(:get).once
13
+ .with('/v1/PhoneNumbers/+13123131434')
14
+ .and_return({ phone_number: number })
15
+ phone_number = client.phone_numbers.get('+13123131434').phone_number
16
+ expect(phone_number).to be(number)
17
+ end
18
+
19
+ it 'URI encodes phone number path parameters' do
20
+ number = '+13123131434'
21
+ client = TwilioLookups::REST::LookupsClient.new 'otherSid', 'otherToken'
22
+ expect(client).to receive(:get).once
23
+ .with('/v1/PhoneNumbers/+1%20312%20313%201434')
24
+ .and_return({ phone_number: number })
25
+ phone_number = client.phone_numbers.get('+1 312 313 1434').phone_number
26
+ expect(phone_number).to be(number)
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require 'bundler'
4
+ Bundler.setup
5
+
6
+ require 'twilio-lookups'
7
+ require 'rack'
8
+
9
+ RSpec.configure do |config|
10
+ config.expect_with :rspec do |c|
11
+ c.syntax = :expect
12
+ end
13
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twilio-lookups/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'twilio-lookups'
8
+ spec.version = TwilioLookups::VERSION
9
+ spec.authors = ['Andrew Benton']
10
+ spec.email = ['andrew@twilio.com']
11
+ spec.summary = 'A simple library for communicating with the TwilioLookups REST API, building TwiML, and generating TwilioLookups Client Capability Tokens'
12
+ spec.description = 'A simple library for communicating with the TwilioLookups REST API, building TwiML, and generating TwilioLookups Client Capability Tokens'
13
+ spec.homepage = 'http://github.com/towski/twilio-lookups'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+ spec.required_ruby_version = '>= 1.9.3'
21
+ spec.extra_rdoc_files = []
22
+ spec.rdoc_options = ['--line-numbers', '--inline-source', '--title', 'twilio-ruby', '--main', 'README.md']
23
+
24
+ spec.add_dependency('multi_json', '>= 1.3.0')
25
+ spec.add_dependency('builder', '>= 2.1.2')
26
+ spec.add_dependency('jwt', '~> 0.1.4')
27
+ spec.add_dependency('jruby-openssl') if RUBY_PLATFORM == 'java'
28
+ # Workaround for RBX <= 2.2.1, should be fixed in next version
29
+ spec.add_dependency('rubysl') if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
30
+
31
+ spec.add_development_dependency 'bundler', '~> 1.5'
32
+ spec.extra_rdoc_files = []
33
+ spec.rdoc_options = ['--line-numbers', '--inline-source', '--title', 'twilio-ruby', '--main', 'README.md']
34
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twilio-lookups
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Benton
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: builder
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: jwt
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.5'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.5'
69
+ description: A simple library for communicating with the TwilioLookups REST API, building
70
+ TwiML, and generating TwilioLookups Client Capability Tokens
71
+ email:
72
+ - andrew@twilio.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - Makefile
81
+ - Rakefile
82
+ - conf/cacert.pem
83
+ - lib/twilio-lookups.rb
84
+ - lib/twilio-lookups/rest/base_client.rb
85
+ - lib/twilio-lookups/rest/errors.rb
86
+ - lib/twilio-lookups/rest/instance_resource.rb
87
+ - lib/twilio-lookups/rest/list_resource.rb
88
+ - lib/twilio-lookups/rest/lookups/phone_numbers.rb
89
+ - lib/twilio-lookups/rest/lookups_client.rb
90
+ - lib/twilio-lookups/rest/next_gen_list_resource.rb
91
+ - lib/twilio-lookups/rest/utils.rb
92
+ - lib/twilio-lookups/util.rb
93
+ - lib/twilio-lookups/util/client_config.rb
94
+ - lib/twilio-lookups/version.rb
95
+ - spec/rest/phone_number_spec.rb
96
+ - spec/spec_helper.rb
97
+ - twilio-lookups.gemspec
98
+ homepage: http://github.com/towski/twilio-lookups
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options:
104
+ - --line-numbers
105
+ - --inline-source
106
+ - --title
107
+ - twilio-ruby
108
+ - --main
109
+ - README.md
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - '>='
115
+ - !ruby/object:Gem::Version
116
+ version: 1.9.3
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.7
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: A simple library for communicating with the TwilioLookups REST API, building
128
+ TwiML, and generating TwilioLookups Client Capability Tokens
129
+ test_files:
130
+ - spec/rest/phone_number_spec.rb
131
+ - spec/spec_helper.rb
132
+ has_rdoc: