Module: ShareProgress::Button
- Defined in:
- lib/shareprogress.rb
Class Method Summary collapse
Class Method Details
.create(data) ⇒ Object
27 28 29 30 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 64 65 66 67 68 69 70 71 72 73 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 |
# File 'lib/shareprogress.rb', line 27 def self.create(data) # Validates the input (a hash) to send a POST request to # ShareProgress to create a share button. The result could be: # a) A hash containing the information of the created button or, # b) An Error message from ShareProgress (i.e. 'Bad API key'). # Arguments received: # data: A hash containing all the info about the button. For example: # { # "key" => "123456", # "page_url" => "http://sumofus.org/", # "page_title" => "My button name", # "auto_fill" => true, # "button_template" => "sp_em_large", # "variants" => { # "email" => [ # {"email_subject" => "Email subject 1!", # "email_body" => "Email body 1 {LINK}"}, # {"email_subject" => "Email subject 2!", # "email_body" => "Email body 2 {LINK}"}, # {"email_subject" => "Email subject 3!", # "email_body" => "Email body 3 {LINK}"} # ] # }, # "advanced_options" => { # "automatic_traffic_routing" => true, # "buttons_optimize_actions" => true, # "customize_params" => { # "param" => "param_to_use", # "e" => "email_source", # "f" => "facebook_source", # "t" => "twitter_source", # "o" => "dark_social_source" # }, # "id_pass" => { # "id" => "id", # "passed" => "referrer_id" # } # } # } # Example of a successful response: # { # "id"=>12136, # "page_url"=>"http://sumofus.org/", # "page_title"=>"My button name", # "button_template"=>"sp_em_large", # "share_button_html"=>"<div class='sp_12136 sp_em_large' ></div>", # "found_snippet"=>false, # "is_active"=>false, # "variants"=>{ # "facebook"=>[{ # "id"=>48542, # "facebook_title"=>"SumOfUs", # "facebook_description"=>"SumOfUs is a global movement of "\ # "consumers, investors, and workers all around the world, "\ # "standing together to hold corporations accountable for their "\ # "actions and forge a new, sustainable and just path for our "\ # "global economy. It's not going to be fast or easy. B", # "facebook_thumbnail"=>"http://sumofus.org/wp-content/themes/pgm/"\ # "img/default-facebook.jpg"}], # "email"=>[{ # "id"=>48539, # "email_subject"=>"Email subject 1!", # "email_body"=>"Email body 1 {LINK}"}, { # "id"=>48540, # "email_subject"=>"Email subject 2!", # "email_body"=>"Email body 2 {LINK}"}, { # "id"=>48541, # "email_subject"=>"Email subject 3!", # "email_body"=>"Email body 3 {LINK}"}], # "twitter"=>[{ # "id"=>48543, # "twitter_message"=>"SumOfUs {LINK}"}]}, # "advanced_options"=>{ # "automatic_traffic_routing"=>true, # "buttons_optimize_actions"=>true, # "customize_params"=>{ # "param"=>"param_to_use", # "e"=>"email_source", # "f"=>"facebook_source", # "t"=>"twitter_source", # "o"=>"dark_social_source"}, # "id_pass"=>{ # "id"=>"id", # "passed"=>"referrer_id"}} # } = NewButton.new(data) unless .valid? return .errors end = .variants.merge(button_template: .) if .variants["email"] variants = NewEmailVariants.new() elsif .variants["twitter"] variants = NewTwitterVariants.new() elsif .variants["facebook"] variants = NewFacebookVariants.new() end unless variants.valid? return variants.errors end # send request to ShareProgress to create a button begin request = request("create", .attributes) return request["response"][0] rescue Requests::Error => e return JSON.parse(e.response.body)["message"] end end |
.request(method, payload) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/shareprogress.rb', line 11 def self.request(method, payload) case method when "create" url = "https://run.shareprogress.org/api/v1/buttons/update" when "update" # ... end response = Requests.request("POST", url, data: JSON.dump(payload), headers: { "Content-Type" => "application/json" } ) return JSON.parse(response.body) end |