contactually 0.0.2 → 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: 8707975aafa15dbd19fbda663a7562c8a633f94d
4
- data.tar.gz: c1de67c3c986cc9f5136c057e243b874c1b355b4
3
+ metadata.gz: 2744934fe9ac93c12fb2ce2919ab4c25a15cecf7
4
+ data.tar.gz: 1211d34bee6b7e0bfd342c83138d7e5c5a5da233
5
5
  SHA512:
6
- metadata.gz: 943faedb5c5ce0b84c730ada77ba7e6d27a5d80e97ebd55d76948c51dea69b1c09851b28e859532d6e930e9254e9a8256340501fd5b3f9e45010c06647cf19e2
7
- data.tar.gz: 5f29c612e1c91bc08ac32b986cce94c543015d77e11c59e12c77512bdf3e2d6a3d6fd4f8a627113961dbf8fa8474ebe2feb9a962aa66a438fa52bdabbb1248db
6
+ metadata.gz: a8ce41005b490e177675b8789f64e3f7c0a6678d0b86614f635871d34b28f84ed90824d83c26c23cb6c4eed207f15bd57ce9127f214be61948670fa9c2a29d13
7
+ data.tar.gz: fcf8965833261d1e223737a4da533d76b8190fef3822a937865ccb3bc1160fcdab9c0c05252429d0dc7bc1d7ea19babcf30d2afaf3471319482fe2346fe53774
@@ -1,3 +1,3 @@
1
1
  module Contactually
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/contactually.rb CHANGED
@@ -4,7 +4,12 @@ require "json"
4
4
  module Contactually
5
5
  class API
6
6
 
7
- def initialize(api_key, headers = {} )
7
+ HTTP_ACTIONS = %w(get post put delete).freeze
8
+ METHODS = %w(contacts notes groupings).freeze
9
+
10
+ attr_reader :http_action, :contactually_method
11
+
12
+ def initialize(api_key, headers = {})
8
13
  @api_key = api_key
9
14
 
10
15
  # The Contactually API recommends passing in a User-Agent header unique to
@@ -18,42 +23,48 @@ module Contactually
18
23
  @headers = headers
19
24
  end
20
25
 
21
- def call(method, args)
22
- parsed_response = make_call(method, args)
23
- parsed_response
26
+ def call(method, params)
27
+ if (parsed_response = make_call(method, params))
28
+ parsed_response
29
+ else
30
+ false
31
+ end
24
32
  end
25
33
 
26
34
  def method_missing(method, *args)
27
- call(method, args)
35
+ call(method, args.try(:first) || {})
28
36
  end
29
37
 
30
38
  private
31
- def param_fields(args)
32
- params = {}
33
- params = args.first.merge(params) if args.first
39
+
40
+ def param_fields(params)
41
+ return params.to_json unless %w(get delete).include?(http_action)
34
42
  params
35
43
  end
36
44
 
37
- def make_call(method, args={})
38
- http_method, contactually_method = get_methods(method)
39
- uri = build_uri(contactually_method, args)
40
- response = Curl.send(http_method.to_sym, uri, JSON.dump(param_fields(args))) do |curl|
45
+ def make_call(composite, params = {})
46
+ return false unless get_methods(composite)
47
+ uri = build_uri(contactually_method, params)
48
+ response = Curl.send(http_action.to_sym, uri, param_fields(params)) do |curl|
41
49
  @headers.each do |header_name, header_value|
42
50
  curl.headers[header_name.to_s.titleize.gsub(" ", "-")] = header_value
43
51
  end
44
- curl.headers['Accept'] = 'application/json'
52
+ curl.headers['Accept'] = 'application/json'
45
53
  curl.headers['Content-Type'] = 'application/json'
46
54
  end
47
55
  JSON.load("[#{response.body_str}]").first
48
56
  end
49
57
 
50
- def build_uri(contactually_method, args={})
51
- #return "https://www.contactually.com/api/v1/#{contactually_method}/#{args[:id]}.json" if args[:id]
52
- "https://www.contactually.com/api/v1/#{contactually_method}.json?api_key=#{@api_key}"
58
+ def build_uri(contactually_method, args = {})
59
+ id_arg = args.has_key?(:id) ? "/#{args["id"]}" : ""
60
+ "https://www.contactually.com/api/v1/#{contactually_method}#{id_arg}.json?api_key=#{@api_key}"
53
61
  end
54
62
 
55
- def get_methods(method)
56
- methods = method.to_s.split("_", 2)
63
+ def get_methods(composite)
64
+ http_action, contactually_method = composite.to_s.split("_", 2)
65
+ return false unless HTTP_ACTIONS.include?(http_action)
66
+ return false unless METHODS.include?(contactually_method)
67
+ [@http_action = http_action, @contactually_method = contactually_method]
57
68
  end
58
69
 
59
70
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contactually
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guille Carlos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-20 00:00:00.000000000 Z
11
+ date: 2015-02-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Write a gem description
14
14
  email: