lib/moip-assinaturas/client.rb in moip-assinaturas-0.2.0 vs lib/moip-assinaturas/client.rb in moip-assinaturas-0.2.1

- old
+ new

@@ -64,23 +64,28 @@ def create_subscription(subscription, new_customer, opts={}) prepare_options(opts, { body: subscription.to_json, headers: { 'Content-Type' => 'application/json' } }) peform_action!(:post, "/subscriptions?new_customer=#{new_customer}", opts) end + def update_subscription(subscription_code, subscription_changes, opts={}) + prepare_options(opts, { body: subscription_changes.to_json, headers: { 'Content-Type' => 'application/json' } }) + peform_action!(:put, "/subscriptions/#{subscription_code}", opts, true) + end + def list_subscriptions(opts={}) prepare_options(opts, { headers: { 'Content-Type' => 'application/json' } }) - peform_action!(:get, "/subscriptions", opts) + peform_action!(:get, "/subscriptions", opts) end def details_subscription(code, opts={}) prepare_options(opts, { headers: { 'Content-Type' => 'application/json' } }) peform_action!(:get, "/subscriptions/#{code}", opts) end def suspend_subscription(code, opts={}) prepare_options(opts, { headers: { 'Content-Type' => 'application/json' } }) - peform_action!(:put, "/subscriptions/#{code}/suspend", opts, true) + peform_action!(:put, "/subscriptions/#{code}/suspend", opts, true) end def activate_subscription(code, opts={}) prepare_options(opts, { headers: { 'Content-Type' => 'application/json' } }) peform_action!(:put, "/subscriptions/#{code}/activate", opts, true) @@ -96,54 +101,54 @@ peform_action!(:get, "/invoices/#{id}", opts) end def list_payments(invoice_id, opts={}) prepare_options(opts, { headers: { 'Content-Type' => 'application/json' } }) - peform_action!(:get, "/invoices/#{invoice_id}/payments", opts) + peform_action!(:get, "/invoices/#{invoice_id}/payments", opts) end def details_payment(id, opts={}) - prepare_options(opts, { headers: { 'Content-Type' => 'application/json' } }) - peform_action!(:get, "/payments/#{id}", opts) + prepare_options(opts, { headers: { 'Content-Type' => 'application/json' } }) + peform_action!(:get, "/payments/#{id}", opts) end private def prepare_options(custom_options, required_options) custom_options.merge!(required_options) if custom_options.include?(:moip_auth) - custom_options[:basic_auth] = { - username: custom_options[:moip_auth][:token], + custom_options[:basic_auth] = { + username: custom_options[:moip_auth][:token], password: custom_options[:moip_auth][:key] } if custom_options[:moip_auth].include?(:sandbox) if custom_options[:moip_auth][:sandbox] custom_options[:base_uri] = "https://sandbox.moip.com.br/assinaturas/v1" else custom_options[:base_uri] = "https://api.moip.com.br/assinaturas/v1" end - end + end custom_options.delete(:moip_auth) end custom_options end def peform_action!(action_name, url, options = {}, accepts_blank_body = false) if (Moip::Assinaturas.token.blank? or Moip::Assinaturas.key.blank?) - raise(MissingTokenError, "Informe o token e a key para realizar a autenticação no webservice") + raise(MissingTokenError, "Informe o token e a key para realizar a autenticação no webservice") end response = self.send(action_name, url, options) - + # when updating a plan the response body is empty and then # the response.nil? returns true despite that the response code was 200 OK # that is why I changed the response.nil? by the current if response.nil? && !accepts_blank_body - raise(WebServerResponseError, "Ocorreu um erro ao chamar o webservice") + raise(WebServerResponseError, "Ocorreu um erro ao chamar o webservice") end response end \ No newline at end of file