tictail-api 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 78711afe2411c0fc32c9050a2aa6df9d660084b7
4
+ data.tar.gz: 424692df03b46dac4cb90309b122f9e2d5c91792
5
+ SHA512:
6
+ metadata.gz: 284eb4af4386d7fed431f96db3c2f248e6f8454c3e9e621b36c2831b51bf931bce8b02ca21960b948fa0718f43d019a239bf62eb9c73e8d837124b1ccddbb0e7
7
+ data.tar.gz: 7d6fe7aeeabc660ff4ba6c6a4e0a4d0d57d9478f8ee61b79271b2862b83d198583d3082d04df50297db0468e20338a2601edbdeec15c7c42077368d1335a7e15
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tictail-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Alon Braitstein
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Tictail::Api
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'tictail-api'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install tictail-api
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/changelog ADDED
@@ -0,0 +1,2 @@
1
+ 2013-07-25 Alon Braitstein
2
+ 1. Initial commit
@@ -0,0 +1,18 @@
1
+ module Tictail
2
+ module Api
3
+ module Authenticate
4
+ def authenticate(params)
5
+ params[:grant_type] = 'authorization_code'
6
+ response = Faraday.post do |req|
7
+ req.url 'https://tictail.com/oauth/token'
8
+ req.headers['Content-Type'] = 'application/x-www-form-urlencoded'
9
+ req.body = params.to_param
10
+ end
11
+ if response.status == 200 && response.body
12
+ response_hash = Oj.load(response.body, mode: :compat)
13
+ response_hash
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,99 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+ require 'tictail/faraday_middleware/response_parser'
4
+ require 'tictail/faraday_middleware/encode_oj'
5
+ require 'tictail/faraday_middleware/parse_oj'
6
+ require 'typhoeus'
7
+ require 'typhoeus/adapters/faraday'
8
+
9
+ require 'tictail/api/authenticate'
10
+ require 'tictail/api/ping'
11
+ require 'tictail/helper'
12
+ require 'tictail/api/order'
13
+ require 'tictail/api/product'
14
+ require 'tictail/api/customer'
15
+
16
+
17
+
18
+ module Tictail
19
+ class Client
20
+ include Tictail::Helper
21
+ include Tictail::Api::Authenticate
22
+ include Tictail::Api::Ping
23
+ include Tictail::Api::Order
24
+ include Tictail::Api::Product
25
+ include Tictail::Api::Customer
26
+
27
+ attr_accessor :access_token
28
+
29
+ #
30
+ # Creates a new instance of Tictail::Api::Client
31
+ #
32
+ # @param url [String] The url to Tictail api (https://api.tictail.com)
33
+ def initialize(url = 'https://api.tictail.com')
34
+ @url = url
35
+ end
36
+
37
+
38
+ #
39
+ # Does a GET request to the url with the params
40
+ #
41
+ # @param url [String] the relative path in the Tictail API
42
+ # @param params [Hash] the url params that should be passed in the request
43
+ def get(url, params = {})
44
+ params = params.inject({}){|memo,(k,v)| memo[k.to_s] = v; memo}
45
+ @access_token = params.delete('access_token') if params['access_token']
46
+ return connection.get(url, params)
47
+ end
48
+
49
+ #
50
+ # Does a POST request to the url with the params
51
+ #
52
+ # @param url [String] the relative path in the Tictail API
53
+ # @param params [Hash] the body of the request
54
+ def post(url, params)
55
+ params = convert_hash_keys(params)
56
+ @access_token = params.delete('access_token') if params['access_token']
57
+ return connection.post(url, params)
58
+ end
59
+
60
+ #
61
+ # Does a PUT request to the url with the params
62
+ #
63
+ # @param url [String] the relative path in the Tictail API
64
+ # @param params [Hash] the body of the request
65
+ def put(url, params)
66
+ params = convert_hash_keys(params)
67
+ @access_token = params.delete('access_token') if params['access_token']
68
+ return connection.put(url, params)
69
+ end
70
+
71
+ #
72
+ # Does a DELETE request to the url with the params
73
+ #
74
+ # @param url [String] the relative path in the Tictail API
75
+ def delete(url, params)
76
+ params = convert_hash_keys(params)
77
+ @access_token = params.delete('access_token') if params['access_token']
78
+ return connection.delete(url)
79
+ end
80
+ private
81
+
82
+ def connection
83
+ header = {:tictail_api_connector => 'Ruby-'+ Tictail::Api::VERSION, 'Content-Type' => 'application/json' }
84
+ header.merge!({:Authorization => 'Bearer ' + access_token}) unless access_token.nil?
85
+ @connection ||= Faraday.new(url: @url, headers: header, ssl: {verify: true}) do |conn|
86
+
87
+ conn.use Tictail::ResponseParser
88
+
89
+ # Setting response parser to oj
90
+ conn.request :oj
91
+ conn.response :oj, :content_type => /\bjson$/
92
+
93
+ conn.adapter :typhoeus
94
+ end
95
+ end
96
+ end
97
+
98
+ class InvalidParams < Exception; end
99
+ end
@@ -0,0 +1,12 @@
1
+ module Tictail
2
+ module Api
3
+ module Customer
4
+ def get_customer params
5
+ raise InvalidParams.new('No store id spacified') unless params[:store_id]
6
+ customer_id = params[:customer_id] ? "/#{params[:customer_id]}" : ''
7
+ response = get("/v1/stores/#{params[:store_id]}/customers" + customer_id, params)
8
+ response.body
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Tictail
2
+ module Api
3
+ module Order
4
+ def get_orders params
5
+ raise InvalidParams.new('No store id spacified') unless params[:store_id]
6
+ order_id = params[:order_id] ? "/#{params[:order_id]}" : ''
7
+ response = get("/v1/stores/#{params[:store_id]}/orders" + order_id, params)
8
+ response.body
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ module Tictail
2
+ module Api
3
+ module Ping
4
+ def ping params
5
+ return get('/v1/teapot', params)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Tictail
2
+ module Api
3
+ module Product
4
+ def get_products params
5
+ raise InvalidParams.new('No store id spacified') unless params[:store_id]
6
+ response = get("/v1/stores/#{params[:store_id]}/products", params)
7
+ response.body
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Tictail
2
+ module Api
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,54 @@
1
+ require 'faraday'
2
+
3
+ module FaradayMiddleware
4
+ # Request middleware that encodes the body as JSON.
5
+ #
6
+ # Processes only requests with matching Content-type or those without a type.
7
+ # If a request doesn't have a type but has a body, it sets the Content-type
8
+ # to JSON MIME-type.
9
+ #
10
+ # Doesn't try to encode bodies that already are in string form.
11
+ class EncodeOj < Faraday::Middleware
12
+ CONTENT_TYPE = 'Content-Type'.freeze
13
+ MIME_TYPE = 'application/json'.freeze
14
+
15
+ dependency do
16
+ require 'oj' unless defined?(::Oj)
17
+ end
18
+
19
+ def call(env)
20
+ match_content_type(env) do |data|
21
+ env[:body] = encode data
22
+ end
23
+ @app.call env
24
+ end
25
+
26
+ def encode(data)
27
+ ::Oj.dump data
28
+ end
29
+
30
+ def match_content_type(env)
31
+ if process_request?(env)
32
+ env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
33
+ yield env[:body] unless env[:body].respond_to?(:to_str)
34
+ end
35
+ end
36
+
37
+ def process_request?(env)
38
+ type = request_type(env)
39
+ has_body?(env) and (type.empty? or type == MIME_TYPE)
40
+ end
41
+
42
+ def has_body?(env)
43
+ body = env[:body] and !(body.respond_to?(:to_str) and body.empty?)
44
+ end
45
+
46
+ def request_type(env)
47
+ type = env[:request_headers][CONTENT_TYPE].to_s
48
+ type = type.split(';', 2).first if type.index(';')
49
+ type
50
+ end
51
+ end
52
+ end
53
+
54
+ Faraday.register_middleware :request, oj: FaradayMiddleware::EncodeOj
@@ -0,0 +1,15 @@
1
+ require 'faraday_middleware/response_middleware'
2
+
3
+ module FaradayMiddleware
4
+ class ParseOj < ResponseMiddleware
5
+ dependency do
6
+ require 'oj' unless defined?(::Oj)
7
+ end
8
+
9
+ define_parser do |body|
10
+ ::Oj.load(body, mode: :compat) unless body.strip.empty?
11
+ end
12
+ end
13
+ end
14
+
15
+ Faraday.register_middleware :response, oj: FaradayMiddleware::ParseOj
@@ -0,0 +1,14 @@
1
+ module Tictail
2
+ class ResponseParser < Faraday::Response::Middleware
3
+ def call(env)
4
+ # "env" contains the request
5
+ @app.call(env).on_complete do
6
+ if env[:status] == 401 || env[:status] == 403
7
+ raise HTTPUnauthorized.new 'invalid storeenvy credentials'
8
+ end
9
+ end
10
+ end
11
+ class HTTPUnauthorized < Exception
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ module Tictail
2
+ module Helper
3
+ def symbolize_keys hash
4
+ return hash.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
5
+ end
6
+ def convert_hash_keys(value)
7
+ case value
8
+ when Array
9
+ value.map { |v| convert_hash_keys(v) }
10
+ when Hash
11
+ Hash[value.map { |k, v| [k.to_s, convert_hash_keys(v)] }]
12
+ else
13
+ value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ require 'tictail/api/client'
2
+
3
+ module Tictail
4
+ class << self
5
+ def client
6
+ @client ||= Tictail::Client.new()
7
+ end
8
+
9
+ #
10
+ # Makes sure that the method missing is checked with the Tictail::Client instance
11
+ #
12
+ # @param method_name [String] the name of the method we want to run
13
+ # @param include_private [Boolean] defines wether to check for private functions as well
14
+ def respond_to_missing?(method_name, include_private=false)
15
+ client.respond_to?(method_name, include_private)
16
+ end
17
+
18
+ private
19
+ #
20
+ # executes any function on the Tictail::Client instance
21
+ #
22
+ # @param args [*] any argument that we want to pass to the client function
23
+ # @param block [Block] any block that is passed to the client function
24
+ def method_missing(method_name, *args, &block)
25
+ return super unless client.respond_to?(method_name)
26
+ client.send(method_name, *args, &block)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tictail/api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'tictail-api'
8
+ spec.version = Tictail::Api::VERSION
9
+ spec.authors = ['Alon Braitstein']
10
+ spec.email = ['alon@yotpo.com']
11
+ spec.description = %q{Ruby wrapper for tictail API - written by Yotpo}
12
+ spec.summary = %q{Ruby wrapper for tictail API - written by Yotpo}
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
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
+
21
+ spec.add_development_dependency 'bundler", "~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+
24
+ spec.add_dependency 'faraday'
25
+ spec.add_dependency 'typhoeus'
26
+ spec.add_dependency 'faraday_middleware', '~> 0.9'
27
+ spec.add_dependency 'oj', '~> 2.0'
28
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tictail-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Alon Braitstein
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler", "~> 1.3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: typhoeus
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
83
+ - !ruby/object:Gem::Dependency
84
+ name: oj
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ description: Ruby wrapper for tictail API - written by Yotpo
98
+ email:
99
+ - alon@yotpo.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - changelog
110
+ - lib/tictail/api/authenticate.rb
111
+ - lib/tictail/api/client.rb
112
+ - lib/tictail/api/customer.rb
113
+ - lib/tictail/api/order.rb
114
+ - lib/tictail/api/ping.rb
115
+ - lib/tictail/api/product.rb
116
+ - lib/tictail/api/version.rb
117
+ - lib/tictail/faraday_middleware/encode_oj.rb
118
+ - lib/tictail/faraday_middleware/parse_oj.rb
119
+ - lib/tictail/faraday_middleware/response_parser.rb
120
+ - lib/tictail/helper.rb
121
+ - lib/tictail/tictail.rb
122
+ - tictail-api.gemspec
123
+ homepage: ''
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - '>='
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubyforge_project:
143
+ rubygems_version: 2.0.3
144
+ signing_key:
145
+ specification_version: 4
146
+ summary: Ruby wrapper for tictail API - written by Yotpo
147
+ test_files: []