angular.module('EssayApp.services') .factory('dataService', ['$http', '$q', '$window', ($http, $q, $window) -> _params = $.url().param() api = { cashed_locale: undefined } api.locale = -> unless angular.isDefined(api.cashed_locale) api.cashed_locale = /^\/([a-z]{2})(?:\/|$)/.exec(window.location.pathname)?[1] || '' api.cashed_locale if api.cashed_locale.length == 2 api.localized_path = (path, locale_in_path = false, force_locale = undefined) -> if locale = (force_locale || api.locale()) if locale_in_path ["/#{locale}", path].join('') else [path, "locale=#{locale}"].join('?') else path api.get_languages = -> $http.get(api.localized_path('/data/languages.json', true), cache: true) api.my_country_code = -> $http.get('/data/country/code.json', cache: true) api.get_countries = -> $http.get(api.localized_path('/data/countries.json', true), cache: true) api.get_feedback_reasons = -> $http.get(api.localized_path('/data/feedback_reasons.json', true), cache: true) api.feedbacks = { writer_orders: (writer_id, cache = true)-> $http.get(api.localized_path("/account/feedbacks/#{writer_id}/orders.json", true), cache: cache) submit: (feedback, violation = undefined)-> params = {feedback: feedback} params['violation'] = violation if violation? # TODO $http.post( api.localized_path('/account/feedbacks.json', true), JSON.stringify(params), { headers: { 'Content-Type':'application/json' }} ) } api.get_rates = -> $http.get(api.localized_path('/data/rates.json', true), cache: true) api.payment = { rates: (payment_token) -> $http.get(api.localized_path("/payments/transactions/#{payment_token}.json", true), cache: true) update: (payment_token, data) -> params = { payment_type_id: data.payment_type.id currency: data.currency.currency } if data?.features? selected = data.features.filter (x) -> x.active if selected.length > 0 for feature in selected params.features = {} params.features[feature.code] ||= feature.id $http.put( api.localized_path("/payments/transactions/#{payment_token}.json", true), params ) } api.tickets = { data: -> $http.get(api.localized_path('/account/tickets/data.json', true), cache: true) create: (data) -> $http.post( api.localized_path("/account/tickets.json", true), JSON.stringify(data), { headers: { 'Content-Type':'application/json' }} ) } api.referrals = { data: -> $http.get(api.localized_path('/account/referrals/data.json', true), cache: true) create: (params)-> $http.post( api.localized_path('/account/referrals.json', true), JSON.stringify(params), { headers: { 'Content-Type':'application/json' }} ) update: (params)-> $http.post( api.localized_path('/account/referrals/profile.json', true), JSON.stringify(params), { headers: { 'Content-Type':'application/json' }} ) resend_email: ()-> $http.post( api.localized_path('/account/referrals/resend_email.json', true), JSON.stringify({}), { headers: { 'Content-Type':'application/json' }} ) } api.features = { get: (order_id) -> $http.get(api.localized_path("/account/orders/#{order_id}/features.json", true), cache: true) add: (order_id, data) -> selected = data.features.filter (x) -> x.active return undefined unless selected.length > 0 data = {} for feature in selected data[feature.code] ||= feature.id # order['discount'] = 1 if discount $http.post( api.localized_path("/account/orders/#{order_id}/features.json", true), JSON.stringify(data), { headers: { 'Content-Type':'application/json' }} ) } api.free_quote = { create: (endpoint, data) -> $http.post(endpoint, JSON.stringify({free_quote: data}), { headers: { 'Content-Type':'application/json' } }) } api.profile = { update: (scope, params) -> data = {} data[scope] = params $http.post( api.localized_path('/account/profile.json', true), JSON.stringify(data), { headers: { 'Content-Type':'application/json' }} ) } api.phone_confirmation = { send: (params)-> $http.post( api.localized_path("/account/profile/phone_verifications.json", true), JSON.stringify(params), { headers: { 'Content-Type':'application/json' }} ) confirm: (token, params)-> # $.ajax({method: 'PATCH', url: '/account/profile/phone_verifications/9ul1SOf6VOQgc8LdfUgG9A.json', data: $.param({code: '303030'})}) $http.patch( api.localized_path("/account/profile/phone_verifications/#{token}.json", true), JSON.stringify(params), { headers: { 'Content-Type':'application/json' }} ) } api.param = (key, items) -> value = _params[key] if angular.isArray(items) if value id = parseInt(value, 10) records = items.filter (x) -> x.id is id if records.length > 0 then records[0] else items[0] else items[0] else if value return value else if items return items else return null api.anyParam = () -> expr = /^(.+)\[(.+)\]$/ig for key in arguments _items = expr.exec(key) if _items? && _items.length > 2 if _params[_items[1]]? _record = _params[_items[1]] if _record[_items[2]]? return _record[_items[2]] else if _params[key]? return _params[key] api.params = () -> return _params api.isInParam = (key, value) -> _records = _params[key] if angular.isArray(_records) return $.inArray(value.toString(), _records) > -1 else return false return api ])