driftrock-service-temp 0.2.0

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.
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ .idea
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+ *.swo
20
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in driftrock-service.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Max
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
+ # Driftrock::Service
2
+
3
+ Gem to make it easier to talk to the driftrock api from your app.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'driftrock-service'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install driftrock-service
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,15 @@
1
+ require "bundler/gem_tasks"
2
+ require 'driftrock-service'
3
+ require 'rspec/core/rake_task'
4
+ require 'rspec'
5
+
6
+ RSpec::Core::RakeTask.new('spec')
7
+
8
+ #require 'rake/testtask'
9
+ #
10
+ #Rake::TestTask.new do |t|
11
+ # t.libs << 'test'
12
+ #end
13
+ #
14
+ #desc "Run tests"
15
+ #task :default => :test
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'driftrock-service/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "driftrock-service-temp"
8
+ gem.version = Driftrock::Service::VERSION
9
+ gem.authors = ["Max"]
10
+ gem.email = ["max.dupenois@forward.co.uk"]
11
+ gem.description = %q{Gem for talking to the driftrock api}
12
+ gem.summary = %q{Maintains the api endpoints and the calls to them with authorisation, etc.}
13
+ gem.homepage = "https://github.com/forward/driftrock-service"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ gem.add_runtime_dependency 'httparty'
20
+ gem.add_development_dependency 'rspec'
21
+ end
@@ -0,0 +1,58 @@
1
+ require "driftrock-service/version"
2
+ require "driftrock-service/config"
3
+ require "driftrock-service/connector"
4
+ require "driftrock-service/api"
5
+ require "driftrock-service/api/administrative"
6
+ require "driftrock-service/api/data"
7
+ require "driftrock-service/api/puller"
8
+ require "driftrock-service/errors/driftrock_error"
9
+
10
+
11
+ module Driftrock
12
+ module Service
13
+ module ClassMethods
14
+ def act_as_driftrock_app
15
+ if respond_to?(:before_filter)
16
+ before_filter :authorise
17
+ end
18
+ include Driftrock::Service::Api::Data
19
+ end
20
+
21
+ def act_as_driftrock_administrator
22
+ include Driftrock::Service::Api::Administrative
23
+ end
24
+
25
+ def act_as_driftrock_puller
26
+ include Driftrock::Service::Api::Puller
27
+ end
28
+ end
29
+
30
+ def self.included(base)
31
+ base.extend ClassMethods
32
+ end
33
+
34
+ def authorise
35
+ return if session && session[:user_id] && session[:company_id]
36
+ timestamp = params[:timestamp]
37
+ company_id= params[:company_id]
38
+ token = params[:token]
39
+
40
+ expected_token = token_for(timestamp, company_id)
41
+ puts "expected_token: #{expected_token}"
42
+
43
+ if token != expected_token
44
+ redirect_to Driftrock::Service::Config.unauthorised_path
45
+ else
46
+ session[:user_id] = params[:user_id]
47
+ session[:company_id] = params[:company_id]
48
+ end
49
+ end
50
+
51
+ def token_for(timestamp, company_id)
52
+ OpenSSL::Digest::SHA1.hexdigest(
53
+ [timestamp,Driftrock::Service::Config.salt,company_id].join(":")
54
+ )
55
+ end
56
+
57
+ end
58
+ end
@@ -0,0 +1,46 @@
1
+ module Driftrock
2
+ module Service
3
+ module Api
4
+ def get(url, opts={})
5
+ api_request do |user_id, company_id|
6
+ Driftrock::Service::Connector.get_from_api(
7
+ url, user_id, company_id, opts
8
+ )
9
+ end
10
+ end
11
+
12
+ def put(url, opts={})
13
+ api_request do |user_id, company_id|
14
+ Driftrock::Service::Connector.put_to_api(
15
+ url, user_id, company_id, opts
16
+ )
17
+ end
18
+ end
19
+
20
+ def post(url, opts={})
21
+ api_request do |user_id, company_id|
22
+ Driftrock::Service::Connector.post_to_api(
23
+ url, user_id, company_id, opts
24
+ )
25
+ end
26
+ end
27
+
28
+ private
29
+ def api_request
30
+ user_id, company_id = nil
31
+ begin
32
+ sess = session.reduce({}){|h, (k, v)| h[k.to_sym] = v; h}
33
+ user_id = sess[:user_id]
34
+ company_id = sess[:company_id]
35
+ rescue => e
36
+ end
37
+ response = yield(user_id, company_id)
38
+ if response['errors']
39
+ raise Errors::DriftrockError.new(response['errors'])
40
+ else
41
+ response['data']
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,91 @@
1
+ module Driftrock
2
+ module Service
3
+ module Api
4
+ module Administrative
5
+ include Driftrock::Service::Api
6
+
7
+ module URLs
8
+ BASE = '/dashboard_api'
9
+ USERS = BASE+'/user'
10
+ CHANNELS = BASE+'/channel'
11
+ APPS = BASE+'/driftrock_app'
12
+ end
13
+
14
+ def list_apps
15
+ get(URLs::APPS+"/")
16
+ end
17
+
18
+ def get_app(app_id)
19
+ get(URLs::APPS+"/#{app_id}")
20
+ end
21
+
22
+ def add_twitter_details(username, password)
23
+ post(URLs::CHANNELS+"/temp/connect_twitter", {username: username, password: password})
24
+ end
25
+
26
+ def auth_token(app_id, timestamp, company_id)
27
+ response = post(URLs::APPS+"/#{app_id}/auth_token",
28
+ {company_id: company_id, timestamp: timestamp})
29
+ response['token']
30
+ end
31
+
32
+ def activate_app(app_id)
33
+ post(URLs::APPS+"/#{app_id}/activate")
34
+ end
35
+
36
+ def channel_types
37
+ get(URLs::CHANNELS+"/types")
38
+ end
39
+
40
+ def add_channel(channel_name, channel_data)
41
+ post(URLs::CHANNELS+"/#{channel_name}", channel_data)
42
+ end
43
+
44
+ def get_channel(channel_name)
45
+ get(URLs::CHANNELS+"/#{channel_name}")
46
+ end
47
+
48
+ def add_profile_for_channel(channel_name, profile_id)
49
+ post(URLs::CHANNELS+"/#{channel_name}/profile", {profile_id: profile_id})
50
+ end
51
+
52
+ def new_token_for_channel(channel_name, token)
53
+ put(URLs::CHANNELS+"/#{channel_name}/profile", token)
54
+ end
55
+
56
+ def driftrock_login(validation_data)
57
+ post(URLs::USERS+"/login", validation_data)
58
+ end
59
+
60
+ def new_account(user_data)
61
+ post(URLs::USERS+"/new", user_data)
62
+ end
63
+
64
+ def add_company(company_data)
65
+ post(URLs::USERS+"/company", company_data)
66
+ end
67
+
68
+ def get_user(user_session_id)
69
+ get(URLs::USERS+"/#{user_session_id}")
70
+ end
71
+
72
+ def get_company(company_id)
73
+ get(URLs::USERS+"/company/#{company_id}")
74
+ end
75
+
76
+ def get_companies
77
+ get(URLs::USERS+"/companies")
78
+ end
79
+
80
+ def user_admin?(user_session_id)
81
+ response = get(URLs::USERS+"/#{user_session_id}/is_admin")
82
+ response['is_admin'] =~ /true/
83
+ end
84
+
85
+ def new_company(company_data)
86
+ post(URLs::COMPANIES+"/new", company_data)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,20 @@
1
+ module Driftrock
2
+ module Service
3
+ module Api
4
+ module Data
5
+ include Driftrock::Service::Api
6
+ ENDPOINT = "/api"
7
+
8
+ def driftrock_data(channel, opts ={})
9
+ get(ENDPOINT+'/'+channel.to_s, opts)
10
+ end
11
+
12
+ def driftrock_query(query)
13
+ get(ENDPOINT+'/query', {query: query})
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,66 @@
1
+ module Driftrock
2
+ module Service
3
+ module Api
4
+ module Puller
5
+ include Driftrock::Service::Api
6
+ module URLs
7
+ BASE = "/puller_api"
8
+ TOKENS= BASE+"/client_tokens"
9
+ ADVERT_STATES = BASE+"/advert_states"
10
+ CONVERSION_STATES = BASE+"/conversion_states"
11
+ end
12
+
13
+ def tokens(channel)
14
+ get(URLs::TOKENS+"/#{channel}")
15
+ end
16
+
17
+ def temp_auth_tokens(channel)
18
+ get(URLs::TOKENS+"/#{channel}/temp")
19
+ end
20
+
21
+ def refresh_token(channel, company_id, profile_id,
22
+ access_token, refresh_token,
23
+ expires_in, expires_at
24
+ )
25
+ post(URLs::TOKENS+"/refreshed/#{channel}",
26
+ {
27
+ company_id: company_id,
28
+ profile_id: profile_id,
29
+ access_token: access_token,
30
+ refresh_token: refresh_token,
31
+ expires_in: expires_in,
32
+ expires_at: expires_at.strftime(datetime_format)
33
+ })
34
+ end
35
+
36
+ def advert_states(channel, date, company_id, profile_id, states)
37
+ post(URLs::ADVERT_STATES+"/#{channel}",
38
+ {
39
+ date: date.strftime(datetime_format),
40
+ company_id: company_id,
41
+ profile_id: profile_id,
42
+ advert_states: states
43
+ }
44
+ )
45
+ end
46
+
47
+ def conversion_states(channel, date, company_id, profile_id, states)
48
+ post(URLs::CONVERSION_STATES+"/#{channel}",
49
+ {
50
+ date: date.strftime(datetime_format),
51
+ company_id: company_id,
52
+ profile_id: profile_id,
53
+ conversion_states: states
54
+ }
55
+ )
56
+
57
+ end
58
+
59
+ def datetime_format
60
+ "%Y-%m-%d %H:%M:%S %z"
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+
@@ -0,0 +1,36 @@
1
+ module Driftrock
2
+ module Service
3
+ class Config < Struct.new(:salt,:base_url,:app_id,:api_token,
4
+ :unauthorised_path)
5
+ private_class_method :new
6
+
7
+ def self.base_url
8
+ @config.base_url
9
+ end
10
+
11
+ def self.salt
12
+ @config.salt
13
+ end
14
+
15
+ def self.api_token
16
+ @config.api_token
17
+ end
18
+
19
+ def self.unauthorised_path
20
+ @config.unauthorised_path
21
+ end
22
+
23
+ def self.app_id
24
+ @config.app_id
25
+ end
26
+
27
+ def self.init
28
+ config = new
29
+ yield(config)
30
+ config.base_url.gsub(/\/$/, "")
31
+ @config = config
32
+ end
33
+
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,49 @@
1
+ require 'httparty'
2
+ module Driftrock
3
+ module Service
4
+ module Connector
5
+
6
+ def build_url_for_driftrock(url)
7
+ url = "/"+url if url !~ /^\/.*/
8
+ Driftrock::Service::Config.base_url+url
9
+ end
10
+
11
+ def post_to_api(url, user_id, company_id, data)
12
+ response = HTTParty.post(build_url_for_driftrock(url), :body => body_for(user_id, company_id, data))
13
+ puts "[MD => CALLING #{build_url_for_driftrock(url)} WITH:"
14
+ puts body_for(user_id, company_id, data)
15
+ puts "[MD] => RESPONSE IS:"
16
+ puts response
17
+ puts "========="
18
+ response
19
+ end
20
+ def get_from_api(url, user_id, company_id, data={})
21
+ response = HTTParty.get(build_url_for_driftrock(url), :body => body_for(user_id, company_id, data))
22
+ puts "[MD => CALLING #{build_url_for_driftrock(url)} WITH:"
23
+ puts body_for(user_id, company_id, data)
24
+ puts "[MD] => RESPONSE IS:"
25
+ puts response
26
+ puts "========="
27
+ response
28
+ end
29
+
30
+ def put_to_api(url, user_id, company_id, data={})
31
+ HTTParty.put(build_url_for_driftrock(url), :body => body_for(user_id, company_id, data))
32
+ end
33
+
34
+
35
+ def body_for(user_id, company_id, data)
36
+
37
+ data = {:company_id => company_id}.merge(data)
38
+ { :data => data,
39
+ :auth => {
40
+ :session_id => user_id,
41
+ :app_id => Driftrock::Service::Config.app_id,
42
+ :api_token => Driftrock::Service::Config.api_token
43
+ }
44
+ }
45
+ end
46
+ module_function :get_from_api, :post_to_api, :body_for, :build_url_for_driftrock
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,17 @@
1
+ module Driftrock
2
+ module Errors
3
+
4
+ class DriftrockError < StandardError
5
+
6
+ attr_reader :errors
7
+
8
+ def initialize(list_of_errors)
9
+ result = super()
10
+ @errors = list_of_errors
11
+ result
12
+ end
13
+
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Driftrock
2
+ module Service
3
+ VERSION = "0.2.0"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe AdministrativeApiTest do
4
+ it "should be able to make a call to the api" do
5
+ #Assume for the sake of testing there's at least one app
6
+ AdministrativeApiTest.new.apps.count.should be > 0
7
+ end
8
+
9
+ end
@@ -0,0 +1,8 @@
1
+ class AdministrativeApiTest
2
+ include Driftrock::Service::Api::Administrative
3
+
4
+ def test
5
+ apps
6
+ end
7
+
8
+ end
@@ -0,0 +1,10 @@
1
+ require 'driftrock-service'
2
+ require File.join(File.dirname(__FILE__), *%w[models administrative_api_test.rb])
3
+
4
+ Driftrock::Service::Config.init do |config|
5
+ config.base_url = "http://localhost:9393"
6
+ config.salt = "3kllki3weid9n3rljcw932oknc2ico23nlckno2i3f2on2o4ilknc2il3jp23mn"
7
+ config.api_token = "45asf42cwewfwf234/sfw34"
8
+ config.app_id = "23df434v34gdfnrnrt/regergw4"
9
+ config.unauthorised_path = "/unauthorised"
10
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: driftrock-service-temp
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.2.0
6
+ platform: ruby
7
+ authors:
8
+ - Max
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2013-05-17 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :development
36
+ version_requirements: *id002
37
+ description: Gem for talking to the driftrock api
38
+ email:
39
+ - max.dupenois@forward.co.uk
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - LICENSE.txt
50
+ - README.md
51
+ - Rakefile
52
+ - driftrock-service.gemspec
53
+ - lib/driftrock-service.rb
54
+ - lib/driftrock-service/api.rb
55
+ - lib/driftrock-service/api/administrative.rb
56
+ - lib/driftrock-service/api/data.rb
57
+ - lib/driftrock-service/api/puller.rb
58
+ - lib/driftrock-service/config.rb
59
+ - lib/driftrock-service/connector.rb
60
+ - lib/driftrock-service/errors/driftrock_error.rb
61
+ - lib/driftrock-service/version.rb
62
+ - spec/administrative_api_spec.rb
63
+ - spec/models/administrative_api_test.rb
64
+ - spec/spec_helper.rb
65
+ homepage: https://github.com/forward/driftrock-service
66
+ licenses: []
67
+
68
+ post_install_message:
69
+ rdoc_options: []
70
+
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.24
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Maintains the api endpoints and the calls to them with authorisation, etc.
92
+ test_files:
93
+ - spec/administrative_api_spec.rb
94
+ - spec/models/administrative_api_test.rb
95
+ - spec/spec_helper.rb