insales_api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
data/README ADDED
@@ -0,0 +1,3 @@
1
+ add in your Gemfile
2
+
3
+ require 'insales_api'
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "insales_api/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "insales_api"
7
+ s.version = InsalesApi::VERSION
8
+ s.authors = "InSales"
9
+ s.email = "dg@insales.ru"
10
+ s.homepage = "http://wiki.insales.ru/wiki/%D0%9A%D0%B0%D0%BA_%D0%B8%D0%BD%D1%82%D0%B5%D0%B3%D1%80%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%D1%81%D1%8F_%D1%81_InSales"
11
+ s.summary = %q{Gem for accessing the InSales REST web services}
12
+ s.description = %q{Gem for accessing the InSales REST web services}
13
+
14
+ s.rubyforge_project = "insales_api"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+
20
+ s.add_dependency("activeresource", [">= 3.0.0"])
21
+ s.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,10 @@
1
+ require "active_resource"
2
+ require "digest/md5"
3
+
4
+ module InsalesApi
5
+
6
+ end
7
+
8
+ require "insales_api/resources"
9
+ require "insales_api/app"
10
+ require "insales_api/password"
@@ -0,0 +1,65 @@
1
+ require 'digest/md5'
2
+ module InsalesApi
3
+ class App
4
+ cattr_accessor :api_key, :api_host, :api_secret, :api_autologin_path
5
+ attr_accessor :shop, :password, :authorized, :auth_token
6
+
7
+ def initialize(shop, password)
8
+ @authorized = false
9
+ @shop = self.class.prepare_shop shop
10
+ @password = password
11
+ end
12
+
13
+ def authorization_url
14
+ store_auth_token
15
+ "http://#{shop}/admin/applications/#{self.class.api_key}/login?token=#{salt}&login=http://#{self.class.api_host}/#{self.class.api_autologin_path}"
16
+ end
17
+
18
+ def store_auth_token
19
+ @auth_token = InsalesApi::Password.create(password, salt)
20
+ end
21
+
22
+ def salt
23
+ @salt ||= Digest::MD5.hexdigest("Twulvyeik#{$$}#{Time.now.to_i}thithAwn")
24
+ end
25
+
26
+ def authorize token
27
+ @authorized = false
28
+ if self.auth_token == token
29
+ @authorized = true
30
+ end
31
+
32
+ @authorized
33
+ end
34
+
35
+ def authorized?
36
+ @authorized
37
+ end
38
+
39
+ def configure_api
40
+ self.class.configure_api shop, password
41
+ end
42
+
43
+ class << self
44
+ def configure_api shop, password
45
+ InsalesApi::Base.configure api_key, shop, password
46
+ end
47
+
48
+ def prepare_shop shop
49
+ shop.to_s.strip.downcase
50
+ end
51
+
52
+ def install shop, token, insales_id
53
+ true
54
+ end
55
+
56
+ def uninstall shop, password
57
+ true
58
+ end
59
+
60
+ def password_by_token token
61
+ InsalesApi::Password.create(api_secret, token)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,8 @@
1
+ require 'digest/md5'
2
+ module InsalesApi
3
+ module Password
4
+ def self.create(secret, token)
5
+ Digest::MD5.hexdigest("#{token}#{secret}")
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,12 @@
1
+ require "insales_api/resources/base.rb"
2
+ require "insales_api/resources/account.rb"
3
+ require "insales_api/resources/category.rb"
4
+ require "insales_api/resources/client.rb"
5
+ require "insales_api/resources/collection.rb"
6
+ require "insales_api/resources/option_name.rb"
7
+ require "insales_api/resources/option_value.rb"
8
+ require "insales_api/resources/order.rb"
9
+ require "insales_api/resources/product.rb"
10
+ require "insales_api/resources/variant.rb"
11
+ require "insales_api/resources/webhook.rb"
12
+
@@ -0,0 +1,13 @@
1
+ module InsalesApi
2
+ class Account < Base
3
+ def self.current
4
+ find(:one, :from => '/admin/account.xml')
5
+ end
6
+
7
+ def update
8
+ connection.put('/admin/account.xml', encode, self.class.headers).tap do |response|
9
+ load_attributes_from_response(response)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ module InsalesApi
2
+ class Base < ActiveResource::Base
3
+ self.format = :xml
4
+
5
+ def self.configure(api_key, shop, password)
6
+ self.user = api_key
7
+ self.site = "http://#{shop}/admin/"
8
+ self.password = password
9
+ return
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ class Category < Base; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ class Client < Base; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ class Collection < Base; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ class OptionName < Base; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ class OptionValue < Base; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ class Order < Base; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ class Product < Base; end
3
+ end
@@ -0,0 +1,5 @@
1
+ module InsalesApi
2
+ class Variant < Base
3
+ self.prefix = "/admin/products/:product_id/"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ class Webhook < Base; end
3
+ end
@@ -0,0 +1,3 @@
1
+ module InsalesApi
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe InsalesApi::App do
4
+
5
+ let :app do
6
+ InsalesApi::App.new('my.shop.com', 'password')
7
+ end
8
+
9
+ describe "initialize" do
10
+ it { app.shop.should be }
11
+ it { app.password.should be }
12
+ it { app.should_not be_authorized }
13
+ end
14
+
15
+ describe "configure_api" do
16
+ it 'should set site and password for InsalesApi' do
17
+ app.configure_api
18
+ InsalesApi::Base.site.to_s.should =~ /#{app.shop}/
19
+ InsalesApi::Base.password.should == app.password
20
+ end
21
+ end
22
+
23
+ describe "salt" do
24
+ it "should return md5 hexdigest" do
25
+ app.salt.should be
26
+ app.salt.length.should == Digest::MD5.hexdigest('test').length
27
+ end
28
+ end
29
+
30
+ describe "store_auth_token" do
31
+ it "should set auth_token" do
32
+ app.store_auth_token.should == InsalesApi::Password.create(app.password, app.salt)
33
+ end
34
+ end
35
+
36
+ describe "authorization_url" do
37
+ it "should set auth_token and return url" do
38
+ app.authorization_url.should == "http://#{app.shop}/admin/applications/#{InsalesApi::App.api_key}/login?token=#{app.salt}&login=http://#{InsalesApi::App.api_host}/#{InsalesApi::App.api_autologin_path}"
39
+ app.auth_token.should be
40
+ end
41
+ end
42
+
43
+ describe "authorize" do
44
+ it "should authorize by valid token" do
45
+ app.authorize app.auth_token
46
+ app.should be_authorized
47
+ end
48
+
49
+ it "should not authorize by invalid token" do
50
+ app.authorize 'bad token'
51
+ app.should_not be_authorized
52
+ end
53
+ end
54
+
55
+ describe "password_by_token" do
56
+ it "should generate password" do
57
+ InsalesApi::App.password_by_token('test').should == InsalesApi::Password.create(InsalesApi::App.api_secret, 'test')
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,19 @@
1
+ # This file is copied to spec/ when you run 'rails generate rspec:install'
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'insales_api'
4
+
5
+ InsalesApi::App.api_key = 'test'
6
+ InsalesApi::App.api_secret = 'test'
7
+ InsalesApi::App.api_host = 'myshop.insales.ru'
8
+ InsalesApi::App.api_autologin_path = 'session/autologin'
9
+
10
+ RSpec.configure do |config|
11
+ # == Mock Framework
12
+ #
13
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
14
+ #
15
+ # config.mock_with :mocha
16
+ # config.mock_with :flexmock
17
+ # config.mock_with :rr
18
+ config.mock_with :rspec
19
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: insales_api
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - InSales
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-12-28 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activeresource
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 7
29
+ segments:
30
+ - 3
31
+ - 0
32
+ - 0
33
+ version: 3.0.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: Gem for accessing the InSales REST web services
51
+ email: dg@insales.ru
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - .gitignore
60
+ - Gemfile
61
+ - README
62
+ - Rakefile
63
+ - insales_api.gemspec
64
+ - lib/insales_api.rb
65
+ - lib/insales_api/app.rb
66
+ - lib/insales_api/password.rb
67
+ - lib/insales_api/resources.rb
68
+ - lib/insales_api/resources/account.rb
69
+ - lib/insales_api/resources/base.rb
70
+ - lib/insales_api/resources/category.rb
71
+ - lib/insales_api/resources/client.rb
72
+ - lib/insales_api/resources/collection.rb
73
+ - lib/insales_api/resources/option_name.rb
74
+ - lib/insales_api/resources/option_value.rb
75
+ - lib/insales_api/resources/order.rb
76
+ - lib/insales_api/resources/product.rb
77
+ - lib/insales_api/resources/variant.rb
78
+ - lib/insales_api/resources/webhook.rb
79
+ - lib/insales_api/version.rb
80
+ - spec/lib/insales_app_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: http://wiki.insales.ru/wiki/%D0%9A%D0%B0%D0%BA_%D0%B8%D0%BD%D1%82%D0%B5%D0%B3%D1%80%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D1%82%D1%8C%D1%81%D1%8F_%D1%81_InSales
83
+ licenses: []
84
+
85
+ post_install_message:
86
+ rdoc_options: []
87
+
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ hash: 3
96
+ segments:
97
+ - 0
98
+ version: "0"
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ requirements: []
109
+
110
+ rubyforge_project: insales_api
111
+ rubygems_version: 1.8.10
112
+ signing_key:
113
+ specification_version: 3
114
+ summary: Gem for accessing the InSales REST web services
115
+ test_files: []
116
+