Class: Sellsy::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/sellsy/api.rb

Defined Under Namespace

Classes: Configuration

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



7
8
9
# File 'lib/sellsy/api.rb', line 7

def configuration
  @configuration
end

Class Method Details

.authentication_headerObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sellsy/api.rb', line 15

def self.authentication_header
  encoded_key = URI::escape(self.configuration.consumer_secret) + "&" + URI::escape(self.configuration.user_secret)
  now = Time.now
  oauth_params = {
      'oauth_consumer_key' => self.configuration.consumer_token,
      'oauth_token' => self.configuration.user_token,
      'oauth_nonce' => Digest::MD5.hexdigest((now.to_i + rand(0..1000)).to_s),
      'oauth_timestamp' => now.to_i.to_s,
      'oauth_signature_method' => 'PLAINTEXT',
      'oauth_version' => '1.0',
      'oauth_signature' => encoded_key
  }

  return 'OAuth ' + oauth_params.map { |k, v| k + '="' + v + '"' }.join(', ')
end

.client=(client) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sellsy/api.rb', line 31

def self.client=(client)
  payload = {
      'method' => 'Client.create',
      'params' => {
          'third' => {
              'name' => client.client_company.name,
              'email' => client.client_company.email,
              'tel' => client.client_company.landline,
              'fax' => client.client_company.fax,
              'siret' => client.client_company.billing_information.company_registration,
              'vat' => client.client_company.billing_information.vat_registration
          },
          'contact' => {
              'name' => client.last_name,
              'forename' => client.first_name,
              'email' => client.email,
              'tel' => client.landline,
              'fax' => client.fax,
              'mobile' => client.mobile
          },
          'address' => {
              'name' => client.address.name,
              'part1' => client.address.street,
              'part2' => client.address.addition,
              'zip' => client.address.postal_code,
              'town' => client.address.city,
              'countrycode' => "FR"
          }
      }
  }

  self.request payload
end

.clientsObject



65
66
67
68
69
70
71
72
# File 'lib/sellsy/api.rb', line 65

def self.clients
  payload = {
      'method' => 'Client.getList',
      'params' => {}
  }

  self.request payload
end

.configure {|configuration| ... } ⇒ Object

Yields:



10
11
12
13
# File 'lib/sellsy/api.rb', line 10

def self.configure
  self.configuration ||= Configuration.new
  yield(configuration)
end

.infoObject



173
174
175
176
177
178
179
180
# File 'lib/sellsy/api.rb', line 173

def self.info
  payload = {
      :method => 'Infos.getInfos',
      :params => {}
  }

  self.request payload
end

.invoices=(invoice) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/sellsy/api.rb', line 74

def self.invoices=(invoice)
  payload = {
      'method' => 'Document.create',
      'params' => {
          'document' => {
              'doctype' => 'doctype',
              'parentId' => 'parentId',
              'thirdid' => 'clientid',
              'displayedDate' => 'displayedDate',
              'subject' => 'document_subject',
              'notes' => 'document_notes',
              'tags' => 'document_tags',
              'displayShipAddress' => 'displayshippaddress_enum',
              'rateCategory' => 'rateCategory',
              'globalDiscount' => 'globalDiscount',
              'globalDiscountUnit' => 'globalDiscountUnit',
              'hasDoubleVat' => 'hasDoubleVat',
              'currency' => 'currency',
              'doclayout' => 'doclayout',
              'payMediums' => 'payMediums',

          },
          'paydate' => {
              'id' => 'paydate_id',
              'xdays' => 'paydate_xdays',
              'endmonth' => 'paydate_endmonth',
              'scaledDetails' => 'paydate_scaledDetails',
              'custom' => 'paydate_custom'

          },
          'thirdaddress' => {
              'id' => 'thirdaddress_id'
          },
          'shipaddress' => {
              'id' => 'shipaddress_id'
          },
          'row' => {
              '1' => {
                  'row_type' => 'packaging',
                  'row_packaging' => 'packagin_name',
                  'row_name' => 'row_name',
                  'row_unitAmount' => 'row_unit_amount',
                  'row_tax' => 'row_taxrate',
                  'row_taxid' => 'row_taxid',
                  'row_tax2id' => 'row_tax2id',
                  'row_qt' => 'row_quantity',
                  'row_isOption' => 'row_option',
                  'row_discount' => 'row_discount',
                  'row_discountUnit' => 'row_discountUnit'
              },
              '2' => {
                  'row_type' => 'shipping',
                  'row_shipping' => 'shipping_name',
                  'row_name' => 'row_name',
                  'row_unitAmount' => 'row_unit_amount',
                  'row_tax' => 'row_taxrate',
                  'row_taxid' => 'row_taxid',
                  'row_tax2id' => 'row_tax2id',
                  'row_qt' => 'row_quantity',
                  'row_isOption' => 'row_option',
                  'row_discount' => 'row_discount',
                  'row_discountUnit' => 'row_discountUnit'
              },
              '3' => {
                  'row_type' => 'item',
                  'row_linkedid' => 'catalogue_id_link',
                  'row_declid' => 'catalogue_declid_link',
                  'row_name' => 'row_name',
                  'row_notes' => 'row_notes',
                  'row_unit' => 'row_unit',
                  'row_unitAmount' => 'row_unit_amount',
                  'row_tax' => 'row_taxrate',
                  'row_taxid' => 'row_taxid',
                  'row_tax2id' => 'row_tax2id',
                  'row_qt' => 'row_quantity',
                  'row_isOption' => 'row_option',
                  'row_purchaseAmount' => 'row_purchaseAmount',
                  'row_discount' => 'row_discount',
                  'row_discountUnit' => 'row_discountUnit'
              },
              '4' => {
                  'row_type' => 'once',
                  'row_name' => 'row_name',
                  'row_notes' => 'row_notes',
                  'row_unit' => 'row_unit',
                  'row_unitAmount' => 'row_unit_amount',
                  'row_tax' => 'row_taxrate',
                  'row_taxid' => 'row_taxid',
                  'row_tax2id' => 'row_tax2id',
                  'row_qt' => 'row_quantity',
                  'row_isOption' => 'row_option',
                  'row_discount' => 'row_discount',
                  'row_discountUnit' => 'row_discountUnit'
              }
          }
      }
  }
end

.request(payload) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/sellsy/api.rb', line 182

def self.request(payload)
  params = {
      'request' => 1,
      'io_mode' => 'json',
      'do_in' => payload.to_json
  }

  puts params.to_json

  RestClient.log = 'stdout'
  RestClient.post 'https://apifeed.sellsy.com/0/', {:request => 1, :io_mode => 'json', 'do_in' => payload.to_json, :multipart => true}, {:authorization => self.authentication_header}
end